Java Collection Framework hierarchy

A collection is a container object that holds a group of objects, often referred to as data or elements.

The Java Collections Framework supports two types of collections:

  • For storing a collection, simply called collection
  • For storing key/value pairs, called map
    • map is not in the collection

The Collection interface is for manipulating a collection of objects.

Iterators

Iterator is a classic design pattern for walking through a data structure without having to expose the details of how data is stored in the data structure.

Each collection is Iterable. You can obtain its Iterator object to traverse all the elements in the collection.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.*;
public class TestIterator {
public static void main(String[] args) {
ArrayList < String > collection = new ArrayList < > ();
collection.add("New York");
collection.add("Atlanta");
collection.add("Dallas");
collection.add("Madison");

//obtain its Iterator object to traverse all the elements
Iterator < String > iterator = collection.iterator();
while (iterator.hasNext()) {
System.out.print(iterator.next().toUpperCase() + " ");
}
System.out.println();
//NEW YORK ATLANTA DALLAS MADISON
}
}

List Iterator

The ListIterator interface extends the Iterator interface to add bidirectional traversal of the list.

In the List interface there are 2 concrete implementations. ArrayList and LinkedList.

Showcasing the bidirectional traversal of the list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.util.*;
public class TestArrayAndLinkedList {
public static void main(String[] args) {
List < Integer > arrayList = new ArrayList < > ();
arrayList.add(1); // 1 is autoboxed to an Integer object
arrayList.add(2);
arrayList.add(3);
arrayList.add(1);
arrayList.add(4);
arrayList.add(0, 10);
arrayList.add(3, 30);
System.out.println("A list of integers in the array list:");
System.out.println(arrayList);
LinkedList < Object > linkedList = new LinkedList < > (arrayList);
linkedList.add(1, "red");
linkedList.removeLast();
linkedList.addFirst("green");

System.out.println("Display the linked list forward:");

ListIterator < Object > listIterator = linkedList.listIterator();
while (listIterator.hasNext()) {
System.out.print(listIterator.next() + " ");
}

System.out.println();
System.out.println("Display the linked list backward:");

//returns the iterator for the elements from the last index
listIterator = linkedList.listIterator(linkedList.size());
while (listIterator.hasPrevious()) {
System.out.print(listIterator.previous() + " ");
}

}
}

Comparing Objects

Comparable Interface Abstract Classes and Interfaces

The Comparable Interface

1
2
3
4
5
6
7
8
9
10
// This interface is defined in 
// java.lang package
package java.lang;

/*
The compareTo method determines the order of this object with the specified object o and returns a negative integer, zero, or a positive interger if this object is less than, equal to, or greater than o
*/
public interface Comparable<E> {
public int compareTo(E o);
}

The classes Byte, Short, Integer, Long, Float, Double, Character, BigInteger, BigDecimal, Calendar, String, and Date all implement the Comparable interface.

Note the classes Number does not implement the Comparable interface.

Generic sort Method

The java.util.Arrays.sort(array) method requires that the elements in an array are instances of Comparable<E>.

Since all Comparable objects have the compareTo method, the java.util.Arrays.sort(Object[]) method in the Java API uses the compareTo method to compare and sorts the objects in an array.

Comparator Interface From Lists, Stacks, Queues, and Priority Queues

What If the objects are not instances of Comparable?

  • You can define a comparator to compare these elements.
  • The Comparator interface has the compare method for comparing two objects.
1
2
3
public interface Comparator<T> {
public int compare(T element1, T element2);
}

Lambda Expression in Comparator

Comparator is a functional interface. It has just a single method, compare. We can use lambda expression to simplify coding.

java.util.Arrays.sort(cities, (s1, s2) −> {return s1.length() − s2.length();});

Or simply
java.util.Arrays.sort(cities, (s1, s2) −> s1.length() − s2.length());

SortStringIgnoreCase

There is method that allows you to sort but ingoring case.

1
2
3
4
5
6
7
8
9
10
11
public class SortStringIgnoreCase {
public static void main(String[] args) {
java.util.List < String > cities = java.util.Arrays.asList("Atlanta", "Savannah", "new York", "dallas");
cities.sort((s1, s2) -> s1.compareToIgnoreCase(s2));
for (String s: cities) {
System.out.print(s + " ");
}
}
}

//Atlanta dallas new York Savannah
1
2
3
4
5
6
7
8
9
10
11
public class SortStringIgnoreCase {
public static void main(String[] args) {
java.util.List < String > cities = java.util.Arrays.asList("Atlanta", "Savannah", "new York", "dallas");
cities.sort(null); //sorted using its natural order.
for (String s: cities) {
System.out.print(s + " ");
}
}
}

//Atlanta Savannah dallas new York

The reversed() method

  • The default reversed() method can be used to reverse the order for a comparator.

Comparable vs Comparator in Java

Some Useful Methods