【问题标题】:searching for specific keys in multiple independent hashMap objects在多个独立的 hashMap 对象中搜索特定键
【发布时间】:2012-06-20 08:30:26
【问题描述】:

在开始进一步编写我的解决方案之前,我有一个稍微有点理论性的问题想弄清楚...

背景。 我需要在 2 个(或更多)MS Access 文件之间进行比较。

每个文件都应包含在其他文件中找到的数据。

由于我在使用 JDBC 和连接到 Access 结果集时遇到了“限制”(它们只能向前滚动!),我创建了对结果集结构进行建模的“java 对象”(类)。

本质上 我有一个对象,它模拟结果集中的单个记录行(让我们称之为 rowSet) 然后一个 resSet 对象有一个 rowSet 的“数组”。

但是,为了“加快速度”,我捕获了键和索引列中的值,并为相关的 rowSet 对象创建了一个 'key_Index' 的 hashMap。

Me 比较然后执行以下操作。

获取第一个 resSet 对象(用作主对象),从中收集各个 Key_Index 哈希图(我们称其为 'aKey'....

现在使用这个 'aKey' 对象搜索其他可用的 resSet 对象,看看是否有任何包含与 'aKey' 中的值匹配的 key_Index 值。

但是我刚刚有一个相当烦人的想法。

如果我使用代码 resSet.get(aKey) 在我的其他 resSet 对象之一上我会遇到问题,因为“aKey”对象显然不是同一个对象 - 尽管它的内容应该相同(即可比较)。

我不得不说,当我阅读我的问题时,我认为它的措辞不好......所以我想我将包含我创建的课程的副本......

重要部分:

成员: 挑战 - “结果集”类型对象的 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。欢迎对我的代码发表任何评论或更多评论

提前致谢

大卫

编辑: 我刚刚发现了这个问题,Nested Maps or combined keys in java 这将是我的解决方案吗,创建一个“自定义”key_Index 对象,然后为其定义一个 hashcode() 和 equals 对象。但是我已经为我的 key_index 对象使用了“hashMap”,所以这会在我没有注意到的地方自动执行吗?

【问题讨论】:

  • 是否可以缩短您的问题?这很可能是一个简单的答案。
  • @PeterLawrey 同意,但是当我尝试缩短它时,我觉得我没有正确解释。此外,我将代码作为“以防万一”类型添加包含在内。因为我有一半希望有人无论如何都会要求它

标签: java hashmap key


【解决方案1】:

你说:

如果我在我的其他 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

    }

}

【讨论】:

  • 查看我的编辑,我正在使用 hashMap,我的阅读告诉我我可能需要使用树形映射,或者创建一个特殊对象 'key_index' 并拥有它实现了equals和hashcode方法,哪个更好?
  • 您可以使用 TreeMap 但您必须提供一个比较器或实现 Comparable。如果您不想实现 equals 和 hashcode,这是另一种选择。
  • 我刚刚又看了一遍文档,HashMap 从其父 AbstractMap 继承了 equals 和 hashcode 方法,我想在这种情况下我会没事的(尽管总是有可能在 DB 之间输入的键值不同!)
  • 您必须在正在使用的类中实现hashcode()equals(),该类在地图中有键。
  • 酷,多棒的明星。谢谢你的例子。然而,较小的问题仍然存在,因为我的键值实际上是 HashMap 的对象,这是否不继承其父级的 equals 和 hashcode 方法。或者像您所做的那样,将我的“key_index”实现为自己的真实对象是否是一个更好的设计。 PS 我希望我能这么快地把代码拼凑起来。
猜你喜欢
  • 2012-10-30
  • 1970-01-01
  • 2018-09-10
  • 2021-12-12
  • 1970-01-01
  • 1970-01-01
  • 2012-11-23
  • 2011-10-13
  • 1970-01-01
相关资源
最近更新 更多