使用InStream的多线程方法向synchronizedList中插入若干个随机数,并计算其中重复的个数。
1 2 3 4 5 6 7 8 9 10
| List<Integer> numberList = Collections.synchronizedList(new ArrayList<>());
IntStream.range(0, 100).parallel().forEach(i -> { numberList.add(new Random().nextInt(100)); }); List<Integer> filterNumberList = numberList.stream().distinct().collect(Collectors.toList());
System.out.println("numberList.size = " + numberList.size()); System.out.println("filterNumberList.size = " + filterNumberList.size()); System.out.println("重复个数:" + (numberList.size() - filterNumberList.size()));
|
发布时间: 2021-08-12 14:23:24
更新时间: 2022-04-21 16:20:53
本文链接: https://wyatt.ink/posts/Code/cbb36818.html
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!