2011-06-04 6 views
0

こんにちはJavaではJavaのHashSetの検索

、私はプロパティを持つオブジェクトUserのリストが含まれていHashSetの持っている:

  • メール
  • グループ
  • マシン名になりました

私のハッシュセットは以下の値を持っています(上記のオブジェクトのリスト)

Javaコードを使用していること

[email protected] (which has "project" and "test" group) 
[email protected] (which has "hewitt" and "test" groups) 

がどのように見つけることができます:

email   | group | machinename 
---------------------------------------- 
[email protected] | hewitt | AP1 
[email protected] | test | AP1 
[email protected] | test | AP1 
[email protected]  | test | AP1 
[email protected] | project | AP1 
[email protected]  | project | AP1 

今私は、上記の場合と同様、電子メールとマシンが、別のグループ名を持つこれらのレコードを検索する必要がありますか?

+0

私はいつも今.. – Makky

+0

これはhttp://stackoverflow.com/questions/6212325/iterating-hashsetsの重複はありませんか? –

+0

うん!!そこに答えを得ることができなかったので、再度質問しなければならなかった – Makky

答えて

3

これは正確に何をしたいでしょう:

Set<User> users = new HashSet<User>(); 
// ... 

Map<String, List<User>> hits = new HashMap<String, List<User>>(); 

for (User user : users) { 
    String key = user.getMachineName() + user.getEmail(); 
    List<User> list = hits.get(key); 
    if (list == null) { 
     list = new ArrayList<User>(); 
     hits.put(key, list); 
    } 
    list.add(user); 
} 

// Users are now grouped by their "machine name + email" as a single key 

for (Map.Entry<String, List<User>> hit : hits.entrySet()) { 
    if (hit.getValue().size() < 2) continue; 
    System.out.println("These users share the same email and machine name: " 
     + hit.getValue()); // hit.getValue() is an ArrayList<User> 
}