JAVA8 stream() 总结归纳

2022-12-23 08:49:11 浏览数 (1)

stream() 将数组转换为数据流,提供排序、去重、类型转换、过滤等多种操作方法,使代码更加简洁高效。

代码语言:javascript复制
public class StreamTest {

    List<Integer> list = Arrays.asList(6, 3, 0, 7, 1, 2, 5, 1);
    int[] arr = new int[]{6, 3, 0, 7, 1, 2, 5, 1};
    /**
     * 排序
     *
     * 重点掌握两种写法:
     * list.stream().sorted((a, b) -> (a -b)).collect(Collectors.toList());
     * Collections.sort(list2, (a, b) -> b.compareTo(a));
     */
    @Test
    public void testSort() {
        // 升序
        System.out.println(list.stream().sorted().collect(Collectors.toList()));
        System.out.println(list.stream().sorted(Comparator.comparing(Integer::intValue)).collect(Collectors.toList()));
        System.out.println(list.stream().sorted((a, b) -> (a - b)).collect(Collectors.toList()));
        // 降序
        System.out.println(list.stream().sorted(Comparator.comparing(Integer::intValue).reversed()).collect(Collectors.toList()));
        System.out.println(list.stream().sorted((a, b) -> (b - a)).collect(Collectors.toList()));
        System.out.println(list.stream().sorted((a, b) -> b.compareTo(a)).collect(Collectors.toList()));


        // 扩展其他升序
        List<Integer> list1 = Arrays.asList(6, 3, 0, 7, 1, 2, 5, 1);
        Collections.sort(list1);
        System.out.println(list1);

        List<Integer> list2 = Arrays.asList(6, 3, 0, 7, 1, 2, 5, 1);
        Collections.sort(list2, (a, b) -> (a -b));
        System.out.println(list2);

        // 扩展其他降序
        List<Integer> list3 = Arrays.asList(6, 3, 0, 7, 1, 2, 5, 1);
        Collections.sort(list3, (a, b) -> (b - a));
        System.out.println(list3);
    }

    /**
     * 装箱操作int[] -> List<Integer>
     */
    @Test
    public void testBoxed() {
        List<Integer> list1 = Arrays.stream(arr).boxed().collect(Collectors.toList());
        System.out.println(list1);
    }

    /**
     * 最大值
     */
    @Test
    public void testMaxAndMin() {
        // 正确的写法
        System.out.println(list.stream().max(Integer::compare).get());
        System.out.println(list.stream().mapToInt(Integer::intValue).max().getAsInt());
        System.out.println(Arrays.stream(arr).max().getAsInt());
        System.out.println(Arrays.stream(arr).min().getAsInt());

        // 错误的写法 stream 的max() 和min() 方法是通过判断结果大于或者小于0进行判断的
//        List<Integer> list1 = Arrays.asList(2, 3, 0, 7, 1, -2, 5, 6);
        System.out.println(list.stream().max(Integer::max).get());
        System.out.println(list.stream().min(Integer::min).get());

    }

    /**
     * 使用示例
     */
    @Test
    public void testCollectors() {
        List<String> strList = Arrays.asList("sdkj1", "dhe", "sdfi2", "nvi", "ndfsk", "dfsdf", "1i2jd", "dsjfl", "11", "11");

        // distinct() 去重
        strList = strList.stream().distinct().collect(Collectors.toList());
        System.out.println(strList);

        // filter() 过滤
        List<String> str1 = strList.stream().filter(e -> e.contains("1")).collect(Collectors.toList());
        System.out.println(str1);

        //map() 方法用于映射每个元素到对应的结果
        List<String> str2 = strList.stream().map(m -> m = m   "map").collect(Collectors.toList());
        System.out.println(str2);


        // limit() 获取指定数量
        System.out.println(strList.stream().limit(3).collect(Collectors.toList()));
        // skip() 跳过指定数量
        System.out.println(strList.stream().skip(3).collect(Collectors.toList()));



        List<Map<String , String>> mapList = new ArrayList<>();
        Map<String, String> m1 = new HashMap<>();
        Map<String, String> m2 = new HashMap<>();
        Map<String, String> m3 = new HashMap<>();

        m1.put("name", "张三");
        m2.put("name", "张三");
        m3.put("name", "王五");
        m1.put("age", "22");
        m2.put("age", "33");
        m3.put("age", "22");
        mapList.add(m1);
        mapList.add(m2);
        mapList.add(m3);

        // Collectors.toList() 返回List
        System.out.println(mapList.stream().map(m -> m.get("name")).collect(Collectors.toList()));

        // Collectors.toSet() 返回Set
        System.out.println(mapList.stream().map(m -> m.get("age")).collect(Collectors.toSet()));

        // Collectors.joining() 拼接数据返回字符串
        System.out.println(mapList.stream().map(m -> m.get("name")).collect(Collectors.joining()));
        System.out.println(mapList.stream().map(m -> m.get("name")).collect(Collectors.joining(",")));

        // Collectors.toMap 返回Map {李四={name=李四, age=33}, 张三={name=张三, age=22}, 王五={name=王五, age=22}}
        // 第三个参数(a, b) -> a 代表如果遇到相同的key 取第一个值,如果没有设置此参数,重复key 会出错 java.lang.IllegalStateException: Duplicate key
        // 第三个参数(a, b) -> b 代表如果遇到相同的key 取最后一个值
        System.out.println(mapList.stream().collect(Collectors.toMap(m -> m.get("name"), Function.identity(), (a, b) -> b)));


    }
}

0 人点赞