460 likes | 773 Views
Collections Chapter 22. Place these programs on the desktop: TestSets (Team5) TestList (Team3) TestMap (Team4) TestCollections (Team2) TestArrays (Team1). Objectives To describe the Java Collections Framework hierarchy
E N D
Collections Chapter 22 Y.Daniel Liang Introduction to Java Programming Sixth Edition and Cay Horstmann Object-Oriented Design & Patterns
Place these programs on the desktop: • TestSets (Team5) • TestList (Team3) • TestMap (Team4) • TestCollections (Team2) • TestArrays (Team1) Y.Daniel Liang Introduction to Java Programming Sixth Edition and Cay Horstmann Object-Oriented Design & Patterns
Objectives • To describe the Java Collections Framework hierarchy • To use the Iterator interface to traverse through all the elements of a collection • To discover the Set interface, and know how and when to use HashSet, LinkedHashSet, or TreeSet to store elements • To compare elements using the Comparator interface • To explore the List interface, and know how and when to use ArrayList or LinkedList to store elements Y.Daniel Liang Introduction to Java Programming Sixth Edition and Cay Horstmann Object-Oriented Design & Patterns
Introduction Java Collection Framework Hierarchy • A collection is a container object (data structure) that represents a group of objects, often referred to as elements of the collection. • The Java Collections Framework supports three major types of collections: • Set, list, map Y.Daniel to Java Programming Sixth Edition and Cay Horstmann Object-Oriented Design & Patterns
Introduction contd.Java Collection Framework Hierarchy • The set, list, and map collections are defined in the interfaces Set, List, and Map. • Collections: The most common collection interface type • Set: an unordered collection that does not allow duplicate elements Y.Daniel Liang Introduction to Java Programming Sixth Edition and Cay Horstmann Object-Oriented Design & Patterns
Introduction contd.Java Collection Framework Hierarchy • SortedSet: a set of whose elements are visited in sorted order • List: stores ordered collection of elements • Map stores a group of objects , each of which is associated with a key. Y.Daniel Liang Introduction to Java Programming Sixth Edition and Cay Horstmann Object-Oriented Design & Patterns
Introduction contd.Java Collection Framework Hierarchy • HashSet: a set implementation that uses hashing to find the set elements • TreeSet: a sorted set implementation that stores the elements in a balanced binary tree • LinkedList and ArrayList: two implementation of the List interface Y.Daniel Liang Introduction to Java Programming Sixth Edition and Cay Horstmann Object-Oriented Design & Patterns
<<Interface>> Collection LinkeditHashSet HashSet <<Interface>> Set <<Interface>> List TreeSet <<Interface>> SortedSet LinkedList ArrayList The relationships of the major interfaces and classes in the Java Collection Frameworks Y.Daniel Liang Introduction to Java Programming Sixth Edition and Cay Horstmann Object-Oriented Design & Patterns
The Collection Interface The Collection interface is the root interface for manipulating a collection of objects. Y.Daniel Liang Introduction to Java Programming Sixth Edition and Cay Horstmann Object-Oriented Design & Patterns
The AbstractCollection Class is a convenience class that provides partial implementation for the Collection interface. It implements all the methods in Collection except the size and iterator methods Y.Daniel Liang Introduction to Java Programming Sixth Edition and Cay Horstmann Object-Oriented Design & Patterns
The Collection interface • The Collection interface provides various query operations: • The size method returns the number of elements in the collection • The contains method checks whether the collection contains all the elements in the specified collection. • The isEmpty method returns true if the collection is empty Y.Daniel Liang Introduction to Java Programming Sixth Edition and Cay Horstmann Object-Oriented Design & Patterns
Note : Some of the methods in the Collection interface cannot be implemented in the concrete subclass in this case, the method would throw java.lang.UnsupportedOperationException , a subclass of RuntimeException. This is a good design that you can use in your project. If a method has no meaning in the subclass, implement it public void someMethod() { throw new UnsupportedOperationException (“Method not supported”); Y.Daniel Liang Introduction to Java Programming Sixth Edition and Cay Horstmann Object-Oriented Design & Patterns
The Set Interface The Set interface extends the Collection interface. It does not introduce new methods or constants, but it stipulates that an instance of Set contains no duplicate elements. The concrete classes that implement Set must ensure that no duplicate elements can be added to the set. That is no two elements e1 and e2 can be in the set such that e1.equals(e2) is true. 13 Y.Daniel Liang Introduction to Java Programming Sixth Edition and Cay Horstmann Object-Oriented Design & Patterns
The Set Interface Hierarchy The three concrete classes of Set are HashSet, LinkedHashSet, and TreeSet. 14 Y.Daniel Liang Introduction to Java Programming Sixth Edition and Cay Horstmann Object-Oriented Design & Patterns
The HashSet Class The HashSet class is a concrete class that implements Set. It can be used to store duplicate-free elements. For efficiency, objects added to a hash set need to implement the hashCode method in a manner that properly disperses the hash code. i.e., the hashCode in the Integer class will return its int value The hashCode in the Character class will returns its Unicode value 15 Y.Daniel Liang Introduction to Java Programming Sixth Edition and Cay Horstmann Object-Oriented Design & Patterns
The hashCode Method and the equals Method • The hashCode() and the equals() method are defined in the Object class as well as in the Collection interface. With the same method signature. • hashCode method and the equals method have default implementation in the Object class. • A class that implements the Collection interface does not have to implement these methods. Y.Daniel Liang Introduction to Java Programming Sixth Edition and Cay Horstmann Object-Oriented Design & Patterns
The hashCode() Method The hash code of two objects must be the same if the two objects are equal. Two unequal objects may have the same hash code. but you must implement the hashCode method to avoid too many such cases. Additionally, it is required that invoking the hasCode method multiple times returns the same integer during one execution of the program Y.Daniel Liang Introduction to Java Programming Sixth Edition and Cay Horstmann Object-Oriented Design & Patterns
Example TestSets This example creates: • A HashSet filled with strings, and uses an iterator to traverse the elements in the list. • 2. A LinkedHashSet – the elements are retrieved in the order they were entered into the set • A TreeSet – the elements are retrieved in a sorted order, they are sorted by using the compareTo method in the Comparable interface TestSet Run Y.Daniel Liang Introduction to Java Programming Sixth Edition and Cay Horstmann Object-Oriented Design & Patterns
The List Interface A set stores non-duplicate elements. To allow duplicate elements to be stored in a collection, you need to use a list. A list can not only store duplicate elements, but can also allow the user to specify where the element is stored. The user can access the element by index. Y.Daniel Liang Introduction to Java Programming Sixth Edition and Cay Horstmann Object-Oriented Design & Patterns
The List Interface, cont. 20 Y.Daniel Liang Introduction to Java Programming Sixth Edition and Cay Horstmann Object-Oriented Design & Patterns
The List Iterator 21 Y.Daniel Liang Introduction to Java Programming Sixth Edition and Cay Horstmann Object-Oriented Design & Patterns
ArrayList and LinkedList The ArrayList class and the LinkedList class are concrete implementations of the List interface. Which of the two classes you use depends on your specific needs. If you need to support random access through an index without inserting or removing elements from any place other than the end, ArrayList offers the most efficient collection. 22 Y.Daniel Liang Introduction to Java Programming Sixth Edition and Cay Horstmann Object-Oriented Design & Patterns
ArrayList and LinkedList If, however, your application requires the insertion or deletion of elements from any place in the list, you should choose LinkedList. A list can grow or shrink dynamically. An array is fixed once it is created. If your application does not require insertion or deletion of elements, the most efficient data structure is the array. 23 Y.Daniel Liang Introduction to Java Programming Sixth Edition and Cay Horstmann Object-Oriented Design & Patterns
LinkedList Y.Daniel Liang Introduction to Java Programming Sixth Edition and Cay Horstmann Object-Oriented Design & Patterns
Example Using ArrayList and LinkedList This example creates an array list filled with numbers, and inserts new elements into the specified location in the list. The example also creates a linked list from the array list, inserts and removes the elements from the list. Finally, the example traverses the list forward and backward. TestList Run Y.Daniel Liang Introduction to Java Programming Sixth Edition and Cay Horstmann Object-Oriented Design & Patterns
The Vector and Stack Classes • Several data structures were supported prior to Java 2. like: • the Vector class • the Stack class. • These classes were redesigned to fit into the Java Collections Framework, but their old-style methods are retained for compatibility. Y.Daniel Liang Introduction to Java Programming Sixth Edition and Cay Horstmann Object-Oriented Design & Patterns
The Vector Class • In Java 2, Vector is the same as ArrayList • Vector contains the synchronized methods for accessing and modifying the vector • None of the new collection data structures introduced so far are synchronized. • If synchronization is required, you can use the synchronized versions of the collection classes. Y.Daniel Liang Introduction to Java Programming Sixth Edition and Cay Horstmann Object-Oriented Design & Patterns
Synchronized is a key word that denotes a block of code subjected to mutual exclusion of an algorithm - only one call to a synchronized method will be executed at a given time. Y.Daniel Liang Introduction to Java Programming Sixth Edition and Cay Horstmann Object-Oriented Design & Patterns
The Vector Class, cont. 29 Y.Daniel Liang Introduction to Java Programming Sixth Edition and Cay Horstmann Object-Oriented Design & Patterns
The Stack Class The Stack class represents a last-in-first-out (LIFO) stack of objects. The elements are accessed only from the top of the stack. You can retrieve, insert, or remove an element from the top of the stack. 30 Y.Daniel Liang Introduction to Java Programming Sixth Edition and Cay Horstmann Object-Oriented Design & Patterns
The Queue Class • The queue class represents a first-in-first-(FIFO) out queue of objects. • The elements are appended at the end of the queue and removed at the beginning of the queue Y.Daniel Liang Introduction to Java Programming Sixth Edition and Cay Horstmann Object-Oriented Design & Patterns
Queues A queue is similar to a stack except that you add items to one end of the queue (the tail) and remove them from the other end of the queue ( the head) of the queue. Queues store items in FIFO. Items are removed in the same order in which they were added. Y.Daniel Liang Introduction to Java Programming Sixth Edition and Cay Horstmann Object-Oriented Design & Patterns
The Priority Queue Class • The priorityqueue class order its elements according to their natural ordering using Comparable Y.Daniel Liang Introduction to Java Programming Sixth Edition and Cay Horstmann Object-Oriented Design & Patterns
The Map Interface • The Map interface maps keys to the elements. • The keys are like indexes. • In List, the indexes are integer. • In Map, the keys can be any objects. Y.Daniel Liang Introduction to Java Programming Sixth Edition and Cay Horstmann Object-Oriented Design & Patterns
Java Collection Framework hierarchy, cont. An instance of Map represents a group of objects, each of which is associated with a key. Y.Daniel Liang Introduction to Java Programming Sixth Edition and Cay Horstmann Object-Oriented Design & Patterns
HashMap and TreeMap • The HashMap and TreeMap classes are two concrete implementations of the Map interface. • The HashMap class is efficient for locating a value, inserting a mapping, and deleting a mapping. • The TreeMap class, implementing SortedMap, is efficient for traversing the keys in a sorted order. Y.Daniel Liang Introduction to Java Programming Sixth Edition and Cay Horstmann Object-Oriented Design & Patterns
LinkedHashMap • The LinkedHashMap extends HashMap with linked list implementation. • The entries in HashMap are not ordered • The entries in LinkedHashMap can be retrieved • in insertion order , or • In access order Y.Daniel Liang Introduction to Java Programming Sixth Edition and Cay Horstmann Object-Oriented Design & Patterns
Example Using HashMap and TreeMap and Linked Map The program first create the HashMap with the students name as a key, and their age as the value The Program create a TreeMap from the HashMap to display the entries in ascending order of the keys Create a LinkedHashMap, add the same entries and displays the entries TestMap Run Y.Daniel Liang Introduction to Java Programming Sixth Edition and Cay Horstmann Object-Oriented Design & Patterns
The Collections Class UML Diagram 39 Y.Daniel Liang Introduction to Java Programming Sixth Edition and Cay Horstmann Object-Oriented Design & Patterns
Example Using the Collections Class This example demonstrates using the methods in the Collections class. The example creates a list, sorts it, and searches for an element. The example wraps the list into a synchronized and read-only list. TestCollections Run Y.Daniel Liang Introduction to Java Programming Sixth Edition and Cay Horstmann Object-Oriented Design & Patterns
The Arrays Class The Arrays class contains various static methods for sorting and searching arrays, for comparing arrays, and for filling array elements. It also contains a method for converting an array to a list. 41 Y.Daniel Liang Introduction to Java Programming Sixth Edition and Cay Horstmann Object-Oriented Design & Patterns
The Arrays Class UMLDiagram 42 Y.Daniel Liang Introduction to Java Programming Sixth Edition and Cay Horstmann Object-Oriented Design & Patterns
Example Using the Arrays Class This example demonstrates using the methods in the Arrays class. The example creates an array of int values, fills part of the array with 50, sorts it, searches for an element, and compares the array with another array. TestArrays Run Y.Daniel Liang Introduction to Java Programming Sixth Edition and Cay Horstmann Object-Oriented Design & Patterns