简体   繁体   English

将两个HashMaps组合成第三个

[英]Combining two HashMaps into a third

Let's say I have an object of each class below, and I put each object in a hashmap where IDnumber is the key in both maps. 假设我有一个下面每个类的对象,我将每个对象放在一个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>();

Now, how can I mash these two HashMaps into a third HashMap so that I can have the key IDnumber, and the values name, company, income, and workyears? 现在,我如何将这两个HashMaps混合到第三个HashMap中,以便我可以获得密钥IDnumber,以及值name,company,income和workyears?

You can not do that. 你不能这样做。 You have two different classes, and java is not going to auto-magically make them one. 你有两个不同的类,java不会自动神奇地使它们成为一个类。

You could create a new third class to merge the info: 您可以创建一个新的第三个类来合并信息:

public Class3 {

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

Then loop through your map to get the entries, creating new Class3 instance for each and place them in a new HashMap<Integer, Class3> . 然后遍历您的映射以获取条目,为每个创建新的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));
}

Create a 3rd class called Combined in one of these 2 ways: 使用以下两种方法之一创建名为Combined的第三个类:

Combined {
    class1 info1;
    class2 info2;
}

Or, better: 或更好:

Combined {
    int IDNumber = 123;
    String name = John;
    String company = Intel;
    int income = 500;
    int workYears = 3;
}
  • Create a 3rd (empty) HashMap 创建第3个(空)HashMap
  • Now iterate over all elements in the first HashMap you had before 现在迭代你之前的第一个HashMap中的所有元素
  • For each entry, look up the same key in the 2nd HashMap: 对于每个条目,在第二个HashMap中查找相同的键:
    • If it is found, combine the information from these 2 entries and add it as a single entry of instance Combined to the 3rd HashMap. 如果发现,从这些条目2相结合的信息,并将其添加为实例的一个条目Combined到第三HashMap中。 Then remove both of these entries from both HashMaps one and two. 然后从HashMaps 1和2中删除这两个条目。
    • If it is not found, then create a Combined instance anyway based on the entry in HashMap one and just set the unavailable information that would have come from a corresponding entry from HashMap two to null. 如果找不到,则根据HashMap中的条目创建一个Combined实例,只需将来自HashMap 2的相应条目的不可用信息设置为null。 Remove the entry from HashMap one. 从HashMap中删除一个条目。
  • Now the 1st HashMap should be empty. 现在第一个HashMap应该是空的。 Iterate HashMap two to find any entries that did not have a corresponding ID in HashMap one. 迭代HashMap二,找到HashMap中没有相应ID的任何条目。 Add them to the 3rd HashMap as above. 如上所述将它们添加到第3个HashMap。

You can't store multiple values (ie Class1 and Class2 in your case) with the same key in a java.util.Map. 您不能在java.util.Map中使用相同的键存储多个值(例如,在您的情况下为Class1和Class2)。 What you want is a Multimap . 你想要的是一个Multimap Guava has an implementation for this. 番石榴有一个实现。 The one you are looking for is ArrayListMultimap . 您正在寻找的是ArrayListMultimap

You need to make a new class which is a mashup of class1 and class2 along with a new Map for it. 你需要创建一个新类,它是class1class2的mashup以及一个新的Map Each time you put something in one and 'two' you make a new mashup containing both objects and put it into three . 每次你把一些东西放在one和'两个'中你就会制作一个包含两个对象的新mashup并将其分成three

You can use an ArrayList to create an HashMap with multiple of values for one key 您可以使用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 */

But it's not very optimized, if you aren't obliged to have an HashMap at final, use directly a multiHashMap: http://commons.apache.org/collections/apidocs/org/apache/commons/collections/MultiHashMap.html 但它并没有得到很好的优化,如果您没有义务在最终使用HashMap,请直接使用multiHashMap: http ://commons.apache.org/collections/apidocs/org/apache/commons/collections/MultiHashMap.html

As your data structure is not solit you should use some adapter/wraper 由于您的数据结构不是solit,您应该使用一些适配器/包装器

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;
  }

}

Then in your code you can declare a map: 然后在您的代码中,您可以声明一个地图:

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

Are you expecting this kind of arrangment 你期待这种安排吗?

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);
}
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM