|
| 1 | +package learnCollections; |
| 2 | + |
| 3 | +import java.util.Collections; |
| 4 | +import java.util.HashSet; |
| 5 | +import java.util.LinkedHashSet; |
| 6 | +import java.util.NavigableSet; |
| 7 | +import java.util.Set; |
| 8 | +import java.util.TreeSet; |
| 9 | +import java.util.concurrent.ConcurrentSkipListSet; |
| 10 | + |
| 11 | +/* |
| 12 | +Set is a collection that can't contain duplicate elements. |
| 13 | +Faster operations |
| 14 | +Map -> HashMap, LinkediHashMap, TreeMap, EnumMap |
| 15 | +Set -> HashSet, LinkedHashSet, TreeSet, EnumSet |
| 16 | + */ |
| 17 | + |
| 18 | +public class SetOverview { |
| 19 | + public static void main(String[] args) { |
| 20 | + Set<Integer> set = new HashSet<>(); |
| 21 | + set.add(12); |
| 22 | + set.add(1); |
| 23 | + set.add(1); |
| 24 | + set.add(67); |
| 25 | + System.out.println(set); // [1, 67, 12] - no duplicates, order is not guaranteed |
| 26 | + |
| 27 | + |
| 28 | + // if wanna maintain insertion order, use LinkedHashSet |
| 29 | + Set<Integer> linkedHashSet = new LinkedHashSet<>(); |
| 30 | + linkedHashSet.add(12); |
| 31 | + linkedHashSet.add(1); |
| 32 | + linkedHashSet.add(1); |
| 33 | + linkedHashSet.add(67); |
| 34 | + System.out.println(linkedHashSet); // [12, 1, 67] - no duplicates, maintains insertion order |
| 35 | + |
| 36 | + |
| 37 | + // if wanna maintain natural order, use TreeSet |
| 38 | + Set<Integer> treeSet = new TreeSet<>(); |
| 39 | + treeSet.add(12); |
| 40 | + treeSet.add(1); |
| 41 | + treeSet.add(1); |
| 42 | + treeSet.add(67); |
| 43 | + System.out.println(treeSet); // [1, 12, 67] - no duplicates, maintains natural order |
| 44 | + |
| 45 | + |
| 46 | + NavigableSet<Integer> navigableSet = new TreeSet<>(); |
| 47 | + navigableSet.add(12); |
| 48 | + navigableSet.add(1); |
| 49 | + navigableSet.add(1); |
| 50 | + navigableSet.add(67); |
| 51 | + System.out.println(navigableSet.contains(67)); // true |
| 52 | + System.out.println(navigableSet.higher(1)); // returns the least element strictly > than the given element, or null if there is no such element. Output: 12 |
| 53 | + System.out.println(navigableSet.lower(12)); // returns the greatest element strictly < than the given element, or null if there is no such element. Output: 1 |
| 54 | + System.out.println(navigableSet.ceiling(1)); // returns the least element >= than the given element, or null if there is no such element. Output: 1 |
| 55 | + System.out.println(navigableSet.floor(12)); // returns the greatest element <= than the given element, or null if there is no such element. Output: 12 |
| 56 | + navigableSet.clear(); // removes all elements from the set |
| 57 | + |
| 58 | + |
| 59 | + // for thread safety |
| 60 | + Set<Integer> integers = Collections.synchronizedSet(set); // not recommended for concurrent access, better to use ConcurrentSkipListSet |
| 61 | + Set<Integer> concurrentSkipListSet = new ConcurrentSkipListSet<>(); |
| 62 | + |
| 63 | + // unmodifiable set |
| 64 | + Set<Integer> unmodifiableSet = Set.of(1, 2, 3, 4); // > 10 entries allowed unlike Map.of() |
| 65 | + Collections.unmodifiableSet(treeSet); // creates an unmodifiable view of the original set |
| 66 | + } |
| 67 | +} |
0 commit comments