Java 8 Functionality: Lost Type Information Midway

Consider this conversion example (see a detailed description of what should happen here ):

Map<String, Integer> transform(Map<Integer, List<String>> old) {
    old.entrySet().stream()
       .flatMap(entry -> entry.getValue().stream()
                   .map(letter -> letter.toLowerCase())
                   .map(lowerCaseLetter -> new SimpleEntry<String, Integer>(lowerCaseLetter, entry.getKey())))
       // at this point, the type is Stream<Object>, not Stream<SimpleEntry<String,Integer>>
       .collect(Collectors.toMap());

  }

Why is information about a particular type lost here and how can I fix it?

+4
source share
2 answers

Eclipse still has problems with its own compiler (especially in terms of output type) and Java 8 features. In this case, when you encounter such a problem, first try compiling javac. If it compiles, then this is definitely an Eclipse problem.

Eclipse, , , ECJ (Eclipse-), , ( , java- ).

Eclipse (, ), java 8 .

- IntelliJ, javac, , Java 8...

, :

Map<String, Integer> transform(Map<Integer, List<String>> old) {
    return old.entrySet().stream()
              .flatMap(e -> e.getValue().stream().map(s -> new SimpleEntry<>(s.toLowerCase(), e.getKey())))
              .collect(toMap(SimpleEntry::getKey, SimpleEntry::getValue));
}
+2

. . , :

java version "1.8.0_40" Java (TM) SE Runtime Environment (build 1.8.0_40-b25) 64- Java HotSpot TM ( 25.40-b25, )

+1

Source: https://habr.com/ru/post/1583976/


All Articles