Why aren't many methods in JCF interfaces running by default in Java 8?

The minimum complete definition of an interface Collectionconsists of only two methods: iterator()and size(), which are abstract in AbstractCollection.

Why are all other methods not executed by default in Java 8? Compatibility should not be a problem, for example, it Iterator.remove()was abstract prior to Java 7, but was performed by default with Java 8.

A subclass of is AbstractCollectionsometimes inconvenient when I want the implementation to Collectionbe a member of another class hierarchy. Isn't this one of the reasons why the default methods in interfaces are really needed in Java?

The same question about Map, Listand other basic interfaces containing the Java Collection Framework.

UPDATE

Paul Sandoz:

As a rule, we only convert existing abstract methods on the interface to non-abstract if there is a good reason to support implementations like Iterator.remove.

These are not new methods in Collection, and there are already implementations in AbstractCollection. The advantage of transforming these abstractions into non-abstract methods, which its not particularly convincing data, most likely inherits from AbstractCollection or provides more efficient implementations.

It would be possible to move all non-abstract methods to AbstractCollection to Collection. If we started with a blank sheet of paper, this is what we could do. (Note that this cannot be done with all non-abstract methods in AbstractList.)

http://mail.openjdk.java.net/pipermail/core-libs-dev/2014-February/025118.html

+3
1

- . . 10 . . . Lambda: Libraries Edition. , Iterable.forEach, Collection.removeIf List.replaceAll.

, List.sort, , Collections.sort.

, , Iterator.remove. remove, UnsupportedOperationException , Iterator. remove . , - . ( ?)

, Collection, , Iterator. , , , , .

Collection.contains(Object). , Iterator . - TreeSet HashSet. List, LinkedList ArrayList, , , . Collection.contains , . .

equals. Collection.equals . , a Set Set, a List List, equals . , a Collection, a List, a Set a List Set.

, Collection.equals instanceof. Lists, AbstractList.equals, Sets, AbstractSet.equals. , Lists, Sets. , , ? .

, , . , , . () - , , . . Set, . Collection, , , . , , , , .

- , .


, . , , Collection , . Collection , , , .

+5

All Articles