What is the name of the interface used to represent collection that maintain non unique elements in order?
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. Show
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:
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 ExamplesExample 1: List(List of Strings) Implemented by ArrayListThe 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 49 Dissecting the Program
Example 2: Listwith Auto-Boxing/Auto-UnboxingCollection 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 19 Dissecting the Program
Example 3: SetImplemented by HashSetThis 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.java1 2 3 4 5 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 22 Dissecting the Program
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 FrameworkJDK 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 InterfacesThe hierarchy of the interfaces and the commonly-used implementation classes in the Collection Framework is as shown below:
Iterable/IteratorInterfaces and for-each LoopThere 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:
The IterableInterfaceThe 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 IteratorInterfaceThe 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 LoopJDK 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. for-each Loop vs. IteratorThe 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 17 Example: 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 CollectionInterfaceThe 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 CollectionIn 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.
The details of these sub-interfaces and implementations will be covered later in the implementation section. MapInterfaceIn 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 AlgorithmsA 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 List1 2 3 4 5 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 List1 2 3 4 5 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 List1 2 3 4 5 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 array1 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 List1 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 AlgorithmsThe 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. Mutating Operators[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:
We shall elaborate in the next section. List Ordering/Searching/Sorting with Comparable/ComparatorOrdering is needed in these two situations:
There are two ways to specify the ordering of objects:
ComparableInterfaceA 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):
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 ComparableThe 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 30 Example 2: Custom Implementation of ComparableLet'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 ComparatorInterfaceBesides 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 IntegerIn 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:
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 AlgorithmsThe 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:
HashSetBy ExampleLet'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
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 ExampleUnlike 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 NavigableSetInterfacesElements 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 Example1 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 Comparable1 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 ComparatorLet'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 AlgorithmsA 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:
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 AlgorithmsA 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:
Basic OperationsThe 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 ViewsThe 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 HashMap1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 196 Utilities Class java.util.ArraysThe 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 ArraysA 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). What is the name of the collection interface used to represent elements in a sequence?A List is an ordered Collection (sometimes called a sequence). Lists may contain duplicate elements.
Which collection interface is responsible for maintaining unique elements?Explanation: Set interface extends collection interface to handle sets, which must contain unique elements.
Which interface is used for collection?The Collection interface is used to pass around collections of objects where maximum generality is desired. For example, by convention all general-purpose collection implementations have a constructor that takes a Collection argument.
Which collection would you use to keep sorted collection of unique objects?If you just want to sort a list, use any kind of List and use Collections. sort(). If you want to make sure elements in the list are unique and always sorted, use a SortedSet.
|