import lombok.extern.slf4j.Slf4j;
import java.util.*;
import java.util.stream.Collectors;
@Slf4j(topic = "c.JStream")
public class JStream {
public static void main(String[] args) {
log.info("------------------------------------------------------------");
log.info("使用Java7");
List<String> strings = Arrays.asList("abc","","bc","efg","abc","","jk1");
log.info("列表: {}", strings.toArray());
long count = getCountEmptyStringJava7(strings);
log.info("空字符串数量: {}", count);
count = getCountLengthJava7(strings);
log.info("字符串长度为3的数量为: {}", count);
List<String> filtered = deleteEmptyStringJava7(strings);
log.info("筛选后的列表: {}", filtered.toArray());
String mergedString = getMergedStringJava7(strings, ",");
log.info("合并字符串:{}", mergedString);
List<Integer> numbers = Arrays.asList(3,2,2,3,7,3,5);
List<Integer> squaresList = getSquares(numbers);
log.info("平方数列表: {}", squaresList.toArray());
List<Integer> integers = Arrays.asList(1,2,13,4,15,6,17,8,19);
log.info("列表:{}, 列表中最大的数:{}, 最小的数:{}, 所有数之和:{}, 平均数:{}, 随机数: ", integers.toArray(),
getMax(integers), getMin(integers), getSum(integers), getAverage(integers));
Random random = new Random();
for(int i=0;i<10;i++){
log.info("{}", random.nextInt());
}
log.info("------------------------------------------------------------");
log.info("使用Java8");
log.info("列表: {}", strings.toArray());
count = strings.stream().filter(String::isEmpty).count();
log.info("空字符串数量为: {}", count);
count = strings.stream().filter(str -> str.length() == 3).count();
log.info("字符串长度为3的数量: {}", count);
filtered = strings.stream().filter(str -> !str.isEmpty()).collect(Collectors.toList());
log.info("筛选后的列表: {}", filtered.toArray());
mergedString = strings.stream().filter(str -> !str.isEmpty()).collect(Collectors.joining(","));
log.info("使用逗号合并后的字符串: {}", mergedString);
squaresList = numbers.stream().map(i -> i^2).distinct().collect(Collectors.toList());
log.info("平方数列表: {}", squaresList.toArray());
IntSummaryStatistics statistics = integers.stream().mapToInt((x) -> x).summaryStatistics();
log.info("列表:{}, 列表中最大的数:{}, 最小的数:{}, 所有数之和:{}, 平均数:{}, 随机数: ", integers.toArray(),
statistics.getMax(), statistics.getMin(), statistics.getSum(), statistics.getAverage());
random.ints().limit(10).sorted().forEach(p -> log.info("{}", p));
count = strings.parallelStream().filter(String::isEmpty).count();
log.info("空字符串数量为: {}", count);
}
private static int getAverage(List<Integer> numbers) {
return getSum(numbers)/numbers.size();
}
private static int getSum(List<Integer> numbers) {
int sum = numbers.get(0);
for(int i=1;i < numbers.size();i++){
sum += numbers.get(i);
}
return sum;
}
private static int getMin(List<Integer> numbers) {
int min = numbers.get(0);
for(int i=1;i < numbers.size();i++){
Integer number = numbers.get(i);
if(number < min){
min = number;
}
}
return min;
}
private static int getMax(List<Integer> integers) {
int max = integers.get(0);
for(Integer i:integers){
if(i > max){
max = i;
}
}
return max;
}
private static List<Integer> getSquares(List<Integer> numbers) {
List<Integer> squaresList = new ArrayList<>();
for(Integer number:numbers){
Integer square = number ^ 2;
if(!squaresList.contains(square)){
squaresList.add(square);
}
}
return squaresList;
}
private static String getMergedStringJava7(List<String> list, String separator) {
StringBuilder builder = new StringBuilder();
for(String str:list){
if(!str.isEmpty()){
builder.append(str).append(separator);
}
}
String mergedString = builder.toString();
return mergedString.substring(0, mergedString.lastIndexOf(separator));
}
private static List<String> deleteEmptyStringJava7(List<String> list){
List<String> filteredList = new ArrayList<>(list.size());
for(String str:list){
if(!str.isEmpty()){
filteredList.add(str);
}
}
return filteredList;
}
private static long getCountLengthJava7(List<String> list){
int count = 0;
for(String str:list){
if(!str.isEmpty() && str.length() == 3){
count++;
}
}
return count;
}
private static long getCountEmptyStringJava7(List<String> list){
int count = 0;
for(String str:list){
if(str.isEmpty()){
count++;
}
}
return count;
}
}
输出:
2022-09-14 20:49:09 [main] c.JStream - ------------------------------------------------------------
2022-09-14 20:49:09 [main] c.JStream - 使用Java7
2022-09-14 20:49:09 [main] c.JStream - 列表: abc
2022-09-14 20:49:09 [main] c.JStream - 空字符串数量: 2
2022-09-14 20:49:09 [main] c.JStream - 字符串长度为3的数量为: 4
2022-09-14 20:49:09 [main] c.JStream - 筛选后的列表: abc
2022-09-14 20:49:09 [main] c.JStream - 合并字符串:abc,bc,efg,abc,jk1
2022-09-14 20:49:09 [main] c.JStream - 平方数列表: 1
2022-09-14 20:49:09 [main] c.JStream - 列表:[1, 2, 13, 4, 15, 6, 17, 8, 19], 列表中最大的数:19, 最小的数:1, 所有数之和:85, 平均数:9, 随机数:
2022-09-14 20:49:09 [main] c.JStream - 1761884609
2022-09-14 20:49:09 [main] c.JStream - 1816268500
2022-09-14 20:49:09 [main] c.JStream - 1046625388
2022-09-14 20:49:09 [main] c.JStream - -233711792
2022-09-14 20:49:09 [main] c.JStream - -1649647326
2022-09-14 20:49:09 [main] c.JStream - 983716357
2022-09-14 20:49:09 [main] c.JStream - -1930134793
2022-09-14 20:49:09 [main] c.JStream - -2141648801
2022-09-14 20:49:09 [main] c.JStream - -1946556390
2022-09-14 20:49:09 [main] c.JStream - 179283699
2022-09-14 20:49:09 [main] c.JStream - ------------------------------------------------------------
2022-09-14 20:49:09 [main] c.JStream - 使用Java8
2022-09-14 20:49:09 [main] c.JStream - 列表: abc
2022-09-14 20:49:09 [main] c.JStream - 空字符串数量为: 2
2022-09-14 20:49:09 [main] c.JStream - 字符串长度为3的数量: 4
2022-09-14 20:49:09 [main] c.JStream - 筛选后的列表: abc
2022-09-14 20:49:09 [main] c.JStream - 使用逗号合并后的字符串: abc,bc,efg,abc,jk1
2022-09-14 20:49:09 [main] c.JStream - 平方数列表: 1
2022-09-14 20:49:09 [main] c.JStream - 列表:[1, 2, 13, 4, 15, 6, 17, 8, 19], 列表中最大的数:19, 最小的数:1, 所有数之和:85, 平均数:9.444444444444445, 随机数:
2022-09-14 20:49:09 [main] c.JStream - -1985482875
2022-09-14 20:49:09 [main] c.JStream - -1639968910
2022-09-14 20:49:09 [main] c.JStream - -1435708960
2022-09-14 20:49:09 [main] c.JStream - -155473287
2022-09-14 20:49:09 [main] c.JStream - 902389493
2022-09-14 20:49:09 [main] c.JStream - 1035272612
2022-09-14 20:49:09 [main] c.JStream - 1079306024
2022-09-14 20:49:09 [main] c.JStream - 1197110298
2022-09-14 20:49:09 [main] c.JStream - 1913795074
2022-09-14 20:49:09 [main] c.JStream - 1973786199
2022-09-14 20:49:09 [main] c.JStream - 空字符串数量为: 2