Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8195084

Stream.collect is unable to infer type after custom flatMap

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 9.0.1
    • core-libs

      FULL PRODUCT VERSION :
      java version "9.0.1"
      Java(TM) SE Runtime Environment (build 9.0.1+11)
      Java HotSpot(TM) 64-Bit Server VM (build 9.0.1+11, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Windows 10

      A DESCRIPTION OF THE PROBLEM :
      Map<String, List<Integer>> map = new HashMap<>();
      map.put("a", Arrays.asList(5, 3, 7));
      map.put("b", Arrays.asList(1, 5));
      map.put("c", Arrays.asList(45, 2));
      map.put("d", Arrays.asList(4, 6, 2, 34));

      Map<String, Integer> test = map.entrySet()
              .stream()
              .flatMap(stringListEntry -> stringListEntry.getValue().stream().map(number -> new Pair<>(stringListEntry.getKey(), number)))
               .collect(groupingBy(Pair::getKey, maxBy(comparingInt(Pair::getValue))));

      The collect in the above example is unable to infer the type Pair<String Integer>. If I use a .map or other operations it is able to infer the correct type but in the collect method only "Object" is inferred.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Just use this code.
      It should work, but doesn't.

      Map<String, List<Integer>> map = new HashMap<>();
      map.put("a", Arrays.asList(5, 3, 7));
      map.put("b", Arrays.asList(1, 5));
      map.put("c", Arrays.asList(45, 2));
      map.put("d", Arrays.asList(4, 6, 2, 34));

      Map<String, Integer> test = map.entrySet()
              .stream()
              .flatMap(stringListEntry -> stringListEntry.getValue().stream().map(number -> new Pair<>(stringListEntry.getKey(), number)))
               .collect(groupingBy(Pair::getKey, maxBy(comparingInt(Pair::getValue))));

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I expect java to infer the type Pair<String, Integer> in the collect method

      The output should be a new Map with the Strings of the first map but only with the max value of each list as map value.
      ACTUAL -
      Java infers the type to "Object" and not as Pair<String, Integer>

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Error:(81, 25) java: incompatible types: inference variable R has incompatible bounds
          equality constraints: java.util.Map<java.lang.String,java.util.Optional<javafx.util.Pair<java.lang.String,java.lang.Integer>>>
          upper bounds: java.util.Map<java.lang.String,java.lang.Integer>,java.lang.Object

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      Map<String, List<Integer>> map = new HashMap<>();
      map.put("a", Arrays.asList(5, 3, 7));
      map.put("b", Arrays.asList(1, 5));
      map.put("c", Arrays.asList(45, 2));
      map.put("d", Arrays.asList(4, 6, 2, 34));

      Map<String, Integer> test = map.entrySet()
              .stream()
              .flatMap(stringListEntry -> stringListEntry.getValue().stream().map(number -> new Pair<>(stringListEntry.getKey(), number)))
               .collect(groupingBy(Pair::getKey, maxBy(comparingInt(Pair::getValue))));
      ---------- END SOURCE ----------

            psonal Pallavi Sonal (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: