Collections Framework A collection is a group of objects. The Collection Framework Standardizes the way in which group of objects are handled by your program.What c++ calls a Container,Java calls a Collection
The Collection interfaces Collection- enable you to work with groups of objects. List- extends collection to handle sequences. Set- extends collection to handle sets, which must contain unique elements.
The Collection Classes ArrayList – implements a dynamic array. LinkedList – implements a linked list. HashSet – uses hash table. linkedHashSet – allow insertion-order iterations. TreeSet – implements a set stored in a tree.
ArrayList class ArrayList class implements the List interface. ArrayList supports dynamic arrays that can grow as needed. It is a Variable length array of object references. ArrayLists are created with an initial size. When this size is enlarged, the collection is automatically enlarged. And when the objects are removed, the array may be shrunk.
Using constructor ArrayList() ArrayList(Collection c) ArrayList(int capacity)
Methods and Examples Create an array list ArrayList a1 = new ArrayList(); Add elements to the array list a1.add(“c”); a1.add(1,“A2”);
Display the array list System.out.println(a1); Remove elements from the array list a1.remove(“F”); a1.remove(2); For finding the size of arrayList a1.size();
Obtaining an Array from an ArrayList Add elements to the array list a1.add(new Integer(1)); Get array object ia[ ] = a1.toArray();
LinkedList Class LinkedList class extends AbstractSequentialList and implements the List interface. It provides a linked list data structure.
Constructor LinkedList() LinkedList(collection c)
Methods To add elements at the first /last position. addFirst(); addLast(); To retrieve the elements at first /last position. getFirst(); getLast(); To remove the elements at first /last position. removeFirst(); removeLast();
TreeSet class TreeSet provides an implementations of the Set interface that uses a tree for storage. Objects are stored in sorted order i.e ascending order. Access and retrieval times are quite fast.