Table of Contents
ToggleJava CopyOnWriteArrayList Class
實作了 List 接口,是 ArrayList 的增強版本 ,其中所有修改像是:新增、修改、刪除等,都是透過建立新副本來實現的,位於 java.util.concurrent 套件中,是為在並發環境中使用而建立的資料結構,使用起來成本很高,因為每次更新操作都會建立一個新的副本,因此,如果頻繁操作是讀取操作,那麼是最好的選擇,CopyOnWriteArrayList Class 介紹常見的 contains 、 indexOf 、 sort 、 stream 、 subList 等方法,了解陣列列表的不同操作和方法,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- list
| +- copyonwritearraylist
| +- CopyOnWriteArrayListClassTest.java
單元測試
CopyOnWriteArrayList Class 提供檢查包含、搜索指定、排序、串流操作列表中的元素。
contains
建立一個 CopyOnWriteArrayList ,增加三個元素,檢查包含指定的元素。
@Test
public void contains() {
List<String> list = new CopyOnWriteArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
boolean contains = list.contains("Apple");
System.out.println(contains);
assertTrue(contains);
contains = list.contains("Grape");
System.out.println(contains);
assertFalse(contains);
}
true
false
containsAll
CopyOnWriteArrayList Class Java 建立一個 CopyOnWriteArrayList ,內有三個元素,,檢查包含所有指定的元素。
@Test
public void containsAll() {
List<String> list = new CopyOnWriteArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
List<String> list2 = new CopyOnWriteArrayList<>();
list2.add("Apple");
list2.add("Banana");
boolean contains = list.containsAll(list2);
System.out.println(contains);
assertTrue(contains);
contains = list2.containsAll(list);
System.out.println(contains);
assertFalse(contains);
}
true
false
indexOf
CopyOnWriteArrayList Class Java 建立一個 CopyOnWriteArrayList ,內有三個元素,搜索指定元素,傳回索引。
@Test
public void indexOf() {
int expected = 1;
List<String> list = new CopyOnWriteArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
int index = list.indexOf("Banana");
System.out.println(index);
assertEquals(expected, index);
index = list.indexOf("Grape");
System.out.println(index);
assertEquals(-1, index);
}
1
-1
lastIndexOf
CopyOnWriteArrayList Class Java 建立一個 CopyOnWriteArrayList ,從最後搜索指定元素,傳回索引。
@Test
public void lastIndexOf() {
int expected = 3;
List<String> list = new CopyOnWriteArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
list.add("Apple");
int index = list.lastIndexOf("Apple");
System.out.println(index);
assertEquals(expected, index);
index = list.indexOf("Grape");
System.out.println(index);
assertEquals(-1, index);
}
3
-1
sort
CopyOnWriteArrayList Class Java 建立一個 CopyOnWriteArrayList ,內有三個元素,排序元素。
@Test
public void sort() {
List<String> list = new CopyOnWriteArrayList<>();
list.add("Banana");
list.add("Apple");
list.add("Cherry");
list.sort(Comparator.naturalOrder());
System.out.println(list);
list.add("Grape");
list.sort(Comparator.reverseOrder());
System.out.println(list);
}
[Apple, Banana, Cherry]
[Grape, Cherry, Banana, Apple]
stream
CopyOnWriteArrayList Class in Java 建立一個 CopyOnWriteArrayList ,內有三個元素,執行串流,取得長度小於 6 的元素。
@Test
public void stream() {
int expectedSize = 1;
List<String> list = new CopyOnWriteArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
list = list.stream().filter(e -> e.length() < 6).collect(Collectors.toList());
System.out.println(list);
assertEquals(expectedSize, list.size());
}
[Apple]
parallelStream
CopyOnWriteArrayList Class in Java 建立一個 CopyOnWriteArrayList ,內有三個元素,並行執行串流。
@Test
public void parallelStream() {
List<String> list = new CopyOnWriteArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
list.parallelStream().forEach(System.out::println);
System.out.println("----------");
list.parallelStream().forEachOrdered(System.out::println);
}
Banana
Cherry
Apple
----------
Apple
Banana
Cherry
subList
CopyOnWriteArrayList Class in Java 建立一個 CopyOnWriteArrayList ,內有三個元素,建立指定位置的子列表 。
@Test
public void subList() {
int expectedSize = 2;
List<String> list = new CopyOnWriteArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
System.out.println(list);
List<String> list2 = list.subList(0, 2);
System.out.println(list2);
assertEquals(expectedSize, list2.size());
}
[Apple, Banana, Cherry]
[Apple, Banana]
replaceAll
CopyOnWriteArrayList Class in Java 建立一個 CopyOnWriteArrayList ,內有三個元素,替換所有元素 。
@Test
public void replaceAll() {
List<String> list = new CopyOnWriteArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
list.replaceAll(e -> e.toUpperCase());
System.out.println(list);
}
[APPLE, BANANA, CHERRY]
retainAll
Java CopyOnWriteArrayList Class Example 建立兩個 CopyOnWriteArrayList ,內各有三個元素,只保留相同元素 。
@Test
public void retainAll() {
int expectedSize = 1;
List<String> list = new CopyOnWriteArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
List<String> list2 = new CopyOnWriteArrayList<>();
list2.add("Apple");
list2.add("Lemon");
list2.add("Mango");
list.retainAll(list2);
System.out.println(list);
assertEquals(expectedSize, list.size());
}
[Apple]
CopyOnWriteArrayListClassTest.java
CopyOnWriteArrayList Class in Java 新增單元測試,驗證 Java CopyOnWriteArrayList Class Example 是否符合預期。
package org.ruoxue.java_147.list.copyonwritearraylist;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;
import org.junit.Test;
public class CopyOnWriteArrayListClassTest {
@Test
public void contains() {
List<String> list = new CopyOnWriteArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
boolean contains = list.contains("Apple");
System.out.println(contains);
assertTrue(contains);
contains = list.contains("Grape");
System.out.println(contains);
assertFalse(contains);
}
@Test
public void containsAll() {
List<String> list = new CopyOnWriteArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
List<String> list2 = new CopyOnWriteArrayList<>();
list2.add("Apple");
list2.add("Banana");
boolean contains = list.containsAll(list2);
System.out.println(contains);
assertTrue(contains);
contains = list2.containsAll(list);
System.out.println(contains);
assertFalse(contains);
}
@Test
public void indexOf() {
int expected = 1;
List<String> list = new CopyOnWriteArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
int index = list.indexOf("Banana");
System.out.println(index);
assertEquals(expected, index);
index = list.indexOf("Grape");
System.out.println(index);
assertEquals(-1, index);
}
@Test
public void lastIndexOf() {
int expected = 3;
List<String> list = new CopyOnWriteArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
list.add("Apple");
int index = list.lastIndexOf("Apple");
System.out.println(index);
assertEquals(expected, index);
index = list.indexOf("Grape");
System.out.println(index);
assertEquals(-1, index);
}
@Test
public void sort() {
List<String> list = new CopyOnWriteArrayList<>();
list.add("Banana");
list.add("Apple");
list.add("Cherry");
list.sort(Comparator.naturalOrder());
System.out.println(list);
list.add("Grape");
list.sort(Comparator.reverseOrder());
System.out.println(list);
}
@Test
public void stream() {
int expectedSize = 1;
List<String> list = new CopyOnWriteArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
list = list.stream().filter(e -> e.length() < 6).collect(Collectors.toList());
System.out.println(list);
assertEquals(expectedSize, list.size());
}
@Test
public void parallelStream() {
List<String> list = new CopyOnWriteArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
list.parallelStream().forEach(System.out::println);
System.out.println("----------");
list.parallelStream().forEachOrdered(System.out::println);
}
@Test
public void subList() {
int expectedSize = 2;
List<String> list = new CopyOnWriteArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
System.out.println(list);
List<String> list2 = list.subList(0, 2);
System.out.println(list2);
assertEquals(expectedSize, list2.size());
}
@Test
public void replaceAll() {
List<String> list = new CopyOnWriteArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
list.replaceAll(e -> e.toUpperCase());
System.out.println(list);
}
@Test
public void retainAll() {
int expectedSize = 1;
List<String> list = new CopyOnWriteArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
List<String> list2 = new CopyOnWriteArrayList<>();
list2.add("Apple");
list2.add("Lemon");
list2.add("Mango");
list.retainAll(list2);
System.out.println(list);
assertEquals(expectedSize, list.size());
}
}
心得分享
Java CopyOnWriteArrayList Class Example 使用場景,當讀多寫少,如:白名單,黑名單,商品類目的訪問和更新場景,因為寫的時候會複製新集合,此外當集合不大,因為寫的時候會複製新集合,最後即時性要求不高,因為有可能會讀取到舊的集合資料,提供了幾種 CopyOnWriteArrayList 常見方法的操作範例, CopyOnWriteArrayList Class in Java 在應用上相當廣泛,熟悉這些方法的操作,能夠提高開發效率,節省維護上的成本。