Bootstrap

javase-Stream流

stream流相当于一个流水线,一共可以分成三大类的方法。

  1. 获取stream流
  2. 中间方法
  3. 终结方法(可以得到一个返回结果的)

获取stream流

单列集合获取Stream流

public class Demo01 {
    // 单列集合获取stream流
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list, "a", "b", "c", "d", "e", "f");
        /*Stream<String> stream = list.stream(); // 获取到一条流水线,并把集合中的数据放到流水线上
        stream.forEach((x)-> System.out.println(x));*/
        list.stream().forEach(x -> System.out.println(x));
    }
}

双列集合获取stream流

public class Demo02 {
    public static void main(String[] args) {
        HashMap<String, Integer> map = new HashMap<>();
        map.put("a", 1);
        map.put("b", 2);
        map.put("c", 3);
        map.put("d", 4);
        // 双列集合获取stream流
        map.keySet().stream().forEach(x -> System.out.println(x));
        map.entrySet().stream().forEach(x -> System.out.println(x));
    }
}

数组获取stream流

public class Demo03 {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};
        Arrays.stream(arr).forEach(x -> System.out.println(x));
    }
}

一堆零散的数据

public class Demo04 {
    public static void main(String[] args) {
        Stream.of("a", 1, 2, 3).forEach(x -> System.out.println(x));
    }
}

流只能用一次,就算保存了流对象,后边也无法使用了。

中间方法

filter过滤

public class Demo01 {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list, "张无忌", "周芷若", "赵敏", "张三丰", "张翠山", "张良", "王二麻子", "谢广坤");
        list.stream()
                .filter(x -> x.startsWith("张")) // 过滤姓名张开头的
                .filter(x -> x.length() == 3) // 过滤长度为3的
                .forEach(x -> System.out.println(x));
    }
}

skip跳过前n个

public class Demo02 {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list, "张无忌", "周芷若", "赵敏", "张三丰", "张翠山", "张良", "王二麻子", "谢广坤");
        list.stream()
                .limit(2) // 获取前两[个]
                .forEach(x -> System.out.println(x));

        System.out.println("=================");

        list.stream()
                .skip(2) // 跳过前两个
                .forEach(x -> System.out.println(x));

        System.out.println("=================");

        list.stream()
                .skip(3)
                .limit(3)
                .forEach(x -> System.out.println(x));
    }
}

distinct去除集合中重复的元素

public class Demo03 {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list, "张无忌","张无忌","张无忌","张无忌","张无忌", "周芷若","周芷若","周芷若","周芷若", "赵敏", "张三丰", "张翠山", "张良", "王二麻子", "谢广坤");
        list.stream()
                .distinct() // 去重
                .forEach(x -> System.out.println(x));
    }
}

concat连接两个集合

public class Demo04 {
    public static void main(String[] args) {
        ArrayList<String> list1 = new ArrayList<>();
        Collections.addAll(list1, "张无忌", "周芷若", "赵敏", "张三丰", "张翠山", "张良");
        ArrayList<String> list2 = new ArrayList<>();
        Collections.addAll(list2, "张无忌", "周芷若" , "王二麻子", "谢广坤");
        Stream.concat(list1.stream(), list2.stream()) // 连接两个集合
                .forEach(x -> System.out.println(x));
    }
}

map类型转换

public class Demo05 {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<String>();
        Collections.addAll(list, "张无忌-1", "周芷若-2", "赵敏-3", "张三丰-4", "张翠山-5", "张良-6", "王二麻子-7", "谢广坤-8");
        list.stream()
                .map(x -> { // map方法执行完后,流上的数据就变成了整数
                        String[] arr = x.split("-");
                        return Integer.parseInt(arr[1]);
                    })
                .forEach(x -> System.out.println(x)); // 打印的是整数
    }
}

flatMap将流中每个元素转成另一个流

public class Demo06 {
    public static void main(String[] args) {
        List<String> familyNameList = Arrays.asList("赵", "钱", "孙", "李");
        List<String> boyNameList = Arrays.asList("男", "子", "孩");

       List<String> cartesianProductList = familyNameList.stream()
                .flatMap(familyName -> boyNameList.stream() // 将流中每个元素转换成另一个流
                                        .map(boyName -> familyName + boyName))
                .forEach(x -> System.out.println(x));
    }
}

终结方法

forEach遍历集合

public class Demo01 {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list, "张无忌", "周芷若", "赵敏", "张三丰", "张翠山", "张良", "王二麻子", "谢广坤");
        list.stream().forEach(x->System.out.println(x));
    }
}

count计算集合中的元素

public class Demo02 {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list, "张无忌", "周芷若", "赵敏", "张三丰", "张翠山", "张良", "王二麻子", "谢广坤");
        long count = list.stream().count();
        System.out.println(count);
    }
}

toArray收集流中的数据放到数组中

public class Demo03 {
    // 收集流中的数据,放到数组中
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<String>();
        Collections.addAll(list, "张无忌", "周芷若", "赵敏", "张三丰", "张翠山", "张良", "王二麻子", "谢广坤");
        Object[] arr1 = list.stream().toArray();
        System.out.println(Arrays.toString(arr1));
        System.out.println("==========");
        String[] arr2 = list.stream().toArray(n -> new String[n]);
        System.out.println(Arrays.toString(arr2));
    }
}

collect收集流中的数据,放到集合中(List、Set、Map)

public class Demo04 {
    // 收集流中的数据,放到集合中(List、Set、Map)
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<String>();
        Collections.addAll(list, "张无忌-男-15", "周芷若-女-14", "赵敏-女-14", "张三丰-男-100", "张翠山-男-40", "张良-男-35", "王二麻子-男-37", "谢广坤-男-41");
        // 收集所有的男性到List集合中
        List<String> list1 = list.stream()
                .filter(x -> "男".equals(x.split("-")[1])) // 过滤所有男性
                .collect(Collectors.toList());// 创建一个ArrayList集合
        
        // 收集所有的男性到Set集合中
        Set<String> set1 = list.stream()
                .filter(x -> "男".equals(x.split("-")[1])) // 过滤所有男性
                .collect(Collectors.toSet());

        // 收集Map集合(键不能重复,否则代码报错)
        // Key:姓名、Value:年龄
        /**
         * toMap(键的规则,值的规则)
         */
        HashMap<String, String> mp1 = list.stream()
                .filter(x -> "男".equals(x.split("-")[1])) // 过滤所有男性
                .collect(Collectors.toMap(x -> x.split("-")[0], // 键的规则
                        x -> x.split("-")[1]));// 值的规则
    }
}
;