3
HashMap<Integer,Integer> hashmapsample= new HashMap<Integer, Integer>();

私は次のような値を持つことができます

(1 , 7)
(2 , 4)
(4 , 5)
(3,  7)

重複する Keys はありません。重複値のみが発生する可能性があります

値が重複している (Key,Value) ペアを選択したい。

Duplicate (Key,Value) を別のハッシュマップとして取得すると、それは素晴らしいことです。どうすればいいですか?

私は出力を期待しています

 (1 , 7)
 (3,  7)
4

4 に答える 4

7

これはどう?

public HashMap getDuplicateValues(HashMap in)
{
   // Clone input HashMap because we're removing stuff from it
   in = (HashMap)in.clone();
   HashMap rval = new HashMap();
   Object[] keys = in.keySet().toArray();

   // iterate through all keys
   for(int x=0;x<keys.length;x++) {
      Object value = in.get(keys[x]);
      in.remove(keys[x]);
      // if value is in input HashMap, store it in duplicate HashMap because it has another value
      if(in.containsValue(value)) {
         rval.put(keys[x],value);
      }
      // if value is in duplicate HashMap, store it also because it HAD another value earlier
      if(rval.containsValue(value)) {
         rval.put(keys[x],value);
      }
   }

   return(rval);
}

このメソッドは、入力 HashMap 内のすべての重複値のキーと値のペアを返します。


テストコード:

  HashMap map = new HashMap();

  map.put("1","2");
  map.put("2","1");
  map.put("3","8");
  map.put("4","4");
  map.put("5","6");
  map.put("6","8");
  map.put("7","3");
  map.put("8","4");
  map.put("9","4");

  HashMap dups = getDuplicateValues(map);

  System.out.println("MAP = "+map);
  System.out.println("DUP = "+dups);

出力:

MAP = {3=8, 2=1, 1=2, 7=3, 6=8, 5=6, 4=4, 9=4, 8=4}
DUP = {3=8, 6=8, 4=4, 9=4, 8=4}
于 2012-07-24T05:43:05.730 に答える
1
    HashMap<Integer, Integer> sample = new HashMap<Integer, Integer>();
    Integer valueForSearch = 7;
    HashMap<Integer, Integer> result = new HashMap<Integer, Integer>();
    for (Entry<Integer, Integer> entry : sample.entrySet()) {
        if (entry.getValue().equals(valueForSearch)) {
            result.put(entry.getKey(), entry.getValue());
        }
    }
于 2012-07-24T05:38:33.657 に答える
1

キーを複製することはできません。箱の束のように考えてください。それぞれに1つのスポットがあります。ボックス1にハンマー、ボックス2にキーボード、ボックス3に懐中電灯、ボックス4に別のハンマーを入れることができます。ただし、ボックス1にハンマーを2つ、またはハンマーとキーボードを入れることはできません。一つの余地があります。すでにいっぱいになっているボックスに別のものを追加しようとすると、自動的にそれが取り出され、古いものは破棄されます。それにアクセスする方法はありません

しかし、私はこの質問を誤解したかもしれないと思います。取得/実行しようとしていることを正確に説明できますか?

さて、これがあなたのHashMapを本質的に逆にするためのいくつかのコードです:

public static void main(String[] args) throws ParseException {
    HashMap start = new HashMap();
    start.put(1, 7);
    start.put(2, 4);
    start.put(4, 5);
    start.put(3, 7);

    HashMap<Object, ArrayList<Object>>  reversed = reverse(start);

    //Some code to print out our results
    Set<Entry<Object, ArrayList<Object>>> set = reversed.entrySet();

    for(Entry entry : set) {
        System.out.println(entry.getKey() + ": " + entry.getValue());
        //if we want here, we can check if the size of the value (The 
    //ArrayList of old keys who has a value of this guy's key) is over 1, if so,
    //there were duplicates of some value (stored to this entry's key)
    }
}
public static HashMap<Object, ArrayList<Object>> reverse(HashMap map) {
    HashMap<Object, ArrayList<Object>> newMap = 
            new HashMap<Object, ArrayList<Object>>();

    Set<Entry> set = map.entrySet();
    for(Entry entry : set) {
        ArrayList list = new ArrayList();
        if(newMap.containsKey(entry.getValue())) {
            list=newMap.get(entry.getValue());
        }
        list.add(entry.getKey());
        newMap.put(entry.getValue(), list);
    }
    return newMap;
}
于 2012-07-24T05:35:19.613 に答える
0

あらすじだけあげると…

  Object array[] = hashmapsample.keySet().toArray();
  for(int i=0;i<array.length();i++)
  {
         if(hashmapsample.containsValue(hashmapsample.get(array[i]){
    //Put that particular value in another hashmap here
    }     
  }

ああ..この投稿で詳細な回答を見つけることができました:D それなら私のものは無視してください..

于 2012-07-24T05:53:32.873 に答える