HashMap<String, List<Integer>> hMap=new HashMap<String, List<Integer>>();
hMap.put("A",new ArrayList<Integer>(Arrays.asList(2,1)));
hMap.put("B",new ArrayList<Integer>(Arrays.asList(1,3,4)));
hMap.put("C",new ArrayList<Integer>(Arrays.asList(5)));
hMap.put("D",new ArrayList<Integer>(Arrays.asList(3)));
hMap.put("E",new ArrayList<Integer>(Arrays.asList(2,4)));
//Original HashMap: { A=[2,1], B=[1,3,4], C=[5], D=[3], E=[2,4] }
//Reversed HashMap: { 1=[A,B], 2=[A,E], 3=[B,D], 4=[B,E], 5=[C] }
HashMap<Integer,List<String>> result = new HashMap<>(hMap.size());
for(Map.Entry<String,List<Integer>> entry : hMap.entrySet()) {
for(Integer n : entry.getValue()) {
if(!result.containsKey(n)) {
result.put(n,new ArrayList<String>());
}
result.get(n).add(entry.getKey());
}
}
System.out.println(hMap);
System.out.println(result);
출력
{D = [3], E가 = [2,4], A = [2,1], B = [1, 3, 4], C = [ 5]}
{1 = [A, B], 2 = [E, A], (3) = [D, B], (4) = [E, B], (5) = [C]}
무엇 코드에 문제가 있습니까? 당신은 우리에게 당신을 위해 그것을 쓰라고 요구하지 않습니다. 맞습니다. – Pshemo
나는 반전을 의미한다고 믿습니다. 되돌리기는 "이전 상태로 돌아갑니다"를 의미합니다. –
새로운'Map>과 루프를 만듭니다. 두번. 끝난. –