Java Collection Framework
- A text document is a collection of paragraphs. Each paragraph is a collection of words. Each word is a collection of characters.
- The computer's file system is a collection of files.
- A browser's bookmark list contains a collection of URLs.
- etc...
- You have already seen one mechanism for storing collections of objects: the array.
- The Java Collections Framework provides several classes that can be used to store collections of data.
Review of Interfaces
- The Java language includes a concept known as interfaces.
- It's a fairly simple concept that we'll need in order to better understand collection types that are part of the Java Collections Framework.
- An interface is basically a promise or contract.
- An interface is a list of method signatures (method name, parameters passed to the method, and the type returned by the method, if anything).
- When a class is declared, it may promise to implement the interface.
- You've already seen interfaces in action...
- An event handler class must implement the
EventHandler<E>interface. - A class declaration like the following promises that the class will implement the
EventHandler<ActionEvent>interface:private class ButtonEventHandler implements EventHandler<ActionEvent> { - The
EventHandler<ActionEvent>interface specifies that the following method must be implemented:public void handle(ActionEvent event) - The compiler makes sure that the
ButtonEventHandlerclass fulfills its promise by refusing to compile unless theButtonEventHandlerclass implements that method.1
- An event handler class must implement the
- In much the same way as we can create a reference to an object, we can create a reference to an interface.
- The reference acts as the gatekeeper for accessing the object to which the reference points. Consider the following code:
String word = new String("nonsense"); CharSequence charSeq = word;- The first line creates a reference of type
Stringand makes it refer to a newly createdStringobject. - The second line creates a reference of type
CharSequenceand makes it refer to the sameStringobject asword. - Since the
Stringclass implements theCharSequenceinterface, the second line is a valid statement. - Both word and charSeq refer to the same object, but we can only access methods known to the
CharSequenceinterface when using the charSeq reference. - The
CharSequenceconsists of four methods: - The following indicates which lines are illegal. All of the legal lines of code function identically regardless of whether the method is called through word or charSeq.
word.toString(); word.endsWith("sense"); word.length(); word.subSequence(1, 3); word.substring(1, 3); charSeq.toString(); charSeq.endsWith("sense"); // ILLEGAL charSeq.length(); charSeq.subSequence(1, 3); charSeq.substring(1, 3); // ILLEGAL
- The first line creates a reference of type
Java Collections Framework
- Because manipulating collections of objects is such a common task, Oracle provides the Java Collections Framework (JCF).
- The framework contains a number of interfaces and classes that provide various ways of storing and interacting with collections of data.
Collection Interface
- The
Collection<E>interface is the root interface for the JCF. - The
Collection<E>represents a group of objects called elements. - Some Collections are ordered while others are unordered
- Some Collections allow duplicate elements while others forbid duplicates.
- The JCF does not actually provide any direct implementations for the
Collection<E>interface. Instead, theCollection<E>interface acts as a base interface for more specific types of interfaces. - Two subinterfaces of the
Collection<E>interface are:List<E>— an ordered collection that allows duplicates. The JCF provides the following implementations of this interface:
Set<E>— an unordered collection that forbids duplicates. The JCF provides the following implementations of this interface:
- We will cover most of these implementations this term.
Extending Interfaces
- An interface can extend another interface.
- A subinterface can declare additional methods and/or redefine methods declared in the superinterface.
- Since an interface doesn't actually provide implementations for methods, redefining a method is a conceptual thing — the definition in the subinterface should describe more specific behavior than the superclass. For example:
Collection.remove(Object)— Removes a single instance of the specified element from this collection, if it is present.List.remove(Object)— Removes the first occurence of the specified element from this list, if it is present.
1
or declares the class as abstract, in which case, if handle() is not implemented, it would be treated as an abstract method.