|
| 1 | +package ConcurrentSkipListSet; |
| 2 | + |
| 3 | +import java.util.Comparator; |
| 4 | +import java.util.Iterator; |
| 5 | +import java.util.Spliterator; |
| 6 | +import java.util.concurrent.ConcurrentSkipListSet; |
| 7 | + |
| 8 | +public class ConcurrentSkipListSet1 { |
| 9 | + |
| 10 | + public static void main(String[] args) { |
| 11 | + ConcurrentSkipListSet<Float> set = new ConcurrentSkipListSet<>(); |
| 12 | + |
| 13 | + // Add |
| 14 | + set.add(1.09f); |
| 15 | + set.add(2.89f); |
| 16 | + set.add(3.90f); |
| 17 | + set.add(4.98f); |
| 18 | + System.out.println(set); |
| 19 | + |
| 20 | + // ceiling |
| 21 | + |
| 22 | + System.out.println(set.ceiling(1.09f)); |
| 23 | + |
| 24 | + // floor |
| 25 | + System.out.println(set.floor(2.89f)); |
| 26 | + |
| 27 | + ConcurrentSkipListSet<Float> set1 = new ConcurrentSkipListSet<>(); |
| 28 | + set1.add(1.09f); |
| 29 | + set1.add(2.89f); |
| 30 | + set1.add(3.90f); |
| 31 | + set1.add(4.98f); |
| 32 | + // clear |
| 33 | + set.clear(); |
| 34 | + System.out.println(set); |
| 35 | + |
| 36 | + // clone |
| 37 | + ConcurrentSkipListSet<Float> clonedset = new ConcurrentSkipListSet<>(); |
| 38 | + clonedset = (ConcurrentSkipListSet<Float>) set1.clone(); |
| 39 | + System.out.println(clonedset); |
| 40 | + |
| 41 | + // comparator |
| 42 | + ConcurrentSkipListSet<Float> set2 = new ConcurrentSkipListSet<>(new ConcurrentSkipListSetComparator()); |
| 43 | + ConcurrentSkipListSet<Float> set3 = new ConcurrentSkipListSet<>(new ConcurrentSkipListSetComparator()); |
| 44 | + set2.add(10.58f); |
| 45 | + set2.add(20.78f); |
| 46 | + set2.add(30.65f); |
| 47 | + set2.add(10.47f); |
| 48 | + set2.add(50.98f); |
| 49 | + set2.add(60.65f); |
| 50 | + set3.add(10.55f); |
| 51 | + set3.add(20.78f); |
| 52 | + set3.add(34.65f); |
| 53 | + set3.add(45.78f); |
| 54 | + set3.add(57.98f); |
| 55 | + set3.add(99.65f); |
| 56 | + |
| 57 | + System.out.println(set2.comparator().compare(99.65f, 10.58f)); |
| 58 | + System.out.println(set2.comparator().compare(20.78f, 20.78f)); |
| 59 | + |
| 60 | + // contains |
| 61 | + |
| 62 | + System.out.println(set1.contains(1.09f)); |
| 63 | + |
| 64 | + // descendingIterator |
| 65 | + Iterator<Float> iterator = set1.descendingIterator(); |
| 66 | + while (iterator.hasNext()) { |
| 67 | + System.out.println(iterator.next()); |
| 68 | + } |
| 69 | + |
| 70 | + // descendingSet |
| 71 | + System.out.println(set1.descendingSet()); |
| 72 | + System.out.println(set1.descendingSet().descendingSet()); |
| 73 | + System.out.println(set1.descendingSet().descendingSet().descendingSet()); |
| 74 | + |
| 75 | + // equals |
| 76 | + System.out.println(set1.equals(set2)); |
| 77 | + |
| 78 | + // first |
| 79 | + |
| 80 | + System.out.println(set1.first()); |
| 81 | + |
| 82 | + // headSet |
| 83 | + System.out.println(set1.headSet(2.89f)); |
| 84 | + System.out.println(set1.headSet(2.89f, true)); |
| 85 | + System.out.println(set1.headSet(2.89f, false)); |
| 86 | + |
| 87 | + // higher |
| 88 | + System.out.println(set1.higher(2.89f)); |
| 89 | + |
| 90 | + // isEmpty |
| 91 | + System.out.println(set1.isEmpty()); |
| 92 | + System.out.println(set.isEmpty()); |
| 93 | + |
| 94 | + // last |
| 95 | + System.out.println(set1.last()); |
| 96 | + |
| 97 | + // lower |
| 98 | + System.out.println(set1.lower(2.89f)); |
| 99 | + |
| 100 | + // pollFirst |
| 101 | + System.out.println(set1.pollFirst()); |
| 102 | + |
| 103 | + // pollLast |
| 104 | + System.out.println(set1.pollLast()); |
| 105 | + |
| 106 | + // remove |
| 107 | + set1.remove(1.09f); |
| 108 | + System.out.println(set1); |
| 109 | + |
| 110 | + // removeAll |
| 111 | + clonedset.removeAll(set1); |
| 112 | + System.out.println(clonedset); |
| 113 | + |
| 114 | + // size |
| 115 | + System.out.println(set1.size()); |
| 116 | + |
| 117 | + // spliterator |
| 118 | + |
| 119 | + set1.spliterator().forEachRemaining((Float s) -> { |
| 120 | + System.out.println(s); |
| 121 | + }); |
| 122 | + |
| 123 | + System.out.println(set1.spliterator().estimateSize()); |
| 124 | + System.out.println(set1.spliterator().getExactSizeIfKnown()); |
| 125 | + System.out.println(set1.spliterator().hasCharacteristics(Spliterator.CONCURRENT)); |
| 126 | + |
| 127 | + //....And it runs all functions of Spliterator interface |
| 128 | + |
| 129 | + // subSet |
| 130 | + set1.add(4.98f); |
| 131 | + System.out.println(set1.subSet(2.89f, 4.98f)); |
| 132 | + System.out.println(set1.subSet(2.89f,true ,4.98f, true)); |
| 133 | + |
| 134 | + //tailset |
| 135 | + System.out.println(set1.tailSet(2.89f)); |
| 136 | + System.out.println(set1.tailSet(2.89f, true)); |
| 137 | + System.out.println(set1.tailSet(2.89f, false)); |
| 138 | + |
| 139 | + |
| 140 | + } |
| 141 | + |
| 142 | +} |
| 143 | + |
| 144 | +// comparator |
| 145 | + |
| 146 | +class ConcurrentSkipListSetComparator implements Comparator<Float> { |
| 147 | + |
| 148 | + public int compare(Float obj1, Float obj2) { |
| 149 | + |
| 150 | + if (((Float) obj1).equals((Float) obj2)) { |
| 151 | + return -1; |
| 152 | + } |
| 153 | + |
| 154 | + // using compareTo() to ensure |
| 155 | + return obj2.compareTo(obj2); |
| 156 | + } |
| 157 | + |
| 158 | +} |
0 commit comments