240 likes | 313 Views
Advanced Java Programming. CSS446 Spring 2014 Nan Wang. Chapter Goals. Java Collection Framework LinkedList Set Map. Collection. When you need to organize multiple objects in your program, you can place them into a collection.
E N D
Advanced Java Programming CSS446 Spring 2014 Nan Wang
Chapter Goals • Java Collection Framework • LinkedList • Set • Map
Collection • When you need to organize multiple objects in your program, you can place them into a collection. • A collection groups together elements and allows them to be retrieved later. • ArrayList
Java Collections Framework • A hierarchy of interface types and classes for collecting objects. Each interface type is implemented by one or more classes.
List • a list is a collection that remembers the order of its elements.
ArrayList • The ArrayList class implements the List interface. • An ArrayList is simply a class containing an array that is expanded as needed. • If you are not concerned about efficiency, you can use the ArrayList class whenever you need to collect objects. However, several common operations are inefficient with array lists. In particular, if an element is added or removed, the elements at larger positions must be moved.
a a a a b b b b d d c c d e d e e f f e f f Insertion and Deletion of ArrayList element element element element
Set • Set is an unordered collection of unique elements. • Because a set does not track the order of the elements, so the operations of finding, adding and removing are more efficient.
Stack • A stack remembers the order of its elements, but it does not allow you to insert elements in every position. • You can add and remove elements only at the top.
Bus Stop Queue • In a queue, you add items to one end (the tail) and remove them from the other end (the head). • A priority queue is an unordered collection that has an efficient operation for removing the element with the highest priority. (reading assignments) front rear rear rear rear rear
Map • A map keeps associations between key and value objects.
LinkedList • A linked list uses a sequence of nodes. • A node is an object that stores an element and references to the neighboring nodes in the sequence
LinkedList Class • It is a generic class, that you specify the type of the list elements in angle brackets.
List Iterators • List iterators are used to access elements inside a linked list. • you should think of the iterator as pointing between two elements, just as the cursor in a word processor points between two characters
List Iterators • Obtain a list iterator with the listIterator() of the LinkedList class.