Although we can use an array as a container to store a group of elements of the same type [primitives or objects]. The array, however, does not support so-called dynamic allocation - it has a fixed length which cannot be changed once allocated. Furthermore, array is a simple linear structure. Many applications may require more complex data structure such as linked list, stack, hash table, set, or tree.
In Java, dynamically allocated data structures [such as
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 331,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 332,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 333,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 334,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 335,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 336,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 337] are supported in a unified architecture called the Collection Framework, which mandates the common behaviors of all the classes.
A collection, as its name implied, is simply a container object that holds a collection of objects. Each item in a collection is called an element. A framework, by definition, is a set of interfaces that force you to adopt some design practices. A well-designed framework can improve your productivity and provide ease of maintenance.
The collection framework provides a unified interface to store, retrieve and manipulate the elements of a collection, regardless of the underlying actual implementation. This allows the programmers to program at the interface specification, instead of the actual implementation.
The Java Collection Framework package [
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 338] contains:
- A set of interfaces,
- Implementation classes, and
- Algorithms [such as sorting and searching].
Similar Collection Framework is the C++ Standard Template Library [STL].
Prior to JDK 1.2, Java's data structures consist of array,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 333, and
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 337 that were designed in a non-unified way with inconsistent public interfaces. JDK 1.2 introduced the unified collection framework, and retrofits the legacy classes [
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 333 and
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 337] to conform to this unified collection framework.
JDK 5 introduced Generics [which supports passing of types], and many related features [such as auto-boxing/auto-unboxing and for-each loop]. The collection framework is retrofitted to support generics and takes full advantages of these new features.
To understand this chapter, you have to be familiar with:
You also need to be familar with these concepts introduced in JDK 5:
You need to refer to the JDK API specification while reading this chapter. The classes and interfaces for the Collection Framework are kept in package
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 338.
Generic Collection Framework [JDK 5] by Examples
Example 1: List[List of Strings] Implemented by ArrayList
The
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 224 interface is the most commonly used data structure of the Collection Framework, which models a resizable [dynamically-allocated] array supporting numerical index access. The
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 225 class is the most commonly used implementation of
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 226.
The
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 227 indicates that the interfaces are generic in design. When you construct an instance of these generic types, you need to provide the specific type of the objects contained in these collection, e.g.,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 227,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 227. This allows the compiler to perform type-checking when elements are added into the collection at compile-time to ensure type-safety at runtime.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49Dissecting the Program
- Line 1-3 imports the collection framework classes and interfaces reside in the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
8 package. - The class hierarchy of the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
1 is shown above. We observe that1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
1 implements1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
6,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
4 and1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
5 interfaces. The1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
4 and1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
5interfaces define the common behaviors of all the collection implementations. - The interfaces/classes are designed [by the class designer] to take a generics type
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
8. To construct an instance of an1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
1, we need to provide the actual type for generic type1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
8. In this example, we pass the actual type1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
1 for the generic type1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
8. - In line 11, we construct an
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
1 instance, and upcast it to the1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
6 interface. This is possible as the1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
1 is a subtype of1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
6. Remember that a good program operates on the specifications instead of an actual implementation. The Collection Framework provides a set of interfaces so that you can program on these interfaces instead of the actual implementation. - JDK 7 supports type inference on instantiation to simplify the codes:
- Interface
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
4 defines how to add and remove an element into the collection. Interface1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
5 defines a mechanism to iterate or traverse through all the elements of a collection. - The super-interface
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
4 interface defines the common behaviors expected from a1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
4 object [such as getting the size, adding and removing element]. Instead of using the interface1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
4 directly, it is more common to use one of its specialized sub-interfaces:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
6 [an ordered list supporting numerical indexed access],1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
3 [models mathematical set with no duplicate elements] or1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
4 [FIFO, priority queues].
The super-interface1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
4 declares these1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
6 methods, implemented in concrete class1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
1: Line 14-16 adds elements [of the instantiated actual type1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
1] to the1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
4. - There are a few ways of traversing through the elements of a
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
4:- Via an associated
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
1 [Lines 20-24] - Use the new for-each loop introduced in JDK 5 [Line 30]
- Use aggregate operations on Stream introduced in JDK 8. See "Example 4".
- Via an associated
- The super-interface
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
5 defines a mechanism to iterate [or traverse] through all the elements of a1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
4 object via a so-called1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
1 object. The1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
5 interface declares one1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
6 method to retrieve the object associated with the1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
4. - The
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
1 interface declares the following1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
6 methods for traversing through the1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
4. Lines 20-24 retrieve the1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
1 associated with this1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
1, and use a while-loop to iterate through all the elements of this1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
1. Take note that you need to specify the actual type. - Line 30 uses the new for-each loop introduced in JDK 5 to iterate through all the elements of a
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
4. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
6 supports numerical index access via1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
6 method, with index begins from 0, shown in Lines 36-38.- With the use of generics, the compiler checks the type of elements added or retrieved and issue compilation error "incompatible type", as shown in lines 44-47. This is known as compiled-time type-safe.
Example 2: Listwith Auto-Boxing/Auto-Unboxing
Collection can hold only objects, not primitives [such as
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 227,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 228]. JDK 5 introduces auto-boxing/auto-unboxing to simplify the conversion between primitives and their wrapper classes [such as
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 229,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 300, etc.]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19Dissecting the Program
- In this example, we pass actual type
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
1 for the generic type1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
8. In Line 9, we construct an1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
1 and upcast to1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
6. - In lines 10-11, we pass
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
7 value into the1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
6. The1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
7 value is auto-boxed into an1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
1 object, and added into the1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
6. - In line 13, the returns an
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
1 object, which is auto-unboxed to an1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
7 value.
Example 3: SetImplemented by HashSet
This example shows how to create a
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 604 of an user-defined objects.
The
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 173 interface models an unordered mathematical set without duplicate elements.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 335 is the most common implementation of
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 173.
We define a
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1906 class with two private variables
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1907 and
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1908, as follows:Person.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
We shall test a
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 173 is follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22Dissecting the Program
- We define our custom objects called
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
06 in "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
11". - In this example, we pass actual type
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
06 for the generic type1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
8. In Line 7, we construct an instance of1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
5, and upcast to1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
3. - Take note that the elements in a
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
3 are not ordered, as shown in the output of Line 11. - A
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
3 does not duplicate elements, as shown in the output of Line 20. To compare two1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
18 objects, we override the1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
19 and1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
20 methods in "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
11".
Example 4: JDK 8 Collection, Stream and Functional Programming [Preview]
JDK 8 greatly enhances the Collection Framework with the introduction of Stream API to support functional programming.
Person.java: See above
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
This article focuses on JDK 5 Collection Framework. Read "Lambda Expressions, Streams and Functional Programming" and "Collection Framework, Part 2" for details on JDK 8 enhancements.
Pre-JDK 5 vs. JDK 5 Collection Framework
JDK 5 introduces Generics to support parameterized type and retrofitted the Collection Framework. To understand the differences between Pre-JDK 5 and JDK 5 Collection Framework, read "Generics".
The Collection Interfaces
The hierarchy of the interfaces and the commonly-used implementation classes in the Collection Framework is as shown below:
- The base interface is
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
4 [which is a subtype of1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
5], that defines the common behaviors. - We hardly program at the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
4, but one of its specialized subtypes. The commonly-used subtypes of are1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
6 [ordered elements supporting index access],1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
3 [unordered and non-duplicate] and1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
4 [FIFO and priority queues]. - The popular implementation classes for
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
6 are1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
1 and1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
2; for1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
3 are1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
5 and1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
33; for Quene is1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
34. - The
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
1 is used to traverse [or iterate] through each element of a1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
4. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
37 [or associative array], which supports key-value pair, is NOT a subtype of1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
4. The popular implementation class is1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
6.- The
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
40 [like Arrays] is a utility class, which contains1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
41 methods to support algorihtms like searching and sorting.
Iterable/IteratorInterfaces and for-each Loop
There are three ways to traverse through all the elements of a
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 604:
- Via the associated
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
1 object retrieved from the super-type1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
5 - Using the for-each loop [introduced in JDK 5]
- Via the Stream API [introduced in JDK 8] [to be discussed in "Collection Framework, Part 2"]
The
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1945 interface, which takes a generic type
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 227 and read as
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 605 of elements of type
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 608, declares one
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 176 method called
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1950 to retrieve the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 221 object associated with the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 604 object. This
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 221 object can then be used to traverse through all the elements of the associated collection.
All implementations of the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 604 [e.g.,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 331,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 332,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 333] must implement this method, which returns an object that implements
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 221 interface, for traversal of the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 604.The IteratorInterface
The
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 221 interface, declares the following three
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 176 methods:
You can use a while-loop to iterate through the elements with the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 221 as follows:The for-each Loop
JDK 5 introduces a new for-each loop to simplify traversal of a
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 604, as shown in the above code.
The for-loop provides a convenience way to traverse through a collection of elements. But it hides the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 221, hence, you CANNOT remove [via
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1965] or replace the elements.
On the other hand, as the loop variable receives a "cloned" copy of the object reference, the enhanced for-loop can be used to modify "mutable" elements [such as
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1966] via the "cloned" object references, but it cannot modify "immutable" objects [such as
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 171 and primitive wrapper classes] as new references are created.Example: Using Enhanced for-each Loop on Collection of "Mutable" Objects [such as StringBuilder]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17Example: Using Enhanced for-each loop on Collection of "Immutable" Objects [such as String]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
CollectionInterface
The
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 604, which takes a generic type
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 608 and read as
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 604 of element of type
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 608, is the root interface of the Collection Framework. It defines the common behaviors expected of all classes, such as how to add or remove an element, via the following
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 176 methods:
Take note that many of these operations are mutable, i.e., they modify the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 604 object. [In the Functional Programming introduced in JDK 8, operations are immutable and should not modify the source collection.]Collection of Primitives?
A
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 604 can only contain objects, not primitives [such as
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 227 or
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 228]. Primitive values are to be wrapped into objects [via the respective wrapper classes such as
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 301 and
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1978]. JDK 5 introduces auto-boxing and auto-unboxing to simplify the wrapping and unwrapping processes. Read "Auto-Boxing and Auto-Unboxing" section for example.
List, Setand Queue: Specialized Sub-Interfaces of Collection
In practice, we typically program on one of the specialized sub-interfaces of the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 604 interface:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 226,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 173, or
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 174.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
6: models a resizable linear array, which allows numerical indexed access, with index starting from 0.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
6 can contain duplicate elements. Implementations of1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
6 include1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
1,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
2,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
3 and1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
4.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
3: models a mathematical set, where no duplicate elements are allowed. Implementations of1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
3 include1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
5 and1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
93. The sub-interface1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
94 models an ordered and sorted set of elements, implemented by1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
33.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
4: models queues such as First-in-First-out [FIFO] queue and priority queue. It sub-interface1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
97 models queues that can be operated on both ends. Implementations include1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
34,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
99 and1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
2.
The details of these sub-interfaces and implementations will be covered later in the implementation section.
MapInterface
In Java, a
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1937 [also known as associative array] contains a collection of key-value pairs. It is similar to
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 226 and array. But instead of an numerical key 0, 1, 2, ..., a
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1937's key could be any arbitrary objects.
The interface
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1937, which takes two generic types
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3305 and
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3306 [read as
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1937 of key type
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3305 and value type
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3306], is used as a collection of "key-value pairs". No duplicate key is allowed. Implementations include
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 336,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 337 and
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3312. Its sub-interface
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3313 models an ordered and sorted map, based on its key, implemented in
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3314.
Take note that
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1937 is not a sub-interface of
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 604, as it involves a pair of objects for each element. The details will be covered later.
List Interfaces, Implementations and Algorithms
A
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 226 models a resizable linear array, which supports numerical indexed access, with index starts from 0. Elements in a list can be retrieved and inserted at a specific index position based on an
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 227 index. It can contain duplicate elements. It can contain
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3319 elements. You can search a list, iterate through its elements, and perform operations on a selected range of values in the list.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 226 is the most commonly-used data structure, as a resizable array.
The
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 226 interface declares the following
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 176 methods, in additional to its super-interfaces. Since
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 226 has a positional index. Operation such as
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3324,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3325,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3326 can be applied to an element at a specified index position.
The
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 176 superclass
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3328 provides implementations to many of the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 176 methods declared in
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 226 and its supertypes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 604 and
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 605. However, some methods such as
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3333 remains
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 176. These methods are implemented by the concrete subclasses such as
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 331 and
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 333.
[TODO] Example
ArrayListand Vector: Implementation Classes for List
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 331 is the best all-around implementation of the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 226 interface. Many useful methods are already implemented in
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3328 but overridden for efficiency in
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 331 [e.g.,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3324,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3325,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3326 etc.].
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 333 is a legacy class [since JDK 1.0], which is retrofitted to conform to the Collection Framework [in JDK 1.2].
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 333 is a synchronized thread-safe implementation of the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 226 interface. It also contains additional legacy methods [e.g.,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3347,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3348,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3349,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3350,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3351,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3352,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3353]. There is no reason to use these legacy methods - other than to maintain backward compatibility.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 331 is not synchronized. The integrity of
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 331 instances is not guaranteed under multithreading. Instead, it is the programmer's responsibility to ensure synchronization. On the other hand,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 333 is synchronized internally. Read "Synchronized Collection" if you are dealing with multi-threads.
Java Performance Tuning Tip: Synchronization involves overheads. Hence, if synchronization is not an issue, you should use
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 331 instead of
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 333 for better performance.
[TODO] Example
Stack: Implementation Class for List
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 334 is a last-in-first-out queue [LIFO] of elements.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 334 extends
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 333, which is a synchronized resizable array, with five additional methods:
[TODO] Example
LinkedList: Implementation Class for List
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 332 is a double-linked list implementation of the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 226 interface, which is efficient for insertion and deletion of elements, in the expense of more complex structure.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 332 also implements
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 174 and
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1997 interfaces, and can be processed from both ends of the queue. It can serve as FIFO or LIFO queue.
[TODO] Example
Converting a List to an Array: toArray[]
The super-interface
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 604 defines a method called
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3368 to create a fixed-length array based on this list. The returned array is free for modification.Example - List to array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
Using an Array as a List: Arrays.asList[]
The utility class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3369 provides a
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1941 method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3371 to convert an array into a
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 226. However, change to the list write-thru the array and vice versa. Take note that the name of the method is
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3373 and not
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3374.Example - Array as List
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
Comparison of ArrayList, Vector, LinkedList and Stack
[TODO] Example on benchmarking
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 331,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 333,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 332, and
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 334
List's Algorithms
The utility class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3379 provides many useful algorithms for collection. Some work for any
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 604s; while many work for
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 226s [with numerical index] only.
[TODO] example
Sub-List [Range-View] OperationsThe
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 226 supports range-view operation via
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3383 as follows. The returned
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 226 is backup by the given
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 226, so change in the returned
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 226 are reflected in the original
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 226.
The Utility class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1940 supports these sub-list operations:
For example,
Searching, Sorting and OrderingThe utility class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1940 provides these
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1941 methods for searching, sorting and ordering [max, min] of
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 226:
Each of these algorithms has two versions:
- Requires a
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
92 object with a1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
93 method to determine the order of the elements. - Requires a
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
6 which implement1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
95 interface, with a method1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
96 to determine the order of elements.
We shall elaborate in the next section.
List Ordering/Searching/Sorting with Comparable/Comparator
Ordering is needed in these two situations:
- To sort a
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
4 or an array [using the1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
98 or1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
99 methods], an ordering specification is needed. - Some
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
4s, in particular,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
94 [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
33] and1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
03 [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
14], are ordered. That is, the objects are stored according to a specified order.
There are two ways to specify the ordering of objects:
- Create a special
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
05 object, with a method1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
93 to specify the ordering of comparing two objects. - Make the objects implement the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
07 interface, and override the1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
96 method to specify the ordering of comparing two objects.
ComparableInterface
A
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 2207 interface specifies how two objects are to be compared for ordering. It defines one
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 176 method:
This ordering is referred to as the class's natural ordering.
It is strongly recommended that
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3396 be consistent with
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 2212 and
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 2213 [inherited from
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 2214]:
- If
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
96 returns a zero,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
12 should return1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
17. - If
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
12 returns1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
17,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
13 shall produce the same1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
7.
All the eight primitive wrapper classes [
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 2222,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 2223,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 301,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 2225,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 2226,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1978,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 2228 and
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 2229] implement
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3395 interface, with the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3396 uses the numerical order.
The
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 171 class also implements
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3395 interface, which compares two strings lexicographically based on the Unicode value of each character in the strings. The uppercase letters are smaller than the lowercase counterparts.Example 1: Searching/Sorting String and Primitive Wrapper Types, which implement Comparable
The utility class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3369 and
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3379 provide many
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1941 method for the various algorithms such as sorting and searching. In this example, we use the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3399 and
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3398 methods to sort an array of
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 171s and a
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 226 of
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 301s, based on their
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3395's
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3396 method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30Example 2: Custom Implementation of Comparable
Let's create a subclass of
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1906 [see "
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1911" above], called ComparablePerson which implements
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3395 interface, and try out the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3398 and
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 2248 methods.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 190
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 191
ComparatorInterface
Besides the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 2207 for the natural ordering, you can pass a
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 2205 object into the sorting methods [
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3398 or
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3399] to provide precise control over the ordering. The
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3392 will override the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3395, if available.
The
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3392 interface declares one
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 176 method [known as Functional Interface in JDK 8]:
Take note that you need to construct an instance of
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3392, and invoke
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3393 to compare
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 2259 and
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 2260. [In the earlier
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3395, the method is called
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3396 and it takes only one argument, i.e., this object compare to the given object.]Example 3: Using Customized Comparatorfor String and Integer
In this example, instead of using the natural
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3395, we define our customized
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3392 for
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 171s and
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 301s. We can do this via any of the following ways:
- A named inner class
- An anonymous inner class
- Lambda Expressions [JDK 8]
Try: Modify the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3392 to sort in A, a ,B, b, C, c ... [uppercase letter before the lowercase].
Notes: You can use Lambda Expressions [JDK 8] to shorten this code, as follows:
More example at "Example: Comparator Lambda".
Example 4: Using Customized Comparatorfor Person ObjectsPerson.java: See above
SetInterfaces, Implementations and Algorithms
The
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 173 interface models a mathematical set, where no duplicate elements are allowed [e.g., playing cards]. It may contain a single
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3319 element.
The
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 173 interface declares the following abstract methods. The insertion, deletion and inspection methods returns
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 2271 if the operation fails, instead of throwing an exception.
The implementations of
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 173 interface include:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
5: Stores the elements in a hash table [hashed via the1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
74].1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
5 is the best all-round implementation for1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
3.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
93: Stores the elements in a linked-list hash table for better efficiency in insertion and deletion. The element are hashed via the1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
13 and arranged in the linked list according to the insertion-order.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
33: Also implements sub-interfaces1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
80 and1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
94. Stores the elements in a red-black tree data structure, which are sorted and navigable. Efficient in search, add and remove operations [in1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
82].
HashSetBy Example
Let's write a
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 2283 class, and create a
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 173 of
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 2283 objects.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 192
We need to provide an
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 2212 method, so that the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 173 implementation can test for equality and duplication. In this example, we choose the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 2288 as the distinguishing feature. We override
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 2212 to return
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 2217 if two books have the same
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 2288. We also override the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 2213 to be consistent with
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 2212.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 193
- A
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
3 cannot hold duplicate element. The elements are check for duplication via the overridden1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
95. - A
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
3 can hold a1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
19 value as its element [but no duplicate too]. - The
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
98 and1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
99 perform set union and set intersection operations, respectively.
Take note that the arrangement of the elements is arbitrary, and does not correspond to the order of
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3324.
LinkedHashSetBy Example
Unlike
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 335,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1993 builds a link-list over the hash table for better efficiency in insertion and deletion [in the expense of more complex structure]. It maintains its elements in the insertion-order [i.e., order of
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3324].
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 191
The output clearly shows that the set is ordered according to the order of
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3324.
SortedSetand NavigableSetInterfaces
Elements in a
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1994 are sorted during
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3324, either using the natural ordering in the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3395, or given a
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3392 object. Read "Ordering, Sorting and Searching" for details on
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3395 and
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3392.
The
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 2280 is a sub-interface of
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1994, which declares these additional navigation methods:
TreeSetby Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1933 is an implementation to
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 2280 and
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1994.Example - TreeSet with Comparable
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 195
This
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 6016 class implements
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3395, in order to be used in
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1933. It overrides
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3396 to compare the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1907 in a case insensitive manner. It also overrides
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 2212 and
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 2213, so as they are consistent with the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3396.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 196
Observe that the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 6016 objects are sorted and stored in the order depicted by the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3395 during
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3324 operation.Example - TreeSet with Comparator
Let's rewrite the previous example to use a
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3392 object instead of
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3395. We shall set the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3392 to order in descending order of
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1907 for illustration.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 197
The
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 6031 class does not implement
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3392. You cannot
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3324 a
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 6031 object into a
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 6035 as in the above example. Instead, we define a
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3392 class, and use an instance of
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3392 to construct a
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1933.
The
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3392 orders the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 6031 objects in descending
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1907 and case insensitive.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 195
In the test program, we construct a
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1933 with the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 6043. We also tried the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 6044 method to obtain a new
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 173 in reverse order.
QueueInterfaces, Implementations and Algorithms
A queue is a collection whose elements are added and removed in a specific order, typically in a first-in-first-out [FIFO] manner. A deque [pronounced "deck"] is a double-ended queue that elements can be inserted and removed at both ends [head and tail] of the queue.
Besides basic
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 604 operations,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 174 provide additional insertion, extraction, and inspection operations. Each of these methods exists in two forms: one throws an exception if the operation fails, the other returns a special value [either
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3319 or
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 2271, depending on the operations]. The latter form of the insert operation is designed specifically for use with capacity-restricted
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 174 implementations
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1997 declares additional methods to operate on both ends [head and tail] of the queue.
A
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1997 can be used as FIFO queue [via methods
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 6053,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3325,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 6055,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 6056,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 6057,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 6058] or LIFO queue [via methods
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 6059,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 6060,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 6058].
The
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 174 and
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1997 implementations include:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
34: A queue where the elements are ordered based on an ordering you specify, instead of FIFO.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
99: A queue and deque implemented as a dynamic array, similar to1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
1.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
2: The1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
2 also implements the1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
4 and1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
97 interfaces, in additional to1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
6 interface, providing a queue or deque that is implemented as a double- linked list data structure.
The basic operations of
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 174 include adding an element, polling the queue to retrieve the next element, or peeking at the queue to see if there is an element available in the queue. The
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1997 operations are similar except element can be added, polled, or peeked at both ends of the deque.
[TODO] Example
MapInterfaces, Implementations and Algorithms
A map is a collection of key-value pairs [e.g., name-address, name-phone, isbn-title, word-count]. Each key maps to one and only value. Duplicate keys are not allowed, but duplicate values are allowed. Maps are similar to linear arrays, except that an array uses an integer key to index and access its elements; whereas a map uses any arbitrary key [such as
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 171s or any objects].
The implementations of
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1937 interface include:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
6: Hash table implementation of the1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
37 interface. The best all-around implementation. Methods in1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
6 is not synchronized.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
14: Red-black tree implementation of the1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
13 interface.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
12: Hash table with link-list to facilitate insertion and deletion.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
7: Retrofitted legacy [JDK 1.0] implementations. A synchronized hash table implementation of the1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
37 interface that does not allow1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
19 key or values, with legacy methods.
The
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1937 interface declares the following
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 176 methods for basic operations:Collection Views
The
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 6087 provides these method to allow a map to be viewed as a
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 604:
The nested class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 6089 contains these methods:
The
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1937 does not have
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 226-like iterator. The
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 604 views provides the means to iterate over a map.Example 1: Iterating through a Map using .entrySet[] and .keySet[]Example 2: Word Count using HashMap
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 196
Utilities Class java.util.Arrays
The Collection Framework provides two utility classes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3369 and
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3379, which provide some useful algorithms, such as sort, shuffle, reverse, and search, on arrays and Collections.
Array is a reference type in Java. It can hold primitives, as well as objects. On the other hand, a
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 604 holds only object.
Array Sorting/Searching: Arrays.sort[] and Arrays.binarySearch[]
Sorting of Primitive and Object ArraysThere is a pair of
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 6096 methods for each of the primitive types [except
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 6097] and
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 6098.
For example, for
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 6099:
Similar
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 6096 methods are available for primitive arrays
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1701,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1702,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1703,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1704,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1705,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1706 [except
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1707], and
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1708. For
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1708, the objects must implement
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3395 interface so that the ordering can be determined via the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3396 method.Soriting for Generic Arrays
A pair of methods is also defined for generic, to be sorted based on the given
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3392 [instead of
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3395].