//HashSet -----------------
//duplicate value are not allowed.
//insertion order not preserved.
// null valued allowed only once time(bcz duplicate not allowed) and add its always 0th index.
==============================================================================
//LinkedHashSet -------------------
//duplicate value are not allowed.
//insertion order preserved.
//null value insert also in order.
==============================================================================
//Treeset .....................
//duplicate value not allowed.
//if we want to perform sorting then we should go for TreeSet-(Sorting order will be Ascending Order)
//null value is not allowed if we insert to null value in TreeSet then we will get NullPointerException:-
//if we want to perform sorting in descending order then descendingSet() is used.but strictly speaking this method is available in
//TreeSet class and This method return type is NavigableSet type so casting is required.
TreeSet<String> s1=new TreeSet<String>();//descendingSet() available for u........
Set<String> s1=new TreeSet<String>();//descendingSet() not available for u.......
===============================================================================
========================================================================
//HashSet demonstration:-----------------
//duplicate value not allowed.
//insertion order not preserved.
// null valued allowed only once time(bcz duplicate not allowed) and add its always 0th index.
=====================================================================
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class SetDemo {
public static void main(String[] args) {
Set<String> s1=new HashSet<String>();
s1.add("govind");
s1.add("ballabh");
s1.add("khan");
s1.add("govind");
s1.add("khan");
s1.add(null);
System.out.println("--------------System.out.println(s1)----------");
System.out.println(s1);
System.out.println("------------for each loop---------------");
for(String s:s1)
{
System.out.println(s);
}
System.out.println("------------iterator---------------------");
Iterator<String> ss = s1.iterator();
while (ss.hasNext()) {
String kk = ss.next();
System.out.println(kk);
}
}
}
o/p:-
--------------System.out.println(s1)----------
[null, ballabh, govind, khan]
------------for each loop---------------
null
ballabh
govind
khan
------------iterator---------------------
null
ballabh
govind
khan
=============================================================================================
//LinkedHashSet demonstration--------------------
//duplicate value not allowed.
//insertion order preserved.
//null value insert also in order.
============================================================================================
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
public class SetDemo {
public static void main(String[] args) {
Set<String> s1=new LinkedHashSet<String>();
s1.add("govind");
s1.add("ballabh");
s1.add("khan");
s1.add("govind");
s1.add("khan");
s1.add(null);
System.out.println("--------------System.out.println(s1)----------");
System.out.println(s1);
System.out.println("------------for each loop---------------");
for(String s:s1)
{
System.out.println(s);
}
System.out.println("------------iterator---------------------");
Iterator<String> ss = s1.iterator();
while (ss.hasNext()) {
String kk = ss.next();
System.out.println(kk);
}
}
}
================================================================================================
o/p:-
--------------System.out.println(s1)----------
[govind, ballabh, khan, null]
------------for each loop---------------
govind
ballabh
khan
null
------------iterator---------------------
govind
ballabh
khan
null
===============================================================================================
//Treeset Demonstration.....................
//duplicate value not allowed.
//if we want to perform sorting then we should go for TreeSet-(Sorting order will be Ascending Order)
//null value is not allowed if we insert to null value in TreeSet then we will get NullPointerException:-
//if we want to perform sorting in descending order then descendingSet() is used.but strictly speaking this method is available in
TreeSet class and This method return type is NavigableSet type so casting is required.
TreeSet<String> s1=new TreeSet<String>();//descendingSet() available for u........
Set<String> s1=new TreeSet<String>();//descendingSet() not available for u.......
===============================================================================================
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.NavigableSet;
import java.util.Set;
import java.util.TreeSet;
public class SetDemo {
public static void main(String[] args) {
TreeSet<String> s1=new TreeSet<String>();
s1.add("govind");
s1.add("ballabh");
s1.add("khan");
s1.add("govind");
s1.add("khan");
//s1.add(null);//java.lang.NullPointerException
System.out.println("--------------System.out.println(s1)----------");
System.out.println(s1);
System.out.println("------------for each loop---------------");
for(String s:s1)
{
System.out.println(s);
}
System.out.println("------------iterator---------------------");
Iterator<String> ss = s1.iterator();
while (ss.hasNext()) {
String kk = ss.next();
System.out.println(kk);
}
System.out.println("------We want to result in reverse order-----");
TreeSet<String> k1 =(TreeSet<String>) s1.descendingSet();
for(String kk:k1)
{
System.out.println(kk);
}
}
}
================================================================================================
o/p:-
--------------System.out.println(s1)----------
[ballabh, govind, khan]
------------for each loop---------------
ballabh
govind
khan
------------iterator---------------------
ballabh
govind
khan
------We want to result in reverse order-----
khan
govind
ballabh
No comments:
Post a Comment