Tuesday, July 28, 2009

All about Java Collection with simple example

Program:-

You can create a class
collections.Main
and copy-paste the following content. You can play with this example then to understand the Java Collection concept.

package collections;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Vector;

public class Main
{
public static void main( String... arg )
{

/*
* Set Interface
*/
Set set = new HashSet();
set.add(31);
set.add(20);
set.add(100);
set.add(31); // Notice: here we are trying to add object which is already present in set
System.out.println("Hash Set\t\t: " + set.toString());
set.clear();
set = new TreeSet(); // Tree Set is ordered collection
set.add(31);
set.add(20);
set.add(100);
set.add(31); // Notice: here we are trying to add object which is already present in set
System.out.println("Tree Set\t\t: " + set.toString());
SortedSet sset = new TreeSet(); // Tree Set is ordered collection
sset.add(31);
sset.add(20);
sset.add(100);
sset.add(32); // Notice: here we are trying to add object which is already present in set
System.out.println("Tree SortedSet\t\t: " + sset.toString());


/*
* List Interface
*/
List list = new ArrayList();
list.add(31);
list.add(20);
list.add(100);
list.add(31); // Notice: here we are trying to add object which is already present in set
System.out.println("Array List\t\t: " + list.toString());
list.clear();
list = new Vector();// Vector are thread safe
list.add(31);
list.add(20);
list.add(100);
list.add(31); // Notice: here we are trying to add object which is already present in set
System.out.println("Vector\t\t\t: " + list.toString());
LinkedList llist = new LinkedList();// Vector are thread safe
llist.add(31);
llist.add(20);
llist.add(100);
llist.add(31); // Notice: here we are trying to add object which is already present in set
llist.addFirst(0);
llist.addLast(1);
System.out.println("Linked List\t\t: " + llist.toString());


/*
* Map Interface
*/
Map map = new HashMap();
map.put(1, "abc");
map.put(3, "xyz");
map.put(2, "ijk");
map.put(null, "first");
map.put(null, "second");
map.put(4, null);
map.put(5, null);
System.out.println("Hash Map\t\t: " + map.toString());
map.clear();
map = new TreeMap();// Ordered collection
map.put(1, "abc");
map.put(3, "xyz");
map.put(2, "ijk");
// Cannot have null as a key
// map.put(null, "first");
// map.put(null, "second");
map.put(4, null);
map.put(5, null);
System.out.println("Tree Map\t\t: " + map.toString());
SortedMap smap = new TreeMap();// Ordered collection
smap.put(1, "abc");
smap.put(3, "xyz");
smap.put(2, "ijk");
// Cannot have null as a key
// map.put(null, "first");
// map.put(null, "second");
smap.put(4, null);
smap.put(5, null);
System.out.println("Tree SortedMap\t\t: " + map.toString());


/*
* Comparator and Comparable interfaces
*/
Employee emp1 = new Employee(2, "Ritesh", 23);
Employee emp2 = new Employee(1, "Ajay", 28);
Employee emp3 = new Employee(5, "Sonal", 25);
Employee emp4 = new Employee(3, "Payal", 22);
Employee emp5 = new Employee(4, "Sushil", 29);
Employee emp6 = new Employee(9, "Pranav", 25);
Employee emp7 = new Employee(8, "Amit", 23);
Employee emp8 = new Employee(7, "Ganesh", 22);
Employee emp9 = new Employee(6, "Gaurav", 20);
List empList = new ArrayList();
empList.add(emp1);
empList.add(emp2);
empList.add(emp3);
empList.add(emp4);
empList.add(emp5);
empList.add(emp6);
empList.add(emp7);
empList.add(emp8);
empList.add(emp9);
// Collections.sort(empList);
System.out.println("\n\nWithout Comparable/Comparator:" + empList);
// WITH COMPARABLE IMPLEMENTED
EmployeeC empc1 = new EmployeeC(2, "Ritesh", 23);
EmployeeC empc2 = new EmployeeC(1, "Ajay", 28);
EmployeeC empc3 = new EmployeeC(5, "Sonal", 25);
EmployeeC empc4 = new EmployeeC(3, "Payal", 22);
EmployeeC empc5 = new EmployeeC(4, "Sushil", 29);
EmployeeC empc6 = new EmployeeC(9, "Pranav", 25);
EmployeeC empc7 = new EmployeeC(8, "Amit", 23);
EmployeeC empc8 = new EmployeeC(7, "Ganesh", 22);
EmployeeC empc9 = new EmployeeC(6, "Gaurav", 20);
List empcList = new ArrayList();
empcList.add(empc1);
empcList.add(empc2);
empcList.add(empc3);
empcList.add(empc4);
empcList.add(empc5);
empcList.add(empc6);
empcList.add(empc7);
empcList.add(empc8);
empcList.add(empc9);
Collections.sort(empcList);
System.out.println("\n\nWith Comparable:" + empcList);
// WITH COMPARATOR
Collections.sort(empList, new EmployeeNameComparator());
System.out.println("\n\nWith Name Comparator:" + empList);
Collections.sort(empList, new EmployeeAgeComparator());
System.out.println("\n\nWith Age Comparator:" + empList);
}
}

/*
* Following class doesn't implements comparable
*/
class Employee
{
public int empId;
public String empName;
public int age;

public Employee( int id, String name, int age )
{
this.empId = id;
this.empName = name;
this.age = age;
}

@Override
public String toString()
{
// TODO Auto-generated method stub
return "\nId:" + this.empId + " Name:" + this.empName + " Age:" + this.age;
}
}

/*
* Following class implement Comparable interface
*/
class EmployeeC extends Employee implements Comparable
{
public EmployeeC( int id, String name, int age )
{
super(id, name, age);
}

@Override
public int compareTo( Employee o )
{
return (this.empId - o.empId);
}
}

/*
* Comparator classes to compare Employee with different criteria
*/
class EmployeeNameComparator implements Comparator
{
@Override
public int compare( Employee o1, Employee o2 )
{
return o1.empName.compareTo(o2.empName);
}
}

class EmployeeAgeComparator implements Comparator
{
@Override
public int compare( Employee o1, Employee o2 )
{
return (o1.age < o2.age ? 0 : 1);
}
}


Output:-

Hash Set : [100, 20, 31]
Tree Set : [20, 31, 100]
Tree SortedSet : [20, 31, 32, 100]
Array List : [31, 20, 100, 31]
Vector : [31, 20, 100, 31]
Linked List : [0, 31, 20, 100, 31, 1]
Hash Map : {null=second, 1=abc, 2=ijk, 3=xyz, 4=null, 5=null}
Tree Map : {1=abc, 2=ijk, 3=xyz, 4=null, 5=null}
Tree SortedMap : {1=abc, 2=ijk, 3=xyz, 4=null, 5=null}


Without Comparable/Comparator:[
Id:2 Name:Ritesh Age:23,
Id:1 Name:Ajay Age:28,
Id:5 Name:Sonal Age:25,
Id:3 Name:Payal Age:22,
Id:4 Name:Sushil Age:29,
Id:9 Name:Pranav Age:25,
Id:8 Name:Amit Age:23,
Id:7 Name:Ganesh Age:22,
Id:6 Name:Gaurav Age:20]


With Comparable:[
Id:1 Name:Ajay Age:28,
Id:2 Name:Ritesh Age:23,
Id:3 Name:Payal Age:22,
Id:4 Name:Sushil Age:29,
Id:5 Name:Sonal Age:25,
Id:6 Name:Gaurav Age:20,
Id:7 Name:Ganesh Age:22,
Id:8 Name:Amit Age:23,
Id:9 Name:Pranav Age:25]


With Name Comparator:[
Id:1 Name:Ajay Age:28,
Id:8 Name:Amit Age:23,
Id:7 Name:Ganesh Age:22,
Id:6 Name:Gaurav Age:20,
Id:3 Name:Payal Age:22,
Id:9 Name:Pranav Age:25,
Id:2 Name:Ritesh Age:23,
Id:5 Name:Sonal Age:25,
Id:4 Name:Sushil Age:29]


With Age Comparator:[
Id:6 Name:Gaurav Age:20,
Id:3 Name:Payal Age:22,
Id:7 Name:Ganesh Age:22,
Id:2 Name:Ritesh Age:23,
Id:8 Name:Amit Age:23,
Id:5 Name:Sonal Age:25,
Id:9 Name:Pranav Age:25,
Id:1 Name:Ajay Age:28,
Id:4 Name:Sushil Age:29]

No comments:

Post a Comment

Was the information useful?

Followers