Java Code Examples for java.util.concurrent.CopyOnWriteArrayList#addIfAbsent()
The following examples show how to use
java.util.concurrent.CopyOnWriteArrayList#addIfAbsent() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Chapter07Concurrency02.java From Java-11-Cookbook-Second-Edition with MIT License | 5 votes |
private static void demo2_CopyOnWriteArrayList() { CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<>(Arrays.asList("Five", "Six", "Seven")); System.out.println(); System.out.println("list: " + list); System.out.println("Calling list.addIfAbsent(One)..."); list.addIfAbsent("One"); System.out.println("list: " + list); }
Example 2
Source File: Chapter07Concurrency02.java From Java-9-Cookbook with MIT License | 5 votes |
private static void demo2_CopyOnWriteArrayList() { CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<>(Arrays.asList("Five", "Six", "Seven")); System.out.println(); System.out.println("list: " + list); System.out.println("Calling list.addIfAbsent(One)..."); list.addIfAbsent("One"); System.out.println("list: " + list); }
Example 3
Source File: UnwrappedWeakReferenceTest.java From cloudhopper-commons with Apache License 2.0 | 5 votes |
@Test public void usage() throws Exception { Object o = new Object(); // verify WeakReference does not work correctly with CopyOnWriteArrayList WeakReference ref0 = new WeakReference(o); WeakReference ref1 = new WeakReference(o); CopyOnWriteArrayList<WeakReference> cowal = new CopyOnWriteArrayList<WeakReference>(); cowal.addIfAbsent(ref0); cowal.addIfAbsent(ref1); // if it didn't work, size is 2 Assert.assertEquals(2, cowal.size()); // use our impl now ref0 = new UnwrappedWeakReference(o); ref1 = new UnwrappedWeakReference(o); cowal = new CopyOnWriteArrayList<WeakReference>(); cowal.addIfAbsent(ref0); cowal.addIfAbsent(ref1); Assert.assertEquals(1, cowal.size()); Assert.assertTrue(cowal.contains(ref0)); cowal.remove(ref1); Assert.assertEquals(0, cowal.size()); }
Example 4
Source File: CopyOnWriteArrayListTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
/** * addIfAbsent will not add the element if it already exists in the list */ public void testAddIfAbsent() { CopyOnWriteArrayList full = populatedArray(SIZE); full.addIfAbsent(one); assertEquals(SIZE, full.size()); }
Example 5
Source File: CopyOnWriteArrayListTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
/** * addIfAbsent adds the element when it does not exist in the list */ public void testAddIfAbsent2() { CopyOnWriteArrayList full = populatedArray(SIZE); full.addIfAbsent(three); assertTrue(full.contains(three)); }
Example 6
Source File: CopyOnWriteArrayListTest.java From j2objc with Apache License 2.0 | 4 votes |
/** * addIfAbsent will not add the element if it already exists in the list */ public void testAddIfAbsent() { CopyOnWriteArrayList full = populatedArray(SIZE); full.addIfAbsent(one); assertEquals(SIZE, full.size()); }
Example 7
Source File: CopyOnWriteArrayListTest.java From j2objc with Apache License 2.0 | 4 votes |
/** * addIfAbsent adds the element when it does not exist in the list */ public void testAddIfAbsent2() { CopyOnWriteArrayList full = populatedArray(SIZE); full.addIfAbsent(three); assertTrue(full.contains(three)); }
Example 8
Source File: BinaryGapTest.java From algorithms with MIT License | 4 votes |
@Test public void collections() { Map<String, Integer> map1 = new HashMap<>(); map1.values(); map1.keySet(); map1.isEmpty(); map1.clear(); map1.merge("t", 5, (v1, v2) -> v1 +v2); System.out.println(map1.get("t")); map1.merge("t", 4, (v1, v2) -> v1 +v2); System.out.println(map1.get("t")); System.out.println(map1.computeIfAbsent("r", k -> 1)); System.out.println(map1.get("r")); map1.put("", 1); System.out.println(map1.computeIfPresent("", (k, v) -> v + 1)); System.out.println(map1.get("")); System.out.println(map1.getOrDefault("", 5)); map1.merge("", 5, (i, j) -> i + j); map1.merge("a", 5, (i, j) -> i + j); Set<Map.Entry<String, Integer>> entrySet = map1.entrySet(); System.out.println(map1.compute("", (k,i) -> i + 1)); System.out.println(map1.get("")); System.out.println(map1.get("a")); map1.putIfAbsent("d", 8); System.out.println(map1.replace("d", 9)); System.out.println(map1.get("d")); map1.containsKey("d"); map1.containsValue(8); map1.remove("d"); NavigableMap<Integer, Integer> map2 = new TreeMap<>(Comparator.naturalOrder()); map2.ceilingEntry(1); map2.ceilingKey(1); map2.floorEntry(1); map2.floorKey(1); map2.descendingKeySet(); map2.descendingMap(); map2.higherEntry(1); map2.higherKey(1); map2.lowerEntry(1); map2.lowerKey(1); map2.navigableKeySet(); map2.firstEntry(); map2.lastEntry(); map2.pollFirstEntry(); map2.pollLastEntry(); Set<Integer> set = new HashSet<>(); set.containsAll(new ArrayList<>()); set.contains(1); set.add(1); set.size(); set.clear(); set.isEmpty(); set.iterator(); set.remove(1); NavigableSet<Integer> set1 = new TreeSet<>(Comparator.naturalOrder()); set1.ceiling(1); set1.floor(1); set1.higher(1); set1.lower(1); set1.pollFirst(); set1.pollLast(); PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(Comparator.reverseOrder()); priorityQueue.add(4); priorityQueue.add(2); priorityQueue.offer(3); int a = priorityQueue.peek(); int b = priorityQueue.poll(); priorityQueue.remove(1); CopyOnWriteArrayList<Integer> copyList = new CopyOnWriteArrayList<>(); copyList.addIfAbsent(1); AtomicInteger atomicInteger = new AtomicInteger(0); int index = atomicInteger.getAndAccumulate(1, (i, j) -> (i+j) % 20); copyList.set(index, 2); copyList.iterator(); }