1

以下に各クラスのオブジェクトがあり、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>();

では、これら2つのHashMapを3番目のHashMapにマッシュアップして、キーID番号と、名前、会社、収入、および労働年の値を取得するにはどうすればよいでしょうか。

4

7 に答える 7

2

それをしてはいけない。2つの異なるクラスがあり、Javaはそれらを自動的に1つにすることはありません。

新しい3番目のクラスを作成して、情報をマージできます。

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));
}
于 2012-11-08T15:53:09.753 に答える
1

Combined次の2つの方法のいずれかで呼び出される3番目のクラスを作成します。

Combined {
    class1 info1;
    class2 info2;
}

または、より良い:

Combined {
    int IDNumber = 123;
    String name = John;
    String company = Intel;
    int income = 500;
    int workYears = 3;
}
  • 3番目の(空の)HashMapを作成します
  • これで、前に持っていた最初のHashMapのすべての要素を繰り返し処理します
  • エントリごとに、2番目のHashMapで同じキーを検索します。
    • 見つかった場合は、これら2つのエントリの情報を組み合わせて、インスタンスの単一のエントリとしてCombined3番目のHashMapに追加します。次に、これらのエントリの両方をHashMap1と2の両方から削除します。
    • 見つからない場合はCombined、HashMap 1のエントリに基づいてインスタンスを作成し、HashMap2の対応するエントリから取得したはずの利用できない情報をnullに設定します。HashMap1からエントリを削除します。
  • これで、最初のHashMapは空になります。HashMap 2を繰り返して、HashMap1に対応するIDがないエントリを見つけます。上記のように、それらを3番目のHashMapに追加します。
于 2012-11-08T15:58:35.800 に答える
1

java.util.Mapに同じキーで複数の値(つまり、この場合はClass1とClass2)を格納することはできません。必要なのはマルチマップです。Guavaにはこのための実装があります。あなたが探しているのはArrayListMultimapです

于 2012-11-08T15:58:50.003 に答える
0

class1あなたはそれのための新しいものとclass2一緒にマッシュアップである新しいクラスを作る必要がありMapます。何かを入れてone「2」にするたびに、mashup両方のオブジェクトを含む新しいものを作成し、それをに入れthreeます。

于 2012-11-08T15:57:34.350 に答える
0

ArrayListを使用して、1つのキーに複数の値を持つHas​​hMapを作成できます

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

于 2012-11-08T16:01:50.183 に答える
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<>();
于 2012-11-08T16:04:38.770 に答える
-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);
}
 }
于 2012-11-08T16:08:45.203 に答える