将两个HashMaps组合成第三个

时间:2012-11-08 15:48:35

标签: java hashmap

假设我有一个下面每个类的对象,我将每个对象放在一个hashmap中,其中IDnumber是两个映射中的关键。

class1 {
 int IDNumber = 123;  //same person as class2
 String name = John;
 String company = Intel;

 class2 { 
 int IDNumber = 123;  //same person as class1
 int income = 500;
 int workYears = 3;
 } 

HashMap<Integer, class1> one = new Hashmap<Integer, class1>();
HashMap<Integer, class2> two = new HashMap<Integer, class2>();

现在,我如何将这两个HashMaps混合到第三个HashMap中,以便我可以获得密钥IDnumber,以及值name,company,income和workyears?

7 个答案:

答案 0 :(得分:2)

你做不到。你有两个不同的类,java不会自动地将它们变成一个。

您可以创建一个新的第三个类来合并信息:

public Class3 {

   public Class3(Class1 class1, Class2 class2){
       //pull the info you want from each into variables in this class
   }
}

然后遍历您的地图以获取条目,为每个条目创建新的Class3实例并将它们放入新的HashMap<Integer, Class3>

//gets the keys from the hashmap
Set<Integer> keys = one.keySet();
//merge the keys from the second hashmap
keys.addAll(two.keySet());
//new hashmap
Map<Integer, Class3> newMap = new HashMap<Integer, Class3>();
for (Integer key : keys){
     //create new instance and place it in the map
     newMap.put(key, new Class3(one.get(key), two.get(key));
}

答案 1 :(得分:1)

使用以下两种方式之一创建名为Combined的第三个类:

Combined {
    class1 info1;
    class2 info2;
}

或者,更好:

Combined {
    int IDNumber = 123;
    String name = John;
    String company = Intel;
    int income = 500;
    int workYears = 3;
}
  • 创建第3个(空)HashMap
  • 现在迭代你之前的第一个HashMap中的所有元素
  • 对于每个条目,在第二个HashMap中查找相同的键:
    • 如果找到,请合并这两个条目中的信息,并将其作为实例Combined的单个条目添加到第3个HashMap。然后从HashMaps 1,2和2中删除这两个条目。
    • 如果找不到,则根据HashMap中的条目创建一个Combined实例,只需将来自HashMap 2的相应条目的不可用信息设置为null。从HashMap中删除条目。
  • 现在第一个HashMap应该是空的。迭代HashMap二,找到HashMap中没有相应ID的任何条目。如上所述将它们添加到第3个HashMap。

答案 2 :(得分:1)

您不能在java.util.Map中使用相同的键存储多个值(例如,在您的情况下为Class1和Class2)。你想要的是MultimapGuava有一个实现。您正在寻找的是ArrayListMultimap

答案 3 :(得分:0)

您需要创建一个新类,它是class1class2的混搭以及新的Map。每次在one和“两个”中添加内容时,您都会创建一个包含两个对象的新mashup并将其放入three

答案 4 :(得分:0)

您可以使用ArrayList为一个键创建具有多个值的HashMap

ArrayList<> list = new ArrayList();

/* populate your list here with two class having the same ID */

HashMap<Integer, List> three = new Hashmap();

/* Put list on the Hashmap */

/* Redo the operation with another ID */

但它并没有得到很好的优化,如果您没有义务在最终使用HashMap,请直接使用multiHashMap:http://commons.apache.org/collections/apidocs/org/apache/commons/collections/MultiHashMap.html

答案 5 :(得分:0)

由于您的数据结构不是孤独的,您应该使用一些适配器/包装器

public class UserClassWrapper {

  private final UserClass1 class1;
  private final UserClass2 class2;

  UserWithDetail(UserClass1 class1, UserClass2 class2) {
    //Check preconditions as class1 and class2 must not be null and have equal id
    this.class1 = class1;
    this.class2 = class2;
  }

}

然后在您的代码中,您可以声明一张地图:

private Map<Integer,UserClassWrapper> userClassWrappeMap = new HashMap<>();

答案 6 :(得分:-1)

您是否期待这种安排

class class1 {
 int IDNumber = 123;  //same person as class2
 String name = "John";
 String company = "Intel";
 }

 class class2 { 

 int IDNumber = 123;  //same person as class1
 int income = 500;
 int workYears = 3;
 } 


 public class MyData{
public static void main(String arg[]){
    HashMap<Integer, class1> one = new HashMap<Integer, class1>();
    HashMap<Integer, class2> two = new HashMap<Integer, class2>();
    one.put(1, new class1());
    two.put(2, new class2());

    HashMap<Integer, Object> three = new HashMap<Integer, Object>();
    three.putAll(one);
    three.putAll(two);

    System.out.println(three);
}
 }
相关问题