addIfAbsent ( )源码实例Demo

java.util.concurrent.CopyOnWriteArrayList#addIfAbsent ( )源码实例Demo

下面列出了java.util.concurrent.CopyOnWriteArrayList#addIfAbsent ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

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);
}
 
源代码2 项目: Java-9-Cookbook   文件: Chapter07Concurrency02.java
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);
}
 
@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());
}
 
源代码4 项目: openjdk-jdk9   文件: CopyOnWriteArrayListTest.java
/**
 * 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());
}
 
源代码5 项目: openjdk-jdk9   文件: CopyOnWriteArrayListTest.java
/**
 * 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));
}
 
源代码6 项目: j2objc   文件: CopyOnWriteArrayListTest.java
/**
 * 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());
}
 
源代码7 项目: j2objc   文件: CopyOnWriteArrayListTest.java
/**
 * 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));
}
 
源代码8 项目: algorithms   文件: BinaryGapTest.java
@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();
}