も使えますHashMap<String, Set<String>>
キー = 文字列EMP_ID
value = String( CNTCT_CDs
)のセット
コード:
次のようなレコードを表すクラスを作成するだけです
CNTCT_CD EMP_ID
1 2
public class Record {
private String empId;
private String cntctId;
//setters and getters
//constructors
}
次のようにレコードのリストを作成して、すべてのレコードを処理します。セットは、重複する cntct ID を排除するために使用されます。
List<Record> records = new ArrayList<>();
records.add(new Record("1", "2"));
records.add(new Record("2", "3"));
records.add(new Record("3", "2"));
records.add(new Record("5", "2"));
records.add(new Record("4", "3"));
Map<String, Set<String>> map = new HashMap<String, Set<String>>();
//Storing records as per your desired format
for (Record record : records) {
//if its a new key, add key and value(in a Set)
if (!map.keySet().contains(record.getEmpId())) {
Set<String> cntctIds = new HashSet<String>();
cntctIds.add(record.getCntctId());
map.put(record.getEmpId(), cntctIds);
//key already added, just add the value to the added Set
} else {
map.get(record.getEmpId()).add(record.getCntctId());
}
}
Integer
上記の代わりに使用できますString
。それでおしまい!