栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java涓璫ollectors(Java中Collector)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java涓璫ollectors(Java中Collector)

用法:
直接上代码

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.*;
import java.util.function.BinaryOperator;
import java.util.stream.Collectors;
import java.util.stream.Stream;


public class TestCollector {
    private List doubleList;
    private List integerList;
    private List stringList;

    @DisplayName("测试返回不可变数组")
    @Test
    public void testToUnmodifiableList(){
        Stream stream = integerList.stream();
        //使用Collectors的toUnmodifiableList()方法
        List collect = stream.collect(Collectors.toUnmodifiableList());
        Assertions.assertThrows(Exception.class,()-> collect.add(1), "没有抛出异常");
        System.out.println("抛出了异常");
    }

    
    @DisplayName("测试返回平均值")
    @Test
    public void testAveragingDouble(){
        Stream stream = doubleList.stream();
        Stream integerStream = integerList.stream();
        //求double数组的平均值
        Double collect = stream.collect(Collectors.averagingDouble((a) -> a));
        System.out.println("double数组的平均值: " +collect);
        //把Integer转化为double再求平均值
        Double collect1 = integerStream.collect(Collectors.averagingDouble(Integer::doublevalue));
        System.out.println("integer数组的平均值: " + collect1);
    }

    
    @DisplayName("测试CollectingAndThen")
    @Test
    public void testCollectingAndThen(){
        Stream integerStream = integerList.stream();
        //把Integer转化为double再求平均值
        Boolean collect = integerStream.collect(Collectors.collectingAndThen(Collectors.averagingInt((a) -> a), (a) -> a > 5));
        testAveragingDouble();
        System.out.println("integer数组求平均值之后再判断是否>5: " + collect);
    }

    
    @DisplayName("测试Counting")
    @Test
    public void testCounting(){
        Stream integerStream = integerList.stream();
        Long collect = integerStream.collect(Collectors.counting());
        System.out.println("数组个数: " + collect);
    }

    
    @DisplayName("测试GroupBy")
    @Test
    public void testGroupBy(){
        Stream integerStream = integerList.stream();
        Map> collect = integerStream.collect(Collectors.groupingBy((t)->t > 5));
        System.out.println("根据是否>5分类");
        for (Object o : collect.entrySet()) {
            System.out.println(o);
        }
        integerStream = integerList.stream();
        Map collect1 = integerStream.collect(Collectors.groupingBy((t) -> t > 5, Collectors.counting()));
        System.out.println("获取大于5和不大于的5的数的个数");
        for (Object o : collect1.entrySet()) {
            System.out.println(o);
        }
    }

    
    @DisplayName("测试Joining")
    @Test
    public void testJoining(){
        Stream stringStream = stringList.stream();
        String collect = stringStream.collect(Collectors.joining());
        System.out.println("将所有字符串串联起来");
        System.out.println(collect);

        stringStream = stringList.stream();
        collect = stringStream.collect(Collectors.joining("-"));
        System.out.println("用-将所有字符串串联起来");
        System.out.println(collect);

        stringStream = stringList.stream();
        collect = stringStream.collect(Collectors.joining("-", "[", "]"));
        System.out.println("用-将所有字符串串并用[]将修饰他们联起来");
        System.out.println(collect);
    }

    
    @DisplayName("测试Mapping")
    @Test
    public void testMapping(){
        Stream stream = integerList.stream();
        //先把原来的数映射为两倍再求平均值
        Double collect = stream.collect(Collectors.mapping((t) -> t * 2, Collectors.averagingInt((t) -> t)));
        System.out.println(collect);
    }

    
    @DisplayName("测试MaxBy")
    @Test
    public void testMaxBy(){
        Stream stream = integerList.stream();
        Optional collect = stream.collect(Collectors.maxBy((a, b) -> a - b));
        System.out.println(collect.orElse(null));
    }

    
    @DisplayName("测试minBy")
    @Test
    public void testMinBy(){
        Stream stream = integerList.stream();
        Optional collect = stream.collect(Collectors.minBy((a, b) -> a - b));
        System.out.println(collect.orElse(null));
    }

    
    @DisplayName("测试PartitionBy")
    @Test
    public void testPartitionBy(){
        Stream stream = integerList.stream();
        Map> collect = stream.collect(Collectors.partitioningBy((t) -> t > 5));
        System.out.println(collect);
    }

    
    @DisplayName("测试Reducing")
    @Test
    public void testReducing(){
        Stream stream = integerList.stream();
        Optional collect = stream.collect(Collectors.reducing(BinaryOperator.minBy((a, b)->a - b)));
        System.out.println(collect.orElse(null));
    }

    
    @DisplayName("测试summarizingInt")
    @Test
    public void testSummarizingInt(){
        Stream stream = stringList.stream();
        IntSummaryStatistics collect = stream.collect(Collectors.summarizingInt((t) -> {
            switch (t) {
                case "zhangsan":
                    return 1;
                case "lisi":
                    return 2;
                case "wangwu":
                    return 3;
                default:
                    return 4;
            }
        }));
        double average = collect.getAverage();
        System.out.println(collect);
        System.out.println(average);
    }

    
    @DisplayName("测试SummingInt")
    @Test
    public void testSummingInt(){
        Stream stream = integerList.stream();
        Integer collect = stream.collect(Collectors.summingInt((t) -> t));
        System.out.println(collect);
    }

    
    @DisplayName("测试ToList")
    @Test
    public void testToList(){
        Stream stream = integerList.stream();
        List collect = stream.collect(Collectors.toList());
        System.out.println(collect);
    }


    
    @DisplayName("测试toMap")
    @Test
    public void testToMap(){
        Stream stream = integerList.stream();
        Map collect = stream.collect(Collectors.toMap((t) -> t*10, (t) -> t));
        System.out.println(collect);
    }

    
    @DisplayName("测试toSet")
    @Test
    public void testToSet(){
        Stream stream = integerList.stream();
        Set collect = stream.collect(Collectors.toSet());
        System.out.println(collect);
    }

    @DisplayName("----------------------------------分界线-------------------------------------------------")
    @BeforeEach
    public void getStringList(){
        List list = Arrays.asList("zhangsan", "lisi", "wangwu");
        System.out.print("字符串数组:[");
        this.stringList = list;
        for (String str: list){
            System.out.print(str+ " ");
        }
        System.out.print("]n");
    }
    @BeforeEach
    public void getIntegerList(){
        List list = new ArrayList<>();
        System.out.print("整型数组:[");
        for (int i = 0; i < 4; i++){
            int num = Math.abs(new Random().nextInt() % 10);
            list.add(num);
            System.out.print(num+ "  ");
        }
        this.integerList = list;
        System.out.print("]n");
    }
    @BeforeEach
    public void getDoubleList(){
        List list = new ArrayList<>();
        System.out.print("Double数组:[ ");
        for (int i = 0; i < 4; i++){
            double num = Math.abs(new Random().nextDouble() % 10);
            list.add(num);
            System.out.print(num+ "  ");
        }
        this.doubleList = list;
        System.out.print("]n");
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/776328.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号