2017-11-23 7 views
1

내가 잘못 가서 그 누구도 날이 설명 할 수있는 작업은 다음 코드는 해시 맵에 대한 형식 인수를 추론 할 수 없습니다 <>

class Test { 

    public static void main(String[] args) { 

     Map<Integer, String> map = new HashMap<>(); 
     map.put(1, "x"); 
     map.put(2, "y"); 
     map.put(3, "x"); 
     map.put(4, "z"); 

    //the following line has error 
     Map<String, ArrayList<Integer>> reverseMap = new java.util.HashMap<>(map.entrySet().stream() 
       .collect(Collectors.groupingBy(Map.Entry::getValue)).values().stream() 
       .collect(Collectors.toMap(item -> item.get(0).getValue(), 
         item -> new ArrayList<>(item.stream().map(Map.Entry::getKey).collect(Collectors.toList())))); 
     System.out.println(reverseMap); 

    } 

} 

에 대한

Cannot infer type arguments for java.util.HashMap<> 

는 무엇입니까? 적절한 가져 오기를 확인하고 java.util.hashmap을 가져 오는 중임을 알았습니다. 아직도 성가신 실수로 저를 괴롭 히고 있습니다.

The Error Persists

답변

5

을 생산 ecj (일식 컴파일러) , 당신은 그것을 해결하려면 더 많은 종류의 정보를 추가 할 수 있습니다 : 나는 ArrayList<Integer>을 추가 한 방법

item -> new ArrayList<Integer>(item.stream().map(Entry::getKey) 

참조.

javac-8 and 9으로 잘 컴파일됩니다.

map.entrySet() 
      .stream() 
      .collect(Collectors.groupingBy(
        Entry::getValue, 
        HashMap::new, 
        Collectors.mapping(Entry::getKey, Collectors.toList()))); 
+0

실제로하게 IntelliJ IDEA는 오류없이 원래의 코드를 컴파일 : 일을 할 수있는 간단한 방법이 같은

그리고 BTW 보인다. –

+1

@SteffenHarbich 왜냐하면 AFAIK는'javac'를 사용하기 때문에 ... – Eugene

+0

나는 [ecj에 대한 버그] (https://bugs.eclipse.org/bugs/show_bug.cgi?id=527742)를 제출했습니다. 수정. –

1

코드가 완료되지 않은, 당신은 일을 시도해보세요 )

누락 :

import java.util.*; 
import java.util.stream.Collectors; 
public class HelloWorld{ 

    public static void main(String []args){ 
     Map<Integer, String> map = new HashMap<>(); 
     map.put(1, "x"); 
     map.put(2, "y"); 
     map.put(3, "x"); 
     map.put(4, "z"); 

     Map<String, ArrayList<Integer>> reverseMap = new java.util.HashMap<>(map.entrySet().stream() 
       .collect(Collectors.groupingBy(Map.Entry::getValue)).values().stream() 
       .collect(Collectors.toMap(item -> item.get(0).getValue(), 
         item -> new ArrayList<>(item.stream().map(Map.Entry::getKey).collect(Collectors.toList()))))); 
     System.out.println(reverseMap); 
    } 
} 

이 버그에서의 출력

{x=[1, 3], y=[2], z=[4]} 
+0

여전히 같은 오류 –