RFR: JDK-8170945: Collectors$Partition should override more Map methods

Tagir F. Valeev tvaleev at openjdk.org
Wed Apr 5 12:41:17 UTC 2023


On Wed, 5 Apr 2023 09:19:57 GMT, Viktor Klang <duke at openjdk.org> wrote:

> Adds overrides for common Map operations to avoid having to allocate the entrySet for Collectors$Partition maps.

Interesting improvement!

How about `forEach` and `values`? These look quite easy to implement.


    @Override
    public void forEach(BiConsumer<? super Boolean, ? super T> action) {
      Objects.requireNonNull(action);
      action.accept(Boolean.FALSE, forFalse);
      action.accept(Boolean.TRUE, forTrue);
    }

    @Override
    public Collection<T> values() {
      return List.of(forFalse, forTrue);
    }


Theoretically, it would be possible to simplify `entrySet()` and `keySet()` as well:

    @Override
    public Set<Entry<Boolean, T>> entrySet() {
      return Set.of(new SimpleImmutableEntry<>(false, forFalse),
              new SimpleImmutableEntry<>(true, forTrue));
    }

    @Override
    public Set<Boolean> keySet() {
      return Set.of(false, true);
    }


However, this won't guarantee the order of false and true. On one hand, this order was never guaranteed. On the other hand, it might be nice to have. However, soon we may have a facility to explicitly state that the order is preserved, namely, SequencedMap (PR #7387). If we really want to guarantee the order, probably it would be better to implement SequencedMap here (when PR #7387 will be merged)? Though this may require adding more methods.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/13347#issuecomment-1497418841


More information about the core-libs-dev mailing list