美文网首页
JAVA8 Stream流的使用

JAVA8 Stream流的使用

作者: CNSTT | 来源:发表于2021-07-28 18:13 被阅读0次

一、常用API

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class StreamTest {

    private static List<Integer> generateData1() {
        List<Integer> a = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            a.add((int) (Math.random() * 10));
        }
        return a;
    }

    private static List<QueryModelVO> generateData2() {
        List<QueryModelVO> a = new ArrayList<>();
        QueryModelVO vo = new QueryModelVO();
        vo.setModelCode("E419HN");
        vo.setModelName(String.valueOf((int) (Math.random()*10)));
        a.add(vo);
        vo = new QueryModelVO();
        vo.setModelCode("9J37C7");
        vo.setModelName(String.valueOf((int) (Math.random()*10)));
        a.add(vo);
        vo = new QueryModelVO();
        vo.setModelCode("9J37C7");
        vo.setModelName(String.valueOf((int) (Math.random()*10)));
        a.add(vo);
        vo = new QueryModelVO();
        vo.setModelCode("0T18WT");
        vo.setModelName(String.valueOf((int) (Math.random()*10)));
        a.add(vo);
        return a;
    }

    public static void main(String[] args) {
        List<Integer> a1 = generateData1();
        List<QueryModelVO> b1 = generateData2();
        List<Integer> c1 = new ArrayList<>();
        System.out.println("a1:" + a1);
        System.out.println("b1:" + b1);
        // filter count
        List<Integer> filter1 = a1.stream().filter(a -> a > 5).collect(Collectors.toList());
        int filter2 = (int) a1.stream().filter(a -> a > 5).count();
        System.out.println("筛选大于5:" + filter1);
        System.out.println("筛选大于5的个数:" + filter2);
        System.out.println("==================================");
        // map 对一个流中的值进行某种形式的转换。需要传递给它一个转换的函数作为参数
        List<String> map1 = a1.stream().map(String::valueOf).collect(Collectors.toList());
        System.out.println("Integer转String:" + map1);
        System.out.println("==================================");
        List<String> map2 = b1.stream().map(QueryModelVO::getModelCode).collect(Collectors.toList());
        System.out.println("map提取转换:" + map2);
        System.out.println("==================================");
        // limit 返回一个包含n个元素的新的流(若总长小于n则返回原始流)
        List<QueryModelVO> limit1 = b1.stream().limit(2).collect(Collectors.toList());
        List<QueryModelVO> limit2 = b1.stream().limit(5).collect(Collectors.toList());
        System.out.println("limit1:" + limit1);
        System.out.println("limit2:" + limit2);
        System.out.println("==================================");
        // skip 丢弃掉前面的n个元素
        List<QueryModelVO> skip1 = b1.stream().skip(2).collect(Collectors.toList());
        List<QueryModelVO> skip2 = b1.stream().skip(5).collect(Collectors.toList());
        System.out.println("skip1:" + skip1);
        System.out.println("skip2:" + skip2);
        System.out.println("==================================");
        // distinct sorted 去重和排序
        List<Integer> sort1 = a1.stream().sorted().collect(Collectors.toList());
        List<Integer> sort2 = a1.stream().distinct().sorted().collect(Collectors.toList());
        System.out.println("排序后:" + sort1);
        System.out.println("去重排序后:" + sort2);
        System.out.println("==================================");
        // max min 求出最大最小值
        Optional<Integer> data = a1.stream().filter(x -> x > 10).max(Integer::compareTo);
        if (data.isPresent()) {
            int maxData = data.get();
            System.out.println("最大值:" + maxData);
        } else {
            System.out.println("不存在大于10的数");
        }
        Optional<Integer> data2 = a1.stream().filter(x -> x < 10).max(Integer::compareTo);
        if (data2.isPresent()) {
            int minDate = data2.get();
            System.out.println("最小值:" + minDate);
        } else {
            System.out.println("不存在小于10的数");
        }
        // findFirst 返回非空集合中的第一个值
        int first = a1.stream().filter(x -> x > 7).findFirst().get();
        int any = a1.stream().filter(x -> x > 7).findAny().get();
        System.out.println("findFirst:" + first);
        System.out.println("findAny:" + any);
    }
}

二、取两个List的交集

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class CompareTwo {

    private static List<QueryModelVO> generateData1() {
        List<QueryModelVO> a = new ArrayList<>();
        QueryModelVO vo = new QueryModelVO();
        vo.setModelCode("E419HN");
        vo.setModelName(String.valueOf((int) (Math.random()*10)));
        a.add(vo);
        vo = new QueryModelVO();
        vo.setModelCode("9J37C7");
        vo.setModelName(String.valueOf((int) (Math.random()*10)));
        a.add(vo);
        vo = new QueryModelVO();
        vo.setModelCode("9J2233");
        vo.setModelName(String.valueOf((int) (Math.random()*10)));
        a.add(vo);
        vo = new QueryModelVO();
        vo.setModelCode("0T18WT");
        vo.setModelName(String.valueOf((int) (Math.random()*10)));
        a.add(vo);
        return a;
    }

    private static List<QueryModelVO> generateData2() {
        List<QueryModelVO> a = new ArrayList<>();
        QueryModelVO vo = new QueryModelVO();
        vo.setModelCode("E419HN");
        vo.setModelName(String.valueOf((int) (Math.random()*10)));
        a.add(vo);
        vo = new QueryModelVO();
        vo.setModelCode("9J2233");
        vo.setModelName(String.valueOf((int) (Math.random()*10)));
        a.add(vo);
        vo = new QueryModelVO();
        vo.setModelCode("9J37C3");
        vo.setModelName(String.valueOf((int) (Math.random()*10)));
        a.add(vo);
        vo = new QueryModelVO();
        vo.setModelCode("0T17WT");
        vo.setModelName(String.valueOf((int) (Math.random()*10)));
        a.add(vo);
        return a;
    }

    public static void main(String[] args) {
        List<QueryModelVO> a1 = generateData1();
        List<QueryModelVO> a2 = generateData2();
        List<String> list = new ArrayList<>(10);
        List<QueryModelVO> a3 = new ArrayList<>(10);
        List<QueryModelVO> a4 = new ArrayList<>(10);
        System.out.println("a1:" + a1);
        System.out.println("a2:" + a2);
        // 1、两层for循环
        for (QueryModelVO vo1 : a1) {
            for (QueryModelVO vo2 : a2) {
                if (vo1.getModelCode().equals(vo2.getModelCode())) {
                    list.add(vo2.getModelCode());
                }
            }
        }
        System.out.println("list:" + list);
        for (QueryModelVO vo1 : a1) {
            if (list.contains(vo1.getModelCode())) {
                a3.add(vo1);
            }
        }
        for (QueryModelVO vo2 : a2) {
            if (list.contains(vo2.getModelCode())) {
                a4.add(vo2);
            }
        }
        System.out.println("a3:" + a3);
        System.out.println("a4:" + a4);
        // 2、Stream流
        List<QueryModelVO> list2 = a1.stream().filter(i -> a2.stream().anyMatch(k -> k.getModelCode().equals(i.getModelCode()))).collect(Collectors.toList());
        List<QueryModelVO> list3 = a2.stream().filter(i -> a1.stream().anyMatch(k -> k.getModelCode().equals(i.getModelCode()))).collect(Collectors.toList());
        System.out.println("list2:" + list2);
        System.out.println("list3:" + list3);
    }
}

相关文章

网友评论

      本文标题:JAVA8 Stream流的使用

      本文链接:https://www.haomeiwen.com/subject/bkazmltx.html