Contents
- 1 What is the purpose of the Java Collection interface?
- 2 What is an optional operation in Java?
- 3 Which interface is implemented by all the collections in Java?
- 4 What do optional methods in the collection interface mean?
- 5 Are there optional methods in the Java interface?
- 6 Are there any special purpose implementations of Java collections?
What is the purpose of the Java Collection interface?
A Collection represents a group of objects known as its elements. The Collection interface is used to pass around collections of objects where maximum generality is desired.
What is an optional operation in Java?
That an interface method is specified as optional in the JavaDoc means that classes implementing this interface does not necessarily have to implement that method. Instead, they could for example, throw an exception.
Which interface is implemented by all the collections in Java?
Iterable interface
The Iterable interface is the root interface for all the collection classes. The Collection interface extends the Iterable interface and therefore all the subclasses of Collection interface also implement the Iterable interface.
Why do we need default method in interface?
The reason we have default methods in interfaces is to allow the developers to add new methods to the interfaces without affecting the classes that implements these interfaces.
What is the hierarchy of collection framework in Java?
The hierarchy of the entire collection framework consists of four core interfaces such as Collection, List, Set, Map, and two specialized interfaces named SortedSet and SortedMap for sorting. All the interfaces and classes for the collection framework are located in java.
What do optional methods in the collection interface mean?
The optional methods in the Collection interface mean that the implementation of the method is allowed to throw an exception, but it has to be implemented anyway. As specified in the docs: Some collection implementations have restrictions on the elements that they may contain.
Are there optional methods in the Java interface?
Aside: The approach used by Java’s Collections API is somewhat controversial (probably less now than when it was first introduced, however). In a perfect world, interfaces would not have optional operations, and fine grained interfaces would instead be used.
Are there any special purpose implementations of Java collections?
The Java Collections Framework also provides several special-purpose implementations for situations that require nonstandard performance, usage restrictions, or other unusual behavior. The java.util.concurrent package contains several collections implementations, which are thread-safe but not governed by a single exclusion lock.
How does the collection interface work in Java?
Collection Interface Array Operations. The toArray methods are provided as a bridge between collections and older APIs that expect arrays on input. The array operations allow the contents of a Collection to be translated into an array. The simple form with no arguments creates a new array of Object.