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

java中stream流的用法(java中stream流去重)

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

java中stream流的用法(java中stream流去重)

用法:
直接上代码

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.Stream;


public class TestStream {
    private List doubleList;
    private List integerList;
    private List stringList;
    
    @DisplayName("测试AllMatch的作用")
    @Test
    public void testAllMatch(){
        Stream integerStream = integerList.stream();
        boolean b = integerStream.allMatch((e) -> e == 1);
        System.out.println("是否都等于1:"+ b);

        integerStream = integerList.stream();
        boolean b1 = integerStream.allMatch((e) -> e > 0);
        System.out.println("是否都大于0:"+b1);
    }

    
    @DisplayName("测试AnyMath")
    @Test
    public void testAnyMath(){
        Stream integerStream = integerList.stream();
        boolean b = integerStream.anyMatch((e) -> e == 1);
        System.out.println("是有一个等于1:"+ b);

        integerStream = integerList.stream();
        boolean b1 = integerStream.anyMatch((e) -> e > 0);
        System.out.println("是有一个大于0:"+b1);
    }

    
    @DisplayName("测试Concat")
    @Test
    public void testConcat(){
        Stream integerStream = integerList.stream();
        Stream integerStream1 = integerList.stream();
        Stream concat = Stream.concat(integerStream, integerStream1);
        List integers = concat.toList();
        System.out.println(integers);
    }

    
    @DisplayName("测试count")
    @Test
    public void testCount(){
        Stream stream = integerList.stream();
        long count = stream.count();
        System.out.println(count);
    }

    
    @DisplayName("测试distinct")
    @Test
    public void testDistinct(){
        Stream stream = integerList.stream();
        Stream distinct = stream.distinct();
        List integers = distinct.toList();
        System.out.println(integers);
    }

    
    @DisplayName("测试Filter")
    @Test
    public void testFilter(){
        Stream stream = integerList.stream();
        Stream integerStream = stream.filter((t) -> t > 5);
        List integers = integerStream.toList();
        System.out.println(integers);
    }

    
    @DisplayName("测试findAny")
    @Test
    public void testFindAny(){
        Stream stream = integerList.stream();
        Optional any = stream.findAny();
        System.out.println(any.orElse(null));
    }

    
    @DisplayName("测试findFirst")
    @Test
    public void testFindFirst(){
        Stream stream = integerList.stream();
        Optional any = stream.findFirst();
        System.out.println(any.orElse(null));
    }
    
    @DisplayName("测试findFlatMap")
    @Test
    public void testFlatMap(){
        Stream stream = stringList.stream();
        Stream stringStream = stream.flatMap((t)->Stream.of("[" +t+"]"));
        System.out.println(stringStream.toList());
    }

    
    @DisplayName("测试ForEach")
    @Test
    public void testForEach(){
        Stream stream = integerList.stream();
        stream.forEach((t)-> System.out.print(t));
        System.out.println();
        //替换为
        stream = integerList.stream();
        stream.forEach(System.out::print);
        System.out.println();
        //还有一个forEachOrder
        stream = integerList.stream();
        stream.forEachOrdered(System.out::print);
        System.out.println();
    }

    
    @DisplayName("测试Generate")
    @Test
    public void testGenerate(){
        Stream generate = Stream.generate(() -> new Random().nextInt());
        System.out.println(generate.limit(4).toList());
    }

    
    @DisplayName("测试Iterate")
    @Test
    public void testIterate(){
        Stream generate = Stream.iterate(1, (t)->++t);
        System.out.println(generate.limit(4).toList());
    }

    
    @DisplayName("测试limit")
    @Test
    public void testLimit(){
        Stream stream = integerList.stream();
        Stream limit = stream.limit(3);
        System.out.println(limit.toList());

    }

    
    @DisplayName("测试map")
    @Test
    public void testMap(){
        Stream stream = integerList.stream();
        Stream integerStream = stream.map((t) -> t * 2);
        System.out.println(integerStream.toList());
    }

    
    @DisplayName("测试MaxMin")
    @Test
    public void testMaxMin(){
        Stream stream = integerList.stream();
        System.out.println(stream.max(Comparator.comparingInt(a -> a)));
        stream = integerList.stream();
        System.out.println(stream.min(Comparator.comparingInt(a -> a)));
    }

    
    @DisplayName("测试NoneMatch")
    @Test
    public void testNoneMatch(){
        Stream stream = integerList.stream();
        boolean b = stream.noneMatch((t) -> t > 7);
        System.out.println(b);
    }

    
    @DisplayName("测试Of")
    @Test
    public void testOf(){
        System.out.println(Stream.of(1).toList());
        System.out.println(Stream.of(1,2,3).toList());
    }

    
    @DisplayName("测试peek")
    @Test
    public void testPeek(){
        Stream stream = integerList.stream();
        Stream peek = stream.peek((t)->t*=2);
        System.out.println(peek.toList());
    }

    
    @DisplayName("测试reduce")
    @Test
    public void testReduce(){
        Stream stream = integerList.stream();
        Optional reduce = stream.reduce(BinaryOperator.maxBy(Comparator.comparingInt(a->a)));
        System.out.println(reduce.orElse(null));
    }

    
    @DisplayName("测试Skip")
    @Test
    public void testSkip(){
        Stream stream = integerList.stream();
        System.out.println(stream.skip(3).toList());
    }
    
    @DisplayName("测试Sorted")
    @Test
    public void testSorted(){
        Stream stream = integerList.stream();
        System.out.println(stream.sorted().toList());
        stream = integerList.stream();
        System.out.println(stream.sorted((a,b)->b-a).toList());
    }

    
    @Test
    public void testToArray(){
        Stream stream = integerList.stream();
        Object[] objects = stream.toArray();
        for (Object o : objects){
            System.out.println(o);
        }
    }
    @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<>();
        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.println();
    }
    @BeforeEach
    public void getDoubleList(){
        List list = new ArrayList<>();
        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.println();
    }
}

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

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

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