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

Collectors.partitioningBy should specify that false and true entries are always present

XMLWordPrintable

    • b150
    • Not verified

      The Collectors.partitioningBy methods return a Map<Boolean, ?> which is basically an API hack for returning two results instead of a single one. The Map is implemented by an internal class Collectors$Partition which has fields to represent the values for false and true. As such, entries for false and true are always present. The partitioningBy collector implementation relies on this, never checking for the absence of these entries in its accumulator and combiner functions.

      It would be very convenient for applications to be able to rely on both entries always being present. The application could then simply write map.get(false) or map.get(true), instead of having to check for presence explicitly using containsKey(), or having to call getOrDefault(bool, defaultVal).

      If the spec were modified with this requirement, it would reduce the need for applications to write defensive code, while requiring no implementation changes, nor imposing any disadvantages on future evolution.

      Something like «The returned Map always contains mappings for both {@code false} and {@code true} keys.»

            smarks Stuart Marks
            smarks Stuart Marks
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: