阅读更多
About parallelStream collect results. Please Use the collect method. The collect method is concurrent secure.
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;
import org.junit.Test;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class ParallelStreamTestcase {
@Test
public void test0Error() {
int total = 100000;
List source = new ArrayList<>(total);
for (int i = 0; i < total; i++) {
source.add(i);
}
List target = new ArrayList<>();
source.parallelStream().forEach(iterator -> target.add(iterator));
assertEquals(total, source.size());
assertEquals(total, target.size());
}
@Test
public void test1Error() {
int total = 100000;
List source = new ArrayList<>(total);
for (int i = 0; i < total; i++) {
source.add(i);
}
HashSet target = new HashSet<>();
source.parallelStream().forEach(iterator -> target.add(iterator));
assertEquals(total, source.size());
assertEquals(total, target.size());
}
@Test
public void test2Error() {
int total = 100000;
List source = new ArrayList<>(total);
for (int i = 0; i < total; i++) {
source.add(i);
}
HashMap target = new HashMap<>();
source.parallelStream().forEach(iterator -> target.put(iterator, iterator));
assertEquals(total, source.size());
assertEquals(total, target.size());
}
@Test
public void test00() {
int total = 100000;
List source = new ArrayList<>(total);
for (int i = 0; i < total; i++) {
source.add(i);
}
List targetList = source.parallelStream().collect(Collectors.toList());
assertEquals(total, source.size());
assertEquals(total, targetList.size());
Set targetSet = source.parallelStream().collect(Collectors.toSet());
assertEquals(total, targetSet.size());
Set targetTreeSet = source.parallelStream().collect(Collectors.toCollection(() -> new TreeSet<>()));
assertEquals(total, targetTreeSet.size());
LinkedList targetLinkedList = source.parallelStream().collect(Collectors.toCollection(() -> new LinkedList<>()));
assertEquals(total, targetLinkedList.size());
}
@Test
public void test22() {
int total = 100000;
List source = new ArrayList<>(total);
long sum = 0;
for (int i = 0; i < total; i++) {
sum += i;
source.add(i);
}
log.debug("sum:{}", sum);
System.err.println(String.format("sum:{%d}", sum));
Map target = source.parallelStream().collect(Collectors.toMap(iterator -> iterator, iterator -> iterator));
long targetSum = 0;
for (Integer value : target.values()) {
targetSum += value;
}
log.debug("targetSum:{}", targetSum);
System.err.println(String.format("targetSum:{%d}", targetSum));
assertEquals(sum, targetSum);
assertEquals(total, source.size());
assertEquals(total, target.size());
Map target2 = source.parallelStream().collect(Collectors.toMap(iterator -> iterator % 1000, iterator -> iterator, (t, u) -> t + u));
long target2Sum = 0;
for (Integer value : target2.values()) {
target2Sum += value;
}
log.debug("target2Sum:{}", target2Sum);
System.err.println(String.format("target2Sum:{%d}", target2Sum));
assertEquals(sum, target2Sum);
TreeMap target3 = source.parallelStream().collect(Collectors.toMap(iterator -> iterator % 1000, iterator -> iterator, (t, u) -> t + u, TreeMap::new));
long target3Sum = 0;
for (Integer value : target3.values()) {
target3Sum += value;
}
log.debug("target3Sum:{}", target3Sum);
System.err.println(String.format("target3Sum:{%d}", target3Sum));
assertEquals(sum, target3Sum);
boolean duplicateKeyExceptionForParallelStream = false;
try {
source.parallelStream().collect(Collectors.toMap(iterator -> iterator % 1000, iterator -> iterator));
} catch (IllegalStateException e) {
duplicateKeyExceptionForParallelStream = true;
}
assertTrue(duplicateKeyExceptionForParallelStream);
boolean duplicateKeyExceptionForStream = false;
try {
source.parallelStream().collect(Collectors.toMap(iterator -> iterator % 1000, iterator -> iterator));
} catch (IllegalStateException e) {
duplicateKeyExceptionForStream = true;
}
assertTrue(duplicateKeyExceptionForStream);
}
@Test
public void test222() {
int total = 100000;
List source = new ArrayList<>(total);
long sum = 0;
for (int i = 0; i < total; i++) {
sum += i;
source.add(i);
}
log.debug("sum:{}", sum);
System.err.println(String.format("sum:{%d}", sum));
Map target = source.parallelStream().collect(Collectors.toConcurrentMap(iterator -> iterator, iterator -> iterator));
long targetSum = 0;
for (Integer value : target.values()) {
targetSum += value;
}
log.debug("targetSum:{}", targetSum);
System.err.println(String.format("targetSum:{%d}", targetSum));
assertEquals(sum, targetSum);
assertEquals(total, source.size());
assertEquals(total, target.size());
Map target2 = source.parallelStream().collect(Collectors.toConcurrentMap(iterator -> iterator % 1000, iterator -> iterator, (t, u) -> t + u));
long target2Sum = 0;
for (Integer value : target2.values()) {
target2Sum += value;
}
log.debug("target2Sum:{}", target2Sum);
System.err.println(String.format("target2Sum:{%d}", target2Sum));
assertEquals(sum, target2Sum);
ConcurrentMap target3 = source.parallelStream()
.collect(Collectors.toConcurrentMap(iterator -> iterator % 1000, iterator -> iterator, (t, u) -> t + u, ConcurrentHashMap::new));
long target3Sum = 0;
for (Integer value : target3.values()) {
target3Sum += value;
}
log.debug("target3Sum:{}", target3Sum);
System.err.println(String.format("target3Sum:{%d}", target3Sum));
assertEquals(sum, target3Sum);
boolean duplicateKeyExceptionForParallelStream = false;
try {
source.parallelStream().collect(Collectors.toConcurrentMap(iterator -> iterator % 1000, iterator -> iterator));
} catch (IllegalStateException e) {
duplicateKeyExceptionForParallelStream = true;
}
assertTrue(duplicateKeyExceptionForParallelStream);
boolean duplicateKeyExceptionForStream = false;
try {
source.parallelStream().collect(Collectors.toConcurrentMap(iterator -> iterator % 1000, iterator -> iterator));
} catch (IllegalStateException e) {
duplicateKeyExceptionForStream = true;
}
assertTrue(duplicateKeyExceptionForStream);
}
@Test
public void test2222() {
int total = 100000;
List source = new ArrayList<>(total);
long sum = 0;
for (int i = 0; i < total; i++) {
sum += i;
source.add(i);
}
log.debug("sum:{}", sum);
System.err.println(String.format("sum:{%d}", sum));
Map target = source.parallelStream().collect(Collectors.toConcurrentMap(iterator -> iterator, iterator -> iterator));
long targetSum = 0;
for (Integer value : target.values()) {
targetSum += value;
}
log.debug("targetSum:{}", targetSum);
System.err.println(String.format("targetSum:{%d}", targetSum));
assertEquals(total, source.size());
assertEquals(total, target.size());
assertEquals(sum, targetSum);
Map target2 = source.parallelStream().collect(Collectors.toConcurrentMap(iterator -> iterator % 1000, iterator -> iterator, (t, u) -> t + u));
long target2Sum = 0;
for (Integer value : target2.values()) {
target2Sum += value;
}
log.debug("target2Sum:{}", target2Sum);
System.err.println(String.format("target2Sum:{%d}", target2Sum));
assertEquals(sum, target2Sum);
ConcurrentMap target3 = source.parallelStream()
.collect(Collectors.toConcurrentMap(iterator -> iterator % 1000, iterator -> iterator, (t, u) -> t + u, ConcurrentHashMap::new));
long target3Sum = 0;
for (Integer value : target3.values()) {
target3Sum += value;
}
log.debug("target3Sum:{}", target3Sum);
System.err.println(String.format("target3Sum:{%d}", target3Sum));
assertEquals(sum, target3Sum);
boolean duplicateKeyExceptionForParallelStream = false;
try {
source.parallelStream().collect(Collectors.toConcurrentMap(iterator -> iterator % 1000, iterator -> iterator));
} catch (IllegalStateException e) {
duplicateKeyExceptionForParallelStream = true;
}
assertTrue(duplicateKeyExceptionForParallelStream);
boolean duplicateKeyExceptionForStream = false;
try {
source.parallelStream().collect(Collectors.toConcurrentMap(iterator -> iterator % 1000, iterator -> iterator));
} catch (IllegalStateException e) {
duplicateKeyExceptionForStream = true;
}
assertTrue(duplicateKeyExceptionForStream);
}
}