A collection groups together elements and allows them to be retrieved later.
Java collections framework: a hierarchy of interface types and classes for collecting objects.
Each interface type is implemented by one or more classes
The Collection interface is at the root
All Collection class implement this interface
So all have a common set of methods
An Overview of the Collections Framework
List interface
A list is a collection that remembers the order of its elements.
Two implementing classes
ArrayList
LinkedList
An Overview of the Collections Framework
Set interface
A set is an unordered collection of unique elements.
Arranges its elements so that finding, adding, and removing elements is more efficient.
Two mechanisms to do this
hash tables
binary search trees
An Overview of the Collections Framework
Stack
Remembers the order of elements
But you can only add and remove at the top
An Overview of the Collections Framework
Queue
Add items to one end (the tail) and remove them from the other end (the head)
A queue of People
A priority queue
an unordered collection
has an efficient operation for removing the element with the highest priority
An Overview of the Collections Framework
Map
Keeps associations between key and value objects.
Every key in the map has an associated value.
The map stores the keys, values, and the associations between them.
Common Methods
Collection<String> coll = new ArrayList<>();
The ArrayList class implements the Collection interface.
coll = new TreeSet<String>();
The TreeSet class also implements the Collection interface.
int n = coll.size();
Gets the size of the collection. n is now 0.
coll.add("Harry"); coll.add("Sally");
Adds elements to the collection.
String s = coll.toString();
Returns a string with all elements in the collection. s is now "[Harry, Sally]"
System.out.println(coll);
Invokes the toString method and prints [Harry, Sally].
coll.remove("Harry"); boolean b = coll.remove("Tom");
Checks whether this collection contains a given element. b is now true.
for (String s : coll) System.out.println(s);
You can use the “for each” loop with any collection. This loop prints the elements on separate lines.
Iterator<> iter = coll.iterator();
You use an iterator for visiting the elements in the collection
Lecture 13 Clicker Question 1
You are taking a social science class and you are assigned lots of books and articles as required reading. Which data structure is best for organizing them?
A set
A queue
A stack
A map
Linked Lists
A data structure used for collecting a sequence of objects:
Allows efficient addition and removal of elements in the middle of the sequence.
A linked list consists of a number of nodes;
Each node has a reference to the next node.
A node is an object that stores an element and references to the neighboring nodes.
Each node in a linked list is connected to the neighboring nodes.
Linked Lists
Adding and removing elements in the middle of a linked list is efficient.
Visiting the elements of a linked list in sequential order is efficient.
Random access is not efficient.
Linked Lists
When inserting or removing a node:
Only the neighboring node references need to be updated
Visiting the elements of a linked list in sequential order is efficient.
Random access is not efficient.
Linked Lists
When to use a linked list:
You are concerned about the efficiency of inserting or removing elements
You rarely need element access in random order
The LinkedList Class of the Java Collections Framework
Generic class
Specify type of elements in angle brackets: LinkedList<Product>
Package: java.util
LinkedList has the methods of the Collection interface.
Some additional LinkedList methods:
List Iterator
Use a list iterator to access elements inside a linked list.
Encapsulates a position anywhere inside the linked list.
Think of an iterator as pointing between two elements:
Analogy: like the cursor in a word processor points between two characters
To get a list iterator, use the listIterator method of the LinkedList class.
The next method returns the element that the iterator is passing.
The return type of the next method matches the list iterator's type parameter.
List Iterator
To traverse all elements in a linked list of strings:
while (iterator.hasNext())
{
String name = iterator.next();
Do something with name
}
To use the “for each” loop:
for (String name : employeeNames)
{
Do something with name
}
Lecture 13 Clicker Question 2
It's your turn. Complete this program so that it prints out every second element of the linked list: the elements at position 0, 2, 4, 6, 8, and so on. Use a list iterator.
What result do you get?
Mary a lamb fleece white snow
had little its was as
An exception occurred
Something else
List Iterator
The nodes of the LinkedList class store two links:
One to the next element
One to the previous one
Called a doubly-linked list
To move the list position backwards, use:
hasPrevious
previous
A List Iterator
The add method adds an object after the iterator.
Then moves the iterator position past the new element.
iterator.add("Juliet");
List Iterator
The remove method:
Removes object that was returned by the last call to next or previous
To remove all names that fulfill a certain condition:
while (iterator.hasNext())
{
String name = iterator.next();
if (condition is fulfilled for name)
iterator.remove();
}
Be careful when calling remove:
It can be called only once after calling next or previous
You cannot call it immediately after a call to add
If you call it improperly, it throws an IllegalStateException
Lecture 13 Clicker Question 3
What is the contents of letters after this sequence of instructions?
List<String> letters = new LinkedList<>();
letters.add("F");
letters.add("R");
letters.add("E");
letters.add("D");
ListIterator<String> iter = letters.listIterator();
iter.next();
iter.next();
iter.remove();
iter.next();
iter.add("X");
[Diana Harry Juliet Nina Tom]
Expected: [Diana Harry Juliet Nina Tom]
Lecture 13 Clicker Question 4
It's your turn again. Complete this program so that the method removes every second element from the linked list: the elements at position 1, 3, 5, 7, 9, and so on (where, as always in Java, 0 is the starting position). Use a list iterator.