Java8流示例

本文详细展示了Java 8 Stream API在处理列表、字符串和整数集合时的高效用法,包括计数空字符串、过滤、合并字符串及计算平均值等。通过对比Java 7 和 8 的示例,突出其简洁性和性能提升。
摘要由CSDN通过智能技术生成

Java8流示例

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

快乐江小鱼

知识创造财富,余额还是小数

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值