The Java Collections Framework: Praveen Raj R (Mark Education Academy)
The Java Collections Framework: Praveen Raj R (Mark Education Academy)
THE JAVA
COLLECTIONS Praveen Raj R
(Mark Education Academy)
FRAMEWORK
Contents
1. An Overview of the Java Collections Framework
2. Linked Lists
3. Sets
4. Maps
5. Stacks Queues and Priority Queues
6. Stack and Queue Applications
In this chapter, you will learn about
the Java collection framework, a
hierarchy of interface types and
classes for collecting objects Page
2
Java Collections Framework
Page
4
• Ordered Lists
Lists and Sets
• ArrayList
• Stores a list of items in a dynamically sized array
• LinkedList
• Allows speedy insertion and removal of items from the list
• HashSet
• Uses hash tables to speed up finding, adding, and removing elements
• TreeSet
• Uses a binary tree to speed up finding, adding, and removing elements
• Queue
• Add items to one end (the tail)
• Remove them from the other end (the head)
• Example: A line of people waiting for a bank teller Page
7
Maps
• A map stores keys, values, and the associations between them
• Example:
• Barcode keys and books
• Keys
• Provides an easy way to represent an object (such as a numeric bar code,
or a Student Identification Number)
• Values
• The actual object that is associated with the key Page
8
The Collection Interface (1)
Page
9
The Collection Interface (2)
Page
10
15.2 Linked Lists
• Linked lists use references to maintain an ordered lists of
‘nodes’
• The ‘head’ of the list references the first node
• Each node has a value and a reference to the next node
Page
11
Linked Lists Operations
• Efficient Operations
• Insertion of a node
• Find the elements it goes between
• Remap the references
• Removal of a node
• Find the element to remove
• Remap neighbor’s references
• Inefficient Operations
Each instance variable is declared just
• Random access
like other variables we have used. Page
12
LinkedList: Important Methods
Page
13
Generic Linked Lists
• The Collection Framework uses Generics
• Each list is declared with a type field in < > angle brackets
LinkedList<String> employeeNames = . . .;
LinkedList<String>
LinkedList<Employee>
Page
14
When traversing a LinkedList, use a ListIterator
Keeps track of where you are in the list.
List Iterators
Use an iterator to:
LinkedList<String> employeeNames = . . .;
ListIterator<String> iter = employeeNames.listIterator()
Page
15
• Think of an iterator as pointing between two elements (think
Using Iterators
of cursor in word processor)
ListIterator<String> iter = myList.listIterator()
iterator.next();
iterator.add(“J”);
Page
17
• Iterators are oftenIterators
used in while and “for-each” loops
and Loops
hasNext returns true if there is a next element
next returns a reference to the value of the next element
while (iterator.hasNext())
{
String name = iterator.next();
// Do something with name
} for (String name : employeeNames)
{
// Do something with name
}
• Removes the object that was returned with the last call to next or
previous
• It can be called only once after next or previous
• You cannot call it immediately after a call to add.(why?)
while (iterator.hasNext())
{
String name = iterator.next();
If you call the remove if (condition is true for name)
method improperly, it throws {
an IllegalStateException. iterator.remove();
Page
} 19
}
ListDemo.java (1)
• Illustrates adding, removing and printing a list
Page
20
ListDemo.java (2)
Page
21
Sets
• A set is an unordered collection
• The collection does not keep track of the order in which elements
have been added
• Therefore, it can carry out its operations more efficiently than an ordered
collection
100
101
Page
102 24
hashCode
• The method is called hashCode
• If multiple elements have the same hash code (so-called clash), they are stored in a
LinkedList
• The elements must also have an equals method for checking whether an element
equals another like:
Page
29
Working With Sets (2)
Page
30
SpellCheck.java (1)
Page
31
SpellCheck.java (2)
Page
32
Programming Tip 15.1
• Use Interface References to Manipulate Data Structures
• It is considered good style to store a reference to a HashSet or TreeSet
in a variable of type Set.
Set<String> words = new HashSet<String>();
• This way, you have to change only one line if you decide to use a TreeSet
instead.
Page
33
Programming Tip 15.1 (continued)
• Unfortunately the same is not true of the ArrayList, LinkedList and List
classes
• The get and set methods for random access are very inefficient (why)
public static void removeLongWords(Collection<String> words)
• Also, if a method can operate on arbitrary collections, use the Collection interface
type for the parameter:
Page
34
Maps
• A map allows you to associate elements from a key set with
elements from a value collection.
• The HashMap and TreeMap classes both implement the Map interface.
Page
35
Key Value Maps Key Value
Page
37
Key Value Pairs in Maps
• Each key is associated with a value
Page
39
MapDemo.java
Page
40