All About Java Collections

By ongraph
February 9, 2015 | 1003 Views

Latest Industry insights to keep you updated on the latest happenings.

Goals to design collection framework is –

  • The implementations of fundamental collections (linked list, dynamic arrays, tree, hashtables) are highly  efficient.
  • The framework had to allow different type of collections to work in a similar manner and with in a high degree of interoperability.
  • Extending or adapting a collection easy.

All Collections framework contain the following

  • Interfaces
  • Implementation (classes)
  • Algorithm

Collection Interfaces :-

These are abstract data types that represent collections. Interfaces allow collections to be manipulated independently of the details of their representation.

List of collection interfaces.

01 The Collection Interface
02 The List Interface
03 The Set
04 The SortedSet
05 The Map
06 The Map.Entry
07 The SortedMap
08 The Enumeration

Collection Classes (Implementation) – These are the concrete implementations of the collection interfaces. In essence, they are reusable data structures.

List of collection classes.

01 AbstractCollection
02 AbstractList
03 AbstractSequentialList
04 LinkedList
05 ArrayList
06 AbstractSet
07 HashSet
08 LinkedHashSet
09 TreeSet
10 AbstractMap
11 HashMap
12 TreeMap
13 WeakHashMap
14 LinkedHashMap
15 IdentityHashMap

Collection Algorithms – These are the methods that perform useful computations, such as searching and sorting, on objects that implement collection interfaces.

The collections framework defines several algorithms. These algorithms are defined as static methods within the Collections class.

Iterate Collection

When the for-each loop is not available, and an explicit Iterator is needed, then iteration over a collection may be done with a while loop or a for loop.The two styles have different advantages:

  • The while loop is considerably more legible
  • The for loop minimizes the scope of the Iterator to the loop itself

Example:-

import java.util.*;
public final class LoopStyles {
public static void main(String... aArguments) {
List<String> flavours = new ArrayList<>();
flavours.add("chocolate");
flavours.add("strawberry");
flavours.add("vanilla");
useWhileLoop(flavours);
useForLoop(flavours);
}
private static void useWhileLoop(Collection<String> aFlavours) {
Iterator<String> flavoursIter = aFlavours.iterator();
while (flavoursIter.hasNext()){
System.out.println(flavoursIter.next());
}
}
/**
* Note that this for-loop does not use an integer index.
*/
private static void useForLoop(Collection<String> aFlavours) {
for (Iterator<String> flavoursIter = aFlavours.iterator(); flavoursIter.hasNext();){
System.out.println(flavoursIter.next());
}
}
}

 

Methods declared by Iterator are –

  • boolean hasNext( )
  • Object next( )
  • void remove( )

Collection Comparator 

Both TreeSet and TreeMap store elements in sorted order. However, it is the comparator that defines precisely what sorted order means. To use the Comparator interface you have to implement it and pass it as anonymous cass to Collections.sort(List list, Comparator c) as second parameter. If you want to pass only the list to Collections.sort(List list) then your Item class has to the implement Comparable interface. So in both cases the Collections.sort methods know how to order the elements in your list

Here is some sample code: Item class implementing Comparable + Inventory holding a list of items

Example :- 

public class Item implements Comparable<Item> {
String id = null;
public Item(String id) {
this.id = id;
}
@Override
public String toString() {
return id;
}
@Override
public int compareTo(Item o) {
return - id.compareToIgnoreCase(o.id);
}
}
public class Inventory {
List<Item> items = new ArrayList<>();
public void addItem(Item item) {
items.add(item);
}
public static void main(String[] args) {
Inventory inventory = new Inventory();
inventory.addItem(new Item("2"));
inventory.addItem(new Item("4"));
inventory.addItem(new Item("1"));
inventory.addItem(new Item("7"));
Collections.sort(inventory.items, new Comparator<Item>() {
@Override
public int compare(Item o1, Item o2) {
return o1.id.compareToIgnoreCase(o2.id);
}
});
System.out.println(inventory.items);
Collections.sort(inventory.items);
System.out.println(inventory.items);
}
}

Monthly industry insights to keep you updated on latest happenings

Follow us on Twitter
Follow us on Facebook
Follow us on Linkedin