[英]searching for specific keys in multiple independent hashMap objects
在我開始編程我的解決方案之前,我有一個稍微理論化的問題,我想要澄清......
背景。 我需要在2個(或更多)MS Access文件之間進行比較。
每個文件都應包含在其他文件中找到的數據。
由於我遇到JDBC的“限制”並連接到Access結果集(它們只能向前滾動!)我創建了“java對象”(類)來模擬結果集的結構。
本質上我有一個對象來模擬結果集中的單個記錄行(讓我們稱之為rowSet)然后resSet對象有一個rowSets的“數組”。
但是為了“加快速度”,我會捕獲鍵和索引列中的值,並為相關的rowSet對象創建一個'key_Index'的hashMap。
我比較然后做以下。
取第一個resSet對象(用作主服務器),從中收集個人Key_Index的hashmaps(讓我們稱之為'aKey'......
現在使用這個'aKey'對象搜索其他可用的resSet對象,看看是否有任何包含與'aKey'中的值匹配的key_Index的值。
然而,我剛剛有一個相當討厭的想法。
如果我在其他一個resSet對象上使用代碼resSet.get(aKey),我會遇到問題,因為'aKey'對象顯然不是同一個對象 - 盡管它的內容應該是相同的(即可比較)。
我不得不說,當我閱讀我的問題時,我認為它的措辭不夠......所以我想我會包含我創建的課程副本......
重要的部分:
members:challenge - “結果集”類型對象的arrayList。
方法:runChallenge()
package KWhite;
/**
* the RScomparator is designed for the instance of comparing a double entry database system, as
* often occurs in localy run medical trials data.
*
* The double entry is to ensure that there are no errors made during input, the entry is performed
* in separate independant instances. The RScolmparator object is specifically able to take any
* number of result sets as its input (ie queries from multiple independantly created databases)
*
* It should be recognised that this object should probably be called as part of a DBcomparator
* object.
*
*/
//imports here
//import of logger class and required dependencies...
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.log4j.PropertyConfigurator;
import MrBlue.DB_Table;
import TawuaiLogger.tawuaiLogger;
public class RScomparator {
//Static variables
//the logger instance
private static final TawuaiLogger.tawuaiLogger errLog = new tawuaiLogger("RScomparator");
//class member variables
/** this is a selection of ResSet objects that are going to have thier data challenged */
private ArrayList<ResSet_KW> challenge;
/**the name of the current table being challenged*/
private String tableName;
/**a 'table' object for meta data reference purposes, this can be used for getting column types etc */
private DB_Table table;
//These are our report objects
/**a report array for challenge failures */
private ArrayList<report_KW> fail;
/**a report array for errors, ie no challenger able to be made, no equivalent value found */
private ArrayList<report_KW> errors;
/**a report array for the good values */
private ArrayList<report_KW> success;
/** this is either the main class or the constructor
*
* If it is a constructor rename to reflect the name of the class
* @param args
*/
public RScomparator(DB_Table t) //TODO add arguments as required
{
PropertyConfigurator.configure("Log4j.properties");
// TODO Auto-generated method stub
challenge = new ArrayList<ResSet_KW>();
//initialise our report objects for this challenge scenario
fail = new ArrayList<report_KW>();
errors = new ArrayList<report_KW>();
success = new ArrayList<report_KW>();
table = t;
tableName = t.getTblName();
}
//class methods go here
/**
* add a result set object into this comparator
*
* @param r the result set object being inserted into this challenge.
*
*/
public void addChallenger(ResSet_KW r)
{
this.challenge.add(r);
}
/**
* this runs the comparison process...
* Although no details in of itself are returned it calls other methods that do return a value
*
*/
public void runChallenge()
{
//TODO finish this method, creating a report object on the way
//these are the 2 result set objects that will be compared
ResSet_KW gold = new ResSet_KW ();
ResSet_KW silver = new ResSet_KW ();
//ensure the challenger list has objects in it.
if(challenge.size() < 2)
{
//it must have 2 objects..
errLog.add(3, "there are no results available for comparison of table " + this.tableName);
//either way we should create report object.
this.errors.add( new report_KW(tableName));
//break out of the method.
return;
}
//get the first row of data
gold = challenge.get(0);//the first result set.
//for each column in the result set, perform a search for the same key in the others..
for(HashMap<String, String> c : gold.getRS().keySet())
{//c is the key value in the map
//cycle over the challenge object
for (int i=1; i<challenge.size(); i++)//we don't want to use the first element, so start from 1 not zero
{
silver = challenge.get(i);
if (silver.hasKey(c))
{
//a temp object for meta data referencing
//only get the actual result values if there is a match
Column_KW a = gold.getRS().get(c);
Column_KW b = silver.getRS().get(c);
//make the comparison
a.compareTo(b, this.table);
//get the reports from the comparison
for(report_KW k :a.getFailure())
{
this.fail.add(k);
}
for(report_KW k :a.getPassed())
{
this.success.add(k);
}
for(report_KW k :a.getPassed())
{
this.errors.add(k);
}
}
else
{
break;//return to the next item in the for loop
}
}
}
}
/**
*a helper method to create the error message creator
*@ param m the extra message if any,
*@return s the full message
*/
private String getErrMessage(String m) {
//the third element in the current stact trace should be the calling method
StackTraceElement caller = Thread.currentThread().getStackTrace()[3];
String s = m + "\ncalled from line " + caller.getLineNumber()
+ "\nmethod: " + this.getClass() + "." + caller.getMethodName();
return s;
}
}//end class
PS。 我的代碼或更多歡迎的任何coments
提前致謝
大衛
編輯:我剛剛發現這個問題, 嵌套映射或java中的組合鍵這將是我的解決方案,創建一個'自定義'key_Index對象,然后為它定義一個hashcode()和equals對象。 但是我已經為我的key_index對象使用了'hashMap',所以這是否會在我注意到的地方自動執行?
你說 :
如果我在其他一個resSet對象上使用代碼resSet.get(aKey),我會遇到問題,因為'aKey'對象顯然不是同一個對象 - 盡管它的內容應該是相同的 (即可比較)。
您必須分別實現equals()
和hashcode()
。 實現equals()
是不夠的,如果對象相等, hashcode()
必須相同。
例:
import java.util.HashMap;
import java.util.Map;
class A {
String name;
Integer number;
public A(String name, Integer number) {
super();
this.name = name;
this.number = number;
}
}
class B {
String name;
Integer number;
public B(String name, Integer number) {
super();
this.name = name;
this.number = number;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof B) {
return obj == this || (name.equals(((B) obj).name) && number.equals(((B) obj).number));
}
return false;
}
@Override
public int hashCode() {
return name.hashCode() + number.hashCode();
}
}
public class TestHashMap {
public static void main(String... args) {
A a1 = new A("a", 1);
A anotherA1 = new A("a", 1);
Map<A, String> as = new HashMap<A, String>();
as.put(a1, "a1");
System.out.println(as.get(anotherA1)); // prints null
B b1 = new B("b", 1);
B anotherB1 = new B("b", 1);
Map<B, String> bs = new HashMap<B, String>();
bs.put(b1, "b1");
System.out.println(bs.get(anotherB1)); // prints b1
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.