6-Collection Part-6
6-Collection Part-6
HashSet:
• HashSet is a direct implementation class to Set interface.
• It was introduced in JDK 1.2 version.
• It is not index based, it able to arrange all the elements on the basis of elements hashcode values.
• It will not allow duplicate elements.
• It will not follow insertion order.
• It will not follow Sorting order.
• It able to allow only one null value.
• Its interal data structure is "Hashtable".
• Its initial capacity is "16" elements and its initial fill_Ratio is 75%.
• It is not synchronized.
• Almost all the methods are not synchronized in HashSet
• It allows more than one thread at a time.
• It follows parallel execution.
• It will reduce execution time.
• It improves performance of the applications.
• It is not giving guarantee for data consistency.
• It is not threadsafe.
Constructors:
1) public HashSet()
This constructor can be used to create an empty HashSet object with 16 elements as initial
capacity and 75% fill ratio.
Ex:
If internal capacity is 16 and the load factor is 0.75 then the number of buckets will automatically get
increased when the table has 12 elements in it.
EX:
HashSet hs = new HashSet();
4) public HashSet(Collection c)
This constructor can be used to create HashSet object with all the elements of the specified
Collection.
EX:
import java.util.*;
class Test
{
public static void main(String[] args)
{
HashSet hs1=new HashSet();
hs1.add("A");
hs1.add("B");
hs1.add("C");
hs1.add("D");
hs1.add("E");
System.out.println(hs1);
HashSet hs2=new HashSet(hs1);
System.out.println(hs2);
}
}
[D,E,A,B,C]
[D,E,A,B,C]
EX:
import java.util.*;
class Test
{
public static void main(String[] args)
{
HashSet hs=new HashSet();
hs.add("A");
hs.add("B");
hs.add("C");
hs.add("D");
hs.add("E");
System.out.println(hs);
hs.add("B"); //It will not add B
System.out.println(hs);
hs.add(null);
hs.add(null);
System.out.println(hs);
hs.add(new Integer(10));
System.out.println(hs);
}
}
LinkedHashSet:
Que )
What are the differences between HashSet and LinkedHashSet?
Ans:
• HashSet was introduced in JDK 1.2 version.
LinkedhashSet was introduced in JDK 1.4 version.
EX
import java.util.*;
class Test
{
public static void main(String[] args)
{
HashSet hs=new HashSet();
hs.add("A");
hs.add("B");
hs.add("C");
hs.add("D");
hs.add("E");
System.out.println(hs);
SortedSet:
SoretedSet is an Interface.
• It was introduced in JDK1.2 version.
• It is a child interface to Set interface.
• It is not index based.
• It is not allowing duplicate elements.
• It is not following insertion order.
• It follows Sorting order.
• It allows only homogeneous elements.
• It will not allow heterogeneous elements, if we are trying to add heterogeneous elements then
JVM will rise an exception like java.lang.ClasscastException.
• It will not allow null values, if we are trying to add any null value then JVM will rise an exception
like java.lang.NullPointerException.
• It able to allow only Comparable objects by default, if we are trying to add non comparable
objects then JVM will rise an exception like java.lang.ClassCastException.
Note:
If we are trying to add non comparable objects then we have to use Comparator.
EX:
import java.util.*;
class Test
{
public static void main(String[] args)
{
TreeSet ts=new TreeSet();
ts.add("D");
ts.add("F");
ts.add("B");
ts.add("E");
ts.add("C");
ts.add("A");
System.out.println(ts);
System.out.println(ts.first());
System.out.println(ts.last());
System.out.println(ts.headSet("D"));
System.out.println(ts.tailSet("D"));
System.out.println(ts.subSet("B","E"));
}
}
NavigableSet:
It was introduced in JAVA6 version, it is a child interface to SortedSet interface, it is following all the properties
of SortedSet and it has define methods to provide navigations over the elements.
Methods:
• public Object ceiling(Object obj)
It will return lowest element among all the elements which are greater than or equals to the specified
element.
• public Object higher(Object obj)
It will return lowest element among all the elements which are greater than the specified element.
• public Object floor(Object obj)
It will return highest element among all the elements which are less than or equals to the specified
element.
TreeSet:
• It was introduced in JDK 1.2 version.
• It is not Legacy Collection.
• It has provided implementation for Collection, Set, SortedSet and NavigableSet interfaces.
• It is not index based.
• It is not allowing duplicate elements.
• It is not following insertion order.
• It follows Sorting order.
• It allows only homogeneous elements.
• It will not allow heterogeneous elements, if we are trying to add heterogeneous elements then JVM will
rise an exception like java.lang.ClasscastEx ception.
• It will not allow null values, if we are trying to add any null value then JVM will rise an exception like
java.lang.NullPointerException.
• It able to allow only Comparable objects by default, if we are trying to add non comparable objects then JVM
will rise an exception like java.lang.ClassCastException.
NOTE:
If we are trying to add non comparable objects then we have to use java.util.Comparator.
• Its internal data structure is "Balanced Tree".
• It is mainly for frequent search operations.
Constructors:
• public TreeSet()
It can be used to create an Empty TreeSet object.
EX:
TreeSet ts = new TreeSet();
• public TreeSet(Comparator c)
It will create an empty TreeSet object with the explicit Sorting mechanism in the form of
Comparator
EX:
TreeSet ts = new TreeSet(new MyComparator());
4) public TreeSet(Collection c)
It able to create TreeSet object with all the elements of the specified Collection.
EX:
import java.util.*;
class Test
{
public static void main(String[] args)
{
ArrayList al=new ArrayList();
al.add("B");
al.add("C");
al.add("F");
al.add("A");
al.add("E");
al.add("D");
System.out.println(al);
TreeSet ts=new TreeSet(al);
System.out.println(ts);
}
}
EX:
import java.util.*;
class Test
{
public static void main(String[] args)
{
TreeSet ts=new TreeSet();
ts.add("B");
ts.add("C");
ts.add("F");
ts.add("A");
ts.add("E");
ts.add("D");
System.out.println(ts);
ts.add("B");
System.out.println(ts);
//ts.add(null);--> NullPointerException
//ts.add(new Integer(10));-->ClassCastException
//ts.add(new StringBuffer("BBB"));->ClassCastException
}
}
When we add elements to the TreeSet object, TreeSet object will arrange all the elements in a particular sorting
order with the following algorithm.
• TreeSet will construct a Tree [Balanced Tree] on the basis of the elements.
To construct Balanced Tree we have to use the following steps.
• If the element is first element to the TreeSet object then make that element as "Root Node".
• If the element is not first element then access compareTo(--) method over the present element by passing
previous elements one by one of the balanced Tree right from root node until the present element is located in
Tree.
• If compareTo(-) method returns -ve value then go to left child of the present node and access again compareTo(-
) method by passing left child. If no left child is existed then make the present element as left child
• If compareTo(-) method returns +ve value then go to right child and access again compareTo(-) by passing right
as parameter. If no right child is existed then make the present element as right child.
• If compareTo(-) method return 0 value then discard the present element and declare that the present element is
a duplicate element of the existed element.
• TreeSet will retrieve all the elements from balanced Tree by following in order traversal.
If we want to add user defined elements like Employee, Student, Customer to TreeSet then we have to use the
following steps.
Ex:
import java.util.TreeSet;
class Employee implements Comparable{
int eno;
String ename;
float esal;
String eaddr;
@Override
public int compareTo(Object obj) {
Employee emp = (Employee)obj;
int val = 0;
val= this.ename.compareTo(emp.ename);
return val;
}
@Override
public String toString() {
return "\nEmployee{" +
"eno=" + eno +
", ename='" + ename + '\'' +
", esal=" + esal +
", eaddr='" + eaddr + '\'' +
'}';
}
}
public class Test {
public static void main(String[] args) {
Employee emp1 = new Employee(111, "DD", 5000, "AGR");
Employee emp2 = new Employee(222, "Neelesh", 6000, " AGR ");
Employee emp3 = new Employee(333, "Aishwary", 7000, " AGR ");
Employee emp4 = new Employee(444, "Gopi Krishna", 8000, " AGR");
Employee emp5 = new Employee(555, "Tahauddin", 9000, " AGR");
If we add non-comparable objects to TreeSet object then JVM will rise an exception like
java.lang.ClassCastException, because, Non-Comparable objects are not providing compareTo(-) method
internally, but, it is required to the TreeSet inorder to provide sorting order over elements.
If we want to add non-Comparable objects to TreeSet object then we must provide sorting logic to TreeSet object
explicitly, for this, we have to use java.util.Comparator interface.
If we want to use Comparator interface in java applications then we have to use the following steps.
Note:
In User defined class it is not required to implement equals(-) method, because, equals(-)
method will come from default super class Object.
EX:
MyComparator mc = new MyComparator();
TreeSet ts = new TreeSet(mc);
EX:
import java.util.*;
class MyComparator implements Comparator
{
int length1=s1.length();
int length2=s2.length();
int val=0;
if(length1<length2)
{
val=-100;
}
else if(length1>length2)
{
val=100;
}
else
{
val=0;
}
return val;
}
}
class Test
{
public static void main(String[] args)
{
StringBuffer sb1=new StringBuffer("AAA");
StringBuffer sb2=new StringBuffer("BB");
StringBuffer sb3=new StringBuffer("CCCC");
StringBuffer sb4=new StringBuffer("D");
StringBuffer sb5=new StringBuffer("EEEEE");
MyComparator mc=new MyComparator();
TreeSet ts=new TreeSet(mc);
ts.add(sb1);
ts.add(sb2);
ts.add(sb3);
ts.add(sb4);
ts.add(sb5);
System.out.println(ts);
}
}
EX:
import java.util.*;
class MyComparator implements Comparator
{
public int compare(Object obj1, Object obj2)
{
Student s1=(Student)obj1;
Student s2=(Student)obj2;
int val=s1.saddr.compareTo(s2.saddr);
return -val;
}
}
class Student
{
String sid;
String sname;
String saddr;
Que) In Java applications, if we provide both implicit Sorting through Comparable and
explicit sorting through Comparator to the TreeSet object at a time then which Sorting
logic would be preferred by TreeSet inorder to Sort elements?
If we provide both implicit Sorting through Comparable and Explicit Sorting through Comparator to the TreeSet
object at a time then TreeSet will take explicit Sorting through Comparator to sort all the elements.
EX:
import java.util.*;
class MyComparator implements Comparator
{
public int compare(Object obj1, Object obj2)
{
Customer cust1=(Customer)obj1;
Customer cust2=(Customer)obj2;
int val=cust1.caddr.compareTo(cust2.caddr);
return -val;
}
}
class Customer implements Comparable
{
String cid;
String cname;
String caddr;
System.out.println(ts);
}
}
Queue:
PriorityQueue:
• It was introduced in JDK 5.0 version.
• It is not Legacy Collection.
• It is a direct implementation class to Queue interface.
• It able to arrange all the elements prior to processing on the basis of the priorities.
• It able to allow duplicate elements.
• It is not following Insertion order.
• It is following Sorting order.
• It will not allow null values, if we add null value then JVM will rise an Exception like
java.lang.NullPointerException.
• It will not allow heterogeneous elements, if we add heterogeneous elements then JVM will rise
an exception like java.lang.ClassCastException.
• It able to allow only homogeneous elements.
• It able to allow comparable objects by default, if we add non comparable objects then JVM will
rise an exception like java.lang.ClassCastException.
• If we want to add non comparable objects then we have to use Comparator.
• Its initial capacity 11 elements.
• It is not synchronized .
• No method is synchronized in PriorityQueue.
• It allows more than one thread at a time to access data.
• It follows parallel execution.
• It able to reduce application execution time.
• It able to improve application performance.
• It is not giving guarantee for Data consistency.
• It is not threadsafe.
Constructors:
• public PriorityQueue()
It able to create an empty PriorityQueue with explicit sorting logic throug COmparator and the
specified capacity.
EX: MyComparator mc = new MyComparator();
PriorityQueue p = new PriorityQueue(20,mc);
• public PriorityQueue(SortedSet ss)
public PriorityQueue(Collection c)
It able to create PriorityQueue object with all the elements of the specified Collection.
EX:
ArrayList al = new ArrayList();
al.add("A");
al.add("B");
al.add("C");
al.add("D");
System.out.println(al);
PriorityQueue p = new PriorityQueue(al);
System.out.println(p);
EX:
1) import java.util.*;
2) class Test
3) {
4) public static void main(String[] args)
5) {
6) PriorityQueue p=new PriorityQueue();
7) p.add("A");
8) p.add("D");
9) p.add("B");
10) p.add("C");
11) p.add("F");
12) p.add("E");
13) System.out.println(p);
14) p.add("B");
15) System.out.println(p);
16) //p.add(null);-->NullPointerException
17) //p.add(new Integer(10));->ClassCastException
18) //p.add(new StringBuffer("BBB"));->ClassCastException
19) }
20) }