NOTE: The below example is for HashSet and to test it for TreeSet just comment out the below green uncommented lines and uncommented the green commented lines.
Both HashSet and TreeSet s' output are given below
//SetExample.java
/*
* 1.Unique
* 2.HashSet => Not Order
* 3.TreeSet Don't support null value but HasSet support one NULL value.
* 4.TreeSet support sorted order
*/
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
//import java.util.TreeSet;
* 1.Unique
* 2.HashSet => Not Order
* 3.TreeSet Don't support null value but HasSet support one NULL value.
* 4.TreeSet support sorted order
*/
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
//import java.util.TreeSet;
public class SetExample
{
public static void main(String a[])
{
Set hs = new HashSet();
//Set hs = new TreeSet();
System.out.println("Initial size = " + hs.size());
for (int i = 0; i < 8; i++)
{
hs.add(i);
}
System.out.println("Set elements are as follows:");
Iterator it1 = hs.iterator();
while (it1.hasNext())
{
System.out.println(it1.next());
}
System.out.println("After adding elements set size = " + hs.size());
hs.add(5);//Add duplicate value to show that SET don't support duplicate value
hs.add(9);
try
{
hs.add(null);
}
catch (Exception e)
{
System.out.println("TreeSet don't support "+e.getMessage()+" value");
}
System.out.println("After adding two elements one duplicate one and two a new one set's elements are as:");
Iterator it2 = hs.iterator();
while (it2.hasNext())
{
System.out.println(it2.next());
}
System.out.println("After adding two elements one duplicate one and two a new one set size = " + hs.size());
}
}
*********************************************************
Output for HashSet =>
Initial size = 0
Set elements are as follows:
2
4
6
1
3
7
5
0
After adding elements set size = 8
After adding two elements one duplicate one and two a new one set's elements are as:
2
4
9
null
6
1
3
7
5
0
After adding two elements one duplicate one and two a new one set size = 10
*********************************************************
Output for TreeSet =>
Initial size = 0
Set elements are as follows:
0
1
2
3
4
5
6
7
After adding elements set size = 8
TreeSet don't support null value
After adding two elements one duplicate one and two a new one set's elements are as:
0
1
2
3
4
5
6
7
9
After adding two elements one duplicate one and two a new one set size = 9
{
public static void main(String a[])
{
Set hs = new HashSet();
//Set hs = new TreeSet();
System.out.println("Initial size = " + hs.size());
for (int i = 0; i < 8; i++)
{
hs.add(i);
}
System.out.println("Set elements are as follows:");
Iterator it1 = hs.iterator();
while (it1.hasNext())
{
System.out.println(it1.next());
}
System.out.println("After adding elements set size = " + hs.size());
hs.add(5);//Add duplicate value to show that SET don't support duplicate value
hs.add(9);
try
{
hs.add(null);
}
catch (Exception e)
{
System.out.println("TreeSet don't support "+e.getMessage()+" value");
}
System.out.println("After adding two elements one duplicate one and two a new one set's elements are as:");
Iterator it2 = hs.iterator();
while (it2.hasNext())
{
System.out.println(it2.next());
}
System.out.println("After adding two elements one duplicate one and two a new one set size = " + hs.size());
}
}
*********************************************************
Output for HashSet =>
Initial size = 0
Set elements are as follows:
2
4
6
1
3
7
5
0
After adding elements set size = 8
After adding two elements one duplicate one and two a new one set's elements are as:
2
4
9
null
6
1
3
7
5
0
After adding two elements one duplicate one and two a new one set size = 10
*********************************************************
Output for TreeSet =>
Initial size = 0
Set elements are as follows:
0
1
2
3
4
5
6
7
After adding elements set size = 8
TreeSet don't support null value
After adding two elements one duplicate one and two a new one set's elements are as:
0
1
2
3
4
5
6
7
9
After adding two elements one duplicate one and two a new one set size = 9
No comments:
Post a Comment