Set
Set
====
It is the Child Interface of Collection.
If we want to Represent a Group of Individual Objects as a
Single Entity where Duplicates are Not allowed
and Insertion Order is Not Preserved then we should go for Set.
Set Interface doesn't contain any New Methods and Hence we have
to Use Only Collection Interface Methods.
1)HashSet
Example :
package set;
import java.util.HashSet;
import java.util.Set;
}
}
Example :
package set;
import java.util.HashSet;
import java.util.Set;
}
}
Example :
package set;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
for (Integer s1 : s) {
System.out.println(s1);
}
}
}
Constructors:
The initial capacity of HashSet is 16 . When the load factor (0.75) is reached,
i.e. 16 * 0.75 = 12 ;
on the insertion of the 12th element the capacity is doubled , i.e. it becomes
32 .
Example :
package set;
import java.util.HashSet;
import java.util.Iterator;
System.out.println(s.size());
System.out.println(s);
}
}
Example :
package set;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
System.out.println(s);
}
}
Example :
package set;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
System.out.println(s);
}
}
package set;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
2)LinkedHashSet:
HashSet LinkedHashSet
The Underlying Data Structure is Hashtable. The Underlying
Data Structure is a Combination
of LinkedList and
Hashtable
Example :
package set;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;