By Using HashSet implementation the order of the input may differ.If you want to print in the alphabetical order you can use TreeSet class.
package collections;
import java.util.Set;
import java.util.TreeSet;
public class HashSetCollection {
public static void main(String[] args) {
Set<String> hs=new TreeSet<String>();
hs.add("hi");
hs.add("i");
hs.add("am");
hs.add("Abhishek");
hs.add("Awasthi");
hs.add("Abhishek Awasthi");
hs.add("Abhishek");
hs.add("Awasthi");
System.out.println("Size: "+hs.size());
System.out.println("Distinct Words : "+hs);
}
}
package collections;
import java.util.Set;
import java.util.TreeSet;
public class HashSetCollection {
public static void main(String[] args) {
Set<String> hs=new TreeSet<String>();
hs.add("hi");
hs.add("i");
hs.add("am");
hs.add("Abhishek");
hs.add("Awasthi");
hs.add("Abhishek Awasthi");
hs.add("Abhishek");
hs.add("Awasthi");
System.out.println("Size: "+hs.size());
System.out.println("Distinct Words : "+hs);
}
}
0 comments:
Post a Comment