Sunday, 14 May 2017

Java Collection Classes ~ mkniit

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("ab");
  al.add("bc");
  al.add("cd");
  system.out.println(al);
 }
}
Output :
[ab,bc,cd]

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 Collectionn 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

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("Ricky", "Pointing","999100091");
       Contact c2 = new Contact("David", "Beckham","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:
[Ricky Pointing(999100091), David Beckham(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.addLast("z");
  ll.addFirst("A");
  System.out.println(ll);
 }
}
Output:
[A, a, b,c, z]

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:
[D, E, F, A, B, C]

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.
  6. TreeSet()
    
    TreeSet( Collection C )
    
    TreeSet( Comparator comp )
    
    TreeSet( SortedSet ss )

No comments:

Post a Comment