|
| 1 | +import java.util.Arrays; |
| 2 | +import java.util.Collections; |
| 3 | +import java.util.Iterator; |
| 4 | +import java.util.LinkedHashSet; |
| 5 | +import java.util.Set; |
| 6 | + |
| 7 | +/* |
| 8 | + * A set is an unordered collection of elements allowing no duplicates, |
| 9 | + * i.e. all the elements in a set are unique. |
| 10 | + */ |
| 11 | + |
| 12 | +public class LinkedHashSet_Learn { |
| 13 | + |
| 14 | + public static void main(String[] args) { |
| 15 | + |
| 16 | + /* |
| 17 | + * The 'Set' interface extends 'Collection' interface and specifies the |
| 18 | + * behaviour of a collection that does not allow duplicate elements. |
| 19 | + * |
| 20 | + * The 'LinkedHashSet' class extends the 'HashSet' class and adds no members of |
| 21 | + * its own. It maintains a linked list of the entries in the set, in the order |
| 22 | + * in which they were inserted. Thus iterating over the set elements are done as |
| 23 | + * per insertion order of elements. |
| 24 | + */ |
| 25 | + |
| 26 | + Set<Integer> demoSet = new LinkedHashSet<>(); |
| 27 | + |
| 28 | + /* |
| 29 | + * Adding elements to LinkedHashSet |
| 30 | + * |
| 31 | + * boolean add(E obj) : Declared in the Collection interface. Adds object to the |
| 32 | + * collection. Returns true if object was added to the collection. Returns false |
| 33 | + * if obj is already a member of the collection and the collection does not |
| 34 | + * allow duplicates. |
| 35 | + * |
| 36 | + * boolean addAll(Collection c) : Declared in the Collection interface. Adds all |
| 37 | + * the elements of c to the invoking collection. Returns true if elements were |
| 38 | + * added to the invoking collection. Otherwise, returns false. |
| 39 | + */ |
| 40 | + |
| 41 | + demoSet.add(7); |
| 42 | + // demoSet = [7] |
| 43 | + |
| 44 | + demoSet.add(5); |
| 45 | + // demoSet = [7, 5] |
| 46 | + |
| 47 | + demoSet.add(3); |
| 48 | + // demoSet = [7, 5, 3] |
| 49 | + |
| 50 | + demoSet.add(9); |
| 51 | + // demoSet = [7, 5, 3, 9] |
| 52 | + |
| 53 | + demoSet.add(5); // 5 already exists in demoSet hence not added |
| 54 | + // demoSet = [7, 5, 3, 9] |
| 55 | + |
| 56 | + if (demoSet.add(3)) // 3 already exists in demoSet hence not added |
| 57 | + System.out.println("Element added to set"); |
| 58 | + else |
| 59 | + System.out.println("Duplicate element cannot be added to set"); |
| 60 | + |
| 61 | + System.out.println("demoSet = " + demoSet); // demoSet = [7, 5, 3, 9] |
| 62 | + |
| 63 | + Set<Integer> numsSet = new LinkedHashSet<>(Arrays.asList(4, 7, 3, 6, 8)); |
| 64 | + // numsSet = [3, 4, 6, 7, 8] |
| 65 | + |
| 66 | + numsSet.addAll(Arrays.asList(5, 7, 2, 8, 1, 3)); // duplicate elements are not added |
| 67 | + // numsSet = [1, 2, 3, 4, 5, 6, 7, 8] |
| 68 | + |
| 69 | + System.out.println("numsSet = " + numsSet); // numsSet = [1, 2, 3, 4, 5, 6, 7, 8] |
| 70 | + |
| 71 | + /* |
| 72 | + * Removing elements from LinkedHashSet |
| 73 | + * |
| 74 | + * boolean remove(Object obj) : Declared in the Collection interface. Removes |
| 75 | + * one instance of obj from the invoking collection. Returns true if the element |
| 76 | + * was removed. Otherwise, returns false. |
| 77 | + * |
| 78 | + * boolean removeAll(Collection c) : Declared in the Collection interface. |
| 79 | + * Removes all elements of c from the invoking collection. Returns true if |
| 80 | + * elements were removed from the invoking collection. Otherwise, returns false. |
| 81 | + * |
| 82 | + * boolean retainAll(Collection c) : Declared in the Collection interface. |
| 83 | + * Removes all elements from the invoking collection except those in c. Returns |
| 84 | + * true if elements were removed from the invoking collection. Otherwise, |
| 85 | + * returns false. |
| 86 | + */ |
| 87 | + |
| 88 | + // demoSet = [7, 5, 3, 9] |
| 89 | + demoSet.remove(7); |
| 90 | + // demoSet = [5, 3, 9] |
| 91 | + |
| 92 | + System.out.println("demoSet = " + demoSet); // demoSet = [5, 3, 9] |
| 93 | + |
| 94 | + // numsSet = [1, 2, 3, 4, 5, 6, 7, 8] |
| 95 | + numsSet.removeAll(Arrays.asList(1, 3, 5, 7)); |
| 96 | + // numsSet = [2, 4, 6, 8] |
| 97 | + |
| 98 | + System.out.println("numsSet = " + numsSet); // numsSet = [2, 4, 6, 8] |
| 99 | + |
| 100 | + // numsSet = [2, 4, 6, 8] |
| 101 | + numsSet.retainAll(Arrays.asList(4, 8)); |
| 102 | + // numsSet = [4, 8] |
| 103 | + |
| 104 | + System.out.println("numsSet = " + numsSet); // numsSet = [4, 8] |
| 105 | + |
| 106 | + /* |
| 107 | + * Get the count of elements present in the LinkedHashSet |
| 108 | + * |
| 109 | + * int size() : Declared in the Collection interface. Returns the number of |
| 110 | + * elements held in the invoking collection. |
| 111 | + */ |
| 112 | + |
| 113 | + // demoSet = [5, 3, 9] |
| 114 | + int setSize = demoSet.size(); |
| 115 | + |
| 116 | + System.out.println("Size = " + setSize); // Size = 3 |
| 117 | + |
| 118 | + /* |
| 119 | + * Check if LinkedHashSet is empty or not |
| 120 | + * |
| 121 | + * boolean isEmpty() : Declared in the Collection interface. Returns true if the |
| 122 | + * invoking collection is empty. Otherwise, returns false. |
| 123 | + */ |
| 124 | + |
| 125 | + // demoSet = [5, 3, 9] |
| 126 | + if (demoSet.isEmpty()) |
| 127 | + System.out.println("Set is empty !"); |
| 128 | + else |
| 129 | + System.out.println("Set is not empty !"); |
| 130 | + |
| 131 | + /* |
| 132 | + * Check if an object is present the LinkedHashSet |
| 133 | + * |
| 134 | + * boolean contains(Object obj) : Declared in the Collection interface. Returns |
| 135 | + * true if obj is an element of the invoking collection. Otherwise, returns |
| 136 | + * false. |
| 137 | + */ |
| 138 | + |
| 139 | + int value = 9; |
| 140 | + |
| 141 | + // demoSet = [5, 3, 9] |
| 142 | + if (demoSet.contains(value)) |
| 143 | + System.out.println("Set contains " + value); |
| 144 | + else |
| 145 | + System.out.println("Set does not contain " + value); |
| 146 | + |
| 147 | + /* |
| 148 | + * Clear the LinkedHashSet |
| 149 | + * |
| 150 | + * void clear() : Declared in the Collection interface. Removes all elements |
| 151 | + * from the invoking collection. |
| 152 | + */ |
| 153 | + |
| 154 | + // demoSet = [5, 3, 9] |
| 155 | + demoSet.clear(); |
| 156 | + // demoSet = [] |
| 157 | + |
| 158 | + System.out.println("demoSet = " + demoSet); // demoSet = [] |
| 159 | + |
| 160 | + /* |
| 161 | + * Construct LinkedHashSet from array |
| 162 | + */ |
| 163 | + |
| 164 | + String fruits[] = { "apple", "grape", "banana", "orange", "grape" }; |
| 165 | + |
| 166 | + LinkedHashSet<String> fruitSet = new LinkedHashSet<>(); |
| 167 | + |
| 168 | + Collections.addAll(fruitSet, fruits); |
| 169 | + |
| 170 | + System.out.println("fruitSet = " + fruitSet); // fruitSet = [apple, grape, banana, orange] |
| 171 | + |
| 172 | + /* |
| 173 | + * Construct array from LinkedHashSet |
| 174 | + */ |
| 175 | + |
| 176 | + String fruitArr[] = fruitSet.toArray(new String[fruitSet.size()]); |
| 177 | + |
| 178 | + System.out.println("fruitArr = " + Arrays.toString(fruitArr)); // fruitArr = [apple, grape, banana, orange] |
| 179 | + |
| 180 | + /* |
| 181 | + * Iterating over the contents of a LinkedHashSet |
| 182 | + * |
| 183 | + * Iterator<E> iterator() : Declared in the Collection interface. Returns an |
| 184 | + * iterator for the invoking collection. |
| 185 | + */ |
| 186 | + |
| 187 | + Iterator itr = fruitSet.iterator(); |
| 188 | + |
| 189 | + while (itr.hasNext()) { |
| 190 | + System.out.print(itr.next() + " "); |
| 191 | + } |
| 192 | + |
| 193 | + System.out.println(); |
| 194 | + |
| 195 | + // Iterate using for-each loop |
| 196 | + |
| 197 | + for (String fruit : fruitSet) { |
| 198 | + System.out.print(fruit + " "); |
| 199 | + } |
| 200 | + |
| 201 | + } |
| 202 | + |
| 203 | +} |
0 commit comments