import java.util.HashSet;
import java.util.TreeSet;
import java.util.Set;
class ConvertHashsettoTreeset{
public static void main(String[] args) {
// Create a HashSet
HashSet<String> hset = new HashSet<String>();
//add elements to HashSet
hset.add("java");
hset.add("DOTNET");
hset.add("ORACLE");
hset.add("MY SQL");
// Displaying HashSet elements
System.out.println("HashSet contains: "+ hset);
// Creating a TreeSet of HashSet elements
Set<String> tset = new TreeSet<String>(hset);
// Displaying TreeSet elements
System.out.println("TreeSet contains: ");
for(String temp : tset){
System.out.println(temp);
}
}
}
OUTPUT:
HashSet contains: [MY SQL, DOTNET, ORACLE, java]
TreeSet contains:
DOTNET
MY SQL
ORACLE
java
import java.util.TreeSet;
import java.util.Set;
class ConvertHashsettoTreeset{
public static void main(String[] args) {
// Create a HashSet
HashSet<String> hset = new HashSet<String>();
//add elements to HashSet
hset.add("java");
hset.add("DOTNET");
hset.add("ORACLE");
hset.add("MY SQL");
// Displaying HashSet elements
System.out.println("HashSet contains: "+ hset);
// Creating a TreeSet of HashSet elements
Set<String> tset = new TreeSet<String>(hset);
// Displaying TreeSet elements
System.out.println("TreeSet contains: ");
for(String temp : tset){
System.out.println(temp);
}
}
}
OUTPUT:
HashSet contains: [MY SQL, DOTNET, ORACLE, java]
TreeSet contains:
DOTNET
MY SQL
ORACLE
java
No comments:
Post a Comment