Collection Framework in JAVA 3
The Collection classes
Java provides a set of Collection classes that implements Collection interface. Some of these classes provide full implementations that can be used as it is and other abstract classes provides skeletal implementations that can be used as starting points for creating concrete collections.
ArrayList Class
1. ArrayList class extends AbstractList class and implements the List interface.
2. ArrayList supports dynamic array that can grow as needed. ArrayList has three constructors.
ArrayList()
ArrayList( Collection C )
ArrayList( int capacity )
3. ArrayLists are created with an initial size, when this size is exceeded, it gets enlarged automatically.
4. It can contain Duplicate elements and maintains the insertion order.
5.ArrayLists are not synchronized.
Example of ArrayList
import java.util.*;
class Test
{
public static void main(String[] args)
{
ArrayList< String> al = new ArrayList< String>();
al.add("abc");
al.add("pqr");
al.add("xyz");
System.out.println(al);
}
}
Output : [abc, pqr, xyz]
Getting Array from an ArrayList
toArray() method is used to get an araay containing all the contents of the list. Following are the reasons why you must obtain array from your ArrayList whenever required.
To obtain faster processing.
To pass array to methods who do not accept Collection as arguments.
To integrate and use collections with legacy code.
Storing User-Defined classes
In the above example we are storing only string object in ArrayList collection. But You can store any type of object, including object of class that you create in Collection classes.
Example of storing User-Defined object
import java.util.*;
//Contact class
class Contact
{
String first_name;
String last_name;
String phone_no;
public Contact(String fn,String ln,String pn)
{
first_name = fn;
last_name = ln;
phone_no = pn;
}
public String toString()
{
return first_name+" "+last_name+"("+phone_no+")";
}
}
//Storing Contact class
public class PhoneBook
{
public static void main(String[] args)
{
Contact c1 = new Contact("Roger", "Federer","999100091");
Contact c2 = new Contact("Lionel", "Messi","998392819");
Contact c3 = new Contact("Virat", "Kohli","998131319");
ArrayList< Contact> al = new ArrayList< Contact>();
al.add(c1);
al.add(c2);
al.add(c3);
System.out.println(al);
}
}
Output:
[Roger Federer(999100091), Lionel Messi(998392819), Virat Kohli(998131319)]
LinkedList Class
1. LinkedList class extends AbstractSequentialList and implements List, Deque and Queue inteface.
2. It can be used as List, stack or Queue as it implements all the related interfaces.
3. It can contain duplicate elements and is not synchronized.
Example of LinkedList class
import java.util.*;
class Test
{
public static void main(String[] args)
{
LinkedList< String> ll = new LinkedList< String>();
ll.add("a");
ll.add("b");
ll.add("c");
ll.addFirst("A");
ll.addLast("Z");
System.out.println(ll);
}
}
Output : [A, a, b, c, Z]
Difference between ArrayList and LinkedList
ArrayList | LinkedList |
ArrayList internally uses dynamic array to store the elements. | LinkedList internally uses doubly linked list to store the elements. |
Manipulation with ArrayList is slow because it internally uses array. If any element is removed from the array, all the bits are shifted in memory. | Manipulation with LinkedList is faster than ArrayList because it uses doubly linked list so no bit shifting is required in memory. |
ArrayList class can act as a list only because it implements List only. | LinkedList class can act as a list and queue both because it implements List and Deque interfaces. |
ArrayList is better for storing and accessing data. | LinkedList is better for manipulating data. |
HashSet Class
1. HashSet extends AbstractSet class and implements the Set interface.
2. It creates a collection that uses hash table for storage.
3. HashSet does not maintain any order of elements.
Example of HashSet class
import java.util.*;
class HashSetDemo
{
public static void main(String args[])
{
HashSet< String> hs = new HashSet< String>();
hs.add("B");
hs.add("A");
hs.add("D");
hs.add("E");
hs.add("C");
hs.add("F");
System.out.println(hs);
}
}
Output : [A, B, C, D, E, F]
LinkedHashSet Class
1. LinkedHashSet class extends HashSet class
2. LinkedHashSet maintains a linked list of entries in the set.
3. LinkedHashSet stores elements in the order in which elements are inserted.
Example of LinkedHashSet class
import java.util.*;
class LinkedHashSetDemo
{
public static void main(String args[])
{
LinkedHashSet< String> hs = new LinkedHashSet< String>();
hs.add("B");
hs.add("A");
hs.add("D");
hs.add("E");
hs.add("C");
hs.add("F");
System.out.println(hs);
}
}
Output : [B, A, D, E, C, F]
TreeSet Class
1. It extends AbstractSet class and implements the NavigableSet interface.
2. It stores elements sorted ascending order.
3. Uses a Tree structure to store elements.
4. Access and retrieval times are quite fast.
5. It has four Constructors.
TreeSet()
TreeSet( Collection C )
TreeSet( Comparator comp )
TreeSet( SortedSet ss )
Example of TreeSet class
import java.util.*;
class HashSetDemo
{
public static void main(String args[])
{
TreeSet< String> ts = new TreeSet< String>();
hs.add("B");
hs.add("A");
hs.add("D");
hs.add("E");
hs.add("C");
hs.add("F");
System.out.println(ts);
}
}
Output : [A, B, C, D, E, F]