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

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

    Details

    • Subcomponent:
    • Resolved In Build:
      b150
    • Verification:
      Not verified

      Description

      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.»

        Attachments

          Activity

            People

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

              Dates

              • Created:
                Updated:
                Resolved: