|
| 1 | +package setExample; |
| 2 | + |
| 3 | +import java.util.HashSet; |
| 4 | +import java.util.Iterator; |
| 5 | + |
| 6 | +public class HashSetExample { |
| 7 | + |
| 8 | + /** |
| 9 | + * Set(I)-> HashSet (C) and LinkedHashSet(C) are implementations |
| 10 | + * Set(I) -> SortedSet(Child Interface)->NavigableSet(I)=> TreeSet(C) is the implementation |
| 11 | + *Important points to remember: |
| 12 | + *1. To store group of individual objects. |
| 13 | + *2. Duplicates not allowed |
| 14 | + *3.Insertion order will not be maintained |
| 15 | + *4.Set(I) doesn't have any new methods other than given in Collection(I). |
| 16 | + *5. DS for HashSet is Hash table |
| 17 | + *6. If we add duplicate value to HashSet, simply it will return false to the |
| 18 | + *add() and it won't throw any error or exception. |
| 19 | + *7. We can insert null values |
| 20 | + *8. Heterogeneous values can be added. |
| 21 | + *9. Implements Serializable and Cloneable?-> Yes |
| 22 | + *10. Data are stored based on hashcode, so search is very effective. |
| 23 | + *11. Fill Ratio or Load factor:0.75 or 75% |
| 24 | + *12.Default capacity-16 |
| 25 | + */ |
| 26 | + |
| 27 | + /* |
| 28 | + * Number Constructors available in HashSet=4 |
| 29 | + * 1. HashSet hs= new HashSet();// size-16 and fill ratio is 0.75 |
| 30 | + * 2. HashSet hs= new HashSet(int initialSize);size as declared and fill ratio is 0.75(default) |
| 31 | + * 3. HashSet hs= new HashSet(int initialSize, float fillRatio);//size and fill ratio can be declared |
| 32 | + * 4. HashSet hs= new HashSet(Collection c);// creates a HashSet for any given Collection (Acts as interconversion) |
| 33 | + */ |
| 34 | + |
| 35 | + public void basicExamplesHashSet(){ |
| 36 | + HashSet<String>hashSet= new HashSet<String>(); |
| 37 | + hashSet.add("A"); |
| 38 | + hashSet.add("D"); |
| 39 | + hashSet.add("E"); |
| 40 | + hashSet.add("F"); |
| 41 | + hashSet.add("A"); // the return type of add() is boolean. Since A is already there it will return false and wont add |
| 42 | + hashSet.add(null); |
| 43 | + //We have no control on insertion order |
| 44 | + System.out.println("Contents of the HashSet :"+ hashSet); |
| 45 | + } |
| 46 | + |
| 47 | + /* |
| 48 | + * Iterate using Iterator |
| 49 | + */ |
| 50 | + |
| 51 | + public void iterateUsingIterator(){ |
| 52 | + HashSet<String>hashSet= new HashSet<String>(); |
| 53 | + hashSet.add("A"); |
| 54 | + hashSet.add("D"); |
| 55 | + hashSet.add("E"); |
| 56 | + hashSet.add("F"); |
| 57 | + hashSet.add("A"); |
| 58 | + |
| 59 | + Iterator< String> iterator= hashSet.iterator(); |
| 60 | + while(iterator.hasNext()){ |
| 61 | + System.out.println("Elements of HashSet :"+ iterator.next()); |
| 62 | + } |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | + public static void main(String[] args) { |
| 68 | + // TODO Auto-generated method stub |
| 69 | + HashSetExample hashSetExample= new HashSetExample(); |
| 70 | + hashSetExample.basicExamplesHashSet(); |
| 71 | + hashSetExample.iterateUsingIterator(); |
| 72 | + } |
| 73 | + |
| 74 | +} |
0 commit comments