Skip to main content

Posts

Showing posts with the label java HashSet

Collection(Set,HashSet,LinkedHashSet)

Set:- Set is a child interface of collection, set is used to represent group of individual objects as a single entity where duplicates not allowed and not maintain the insertion order of elements. In case of duplicate insertion add method return false otherwise return true,here HashSet is implemented class on Set interface. Example1:-TestSet.java import java.util.*; class TestSet { public static void main(String[]args) { Set s=new HashSet(); System.out.println(s); s.add(1); s.add(2); s.add(3); s.add(4); System.out.println(s); System.out.println("Set elements by Iterator"); Iterator itr=s.iterator(); while(itr.hasNext()) { System.out.println(itr.next()); } } } Output:- C:\JAVATECH>javac TestSet.java C:\JAVATECH>java TestSet [] [1, 2, 3, 4] Set elements by Iterator 1 2 3 4 Example2:-TestSet.java import java.util.*; class TestSet { public static void main(String[]args) { Set s=new HashSet(); Sy...