PowerPoint Presentation
Java Collections
Parag Shah
Adaptive Software Solutions
http://www.adaptivelearningonline.net
http://www.adaptivesoftware.biz
Agenda
Collection classes in Java
New
Legacy
Iterating through collections
Introduction
The need for holding objects
Collection support in Java
Array
Vector
Hashmap
List
Set
Map
Array
Arrays are first class objects
Advantages of using Arrays
Efficient
Type of objects determined at compile time
Can store primitives directly
Disadvantages of using Arrays
The size is fixed
The type of objects it can store is fixed
The Java Collections Library
Basic Collection Interfaces
Collection
List
Set
Map
The basic collection support in Java comes in the form of two top level interfaces and their sub interfaces. At the top are the Collection interface and the Map interface. The Collection interface represents collections of elements while the Map interface represents collections of key-value pairs. The Collection interface has two sub interfaces, list and Set.
Basic Collection Classes
Basic Collection Classes
This is a high level class diagram of the collection library in Java. We can see the two top level interfaces Collection, and Map. Collection has two sub interfaces List and Set. Lists represent ordered Lists of elements, while sets are similar to Lists, with a little distinction that they cannot contain duplicate elements. As we can see in the diagram, there are two type of Lists, ArrayList and LinkedList. There are two types of Set's as well, HashSet, and TreeSet. The Map interface has 3 subclasses; HashMap, TreeMap, and WeakHashMap. We will understand the properties of these collection classes and how to use them in the next few slides.
Collection Classes (contd.)
Classes that work with Collections
Collection Classes Overview
Collection: Stores a collection of objects, such that a rule is applied to the objects
List: Stores objects in a certain order
Implemented By
ArrayList
LinkedList
Set: Stores only unique objects
Implemented By
HashSet
TreeSet
Map: Stores key-value pairs
Implemented By
HashMap
TreeMap
WeakHashMap
Collection Interface
java.util.Collection
Root Interface for the Collection hierarchy in Java
Holds a group of objects which adhere to some rule
Classes implementing this Interface are suggested to have atleast 2 constructors:
A no-arg constructor
A constructor which takes a collection of objects
List
java.util.List
A List is an ordered collection of objects
Implemented by ArrayList, and LinkedList
Users have control on where in the List, new elements should be added
Lists can contain duplicates as determined by the equals() method
Use caution while adding a List element to a List
equals(), and hashCode() may not work properly
ArrayList
See [SimpleArrayList.java]
java.util.ArrayList
Resizable array implementation of the List Interface
Adding elements require O(n) time
Other operations are performed linear time
The capacity of ArrayLists can be increased before adding a large number of elements
ArrayList is NOT synchronized
Inserting elements in the list is inefficient
LinkedList
See [SimpleLinkedList.java]
java.util.LinkedList
Doubly Linked List implementation of the List Interface
Provides special methods to get, remove, and insert elements at the beginning and the end of the list
Enables the usage of a LinkedList as:
Queues
Stacks
Deque
Inefficient for random access
Very efficient for sequential access and insertions in the middle of the list
LinkedList's are not synchronized
Set
java.util.Set
A Set is a Collection that does not contain any duplicate elements
Elements that are put in a Set must override the equals() method to establish uniqueness
A Set cannot contain itself as an element
Caution must be exercised when adding a mutable element to a set
HashSet
See [SimpleHashSet.java]
java.util.HashSet
A Set backed by a HashMap
Can contain nulls
Basic operations take constant time
Iteration order may not always be the same
Used for Sets where lookup time has to be optimized
User defined classes must override the hashCode() method
Operations not synchronized
TreeSet
See [SimpleTreeSet.java]
java.util.TreeSet
A Set backed by a TreeMap
The sorted Set is gauranteed to be in ascending order, sorted by the natural order of the elements or by the comparator provided
User defined classes must implement the Comparable Interface
A TreeSet is not synchronized
Map
java.util.Map
It is an associative array
Used to lookup objects based on keys
Implemented by the HashMap and TreeMap classes
HashMap
See [SimpleHashMap.java]
java.util.HashMap
Implementation is based on a Hashtable
Ideal for fast lookups
Provides constant time performance for inserting and locating pairs
Allows null for keys and values
Methods are not synchronized
TreeMap
See [SimpleTreeMap.java]
java.util.TreeMap
Implemented based on Red-Black trees
The keys appear sorted
Basic operations take log(n) time
The ordering maintained by HashMap must be consistent with equals()
Methods are not synchronized
Iterators
Iterators provide the ability to traverse through a sequence of elements without knowing the underlying data structure of the sequence
Iterator support in Java
Interface Iterator
Allows unidirectional traversal and removal of elements from the underlying collection
Interface ListIterator
Allows bi-directional traversal as well as addition, removal, & substitution of elements in the underlying collection
Using Iterators
Always use iterators and not for loops while iterating through collections
Use Iterator to iterate and remove elements from a collection
ListIterator provides more features
Allows insertion & replacement of elements
Allows bi-directional navigation
See [ListIterationExample.java]
See [MapIteration.java]
Summary
Revise various collection interfaces and their implementations
Discuss pros and cons of choosing the collection type
Discuss legacy collection types
Where to Get More Information
Bruce Eckel's Thinking in Java
Java tutorial
Adaptive Software Solutions 2002 - 2005 http://www.adaptivesoftware.biz
Adaptive Software Solutions 2002 - 2005 http://www.adaptivesoftware.biz