image.png
1.获取 Stream 流
image.png
【特别注意:只有引用类型的数组才能使用Stream.of()获取Stream 流,基本数据类型的数组不会自动装箱】
// 生成流 - 集合数组等
private static void CreateStream() {
{
System.out.println("--------- 单列集合 stream 流 ---------");
// list集合
List<String> list = new ArrayList<>();
Collections.addAll(list, "张三", "李四", "王五", "赵六");
list.stream().forEach(s -> {
System.out.print(s + " ");
});
System.out.println(); // 换行
// set集合
Set<String> set = new HashSet<>();
Collections.addAll(set, "张三", "李四", "王五", "赵六");
set.stream().forEach(s -> {
System.out.print(s + " ");
});
System.out.println(); // 换行
}
System.out.println(); // 换行
{
System.out.println("--------- 双列集合 stream 流 ---------");
// Map需要先转set再调用stream()方法
Map<String, String> map = new HashMap<>();
map.put("001", "张三");
map.put("002", "李四");
map.put("003", "王五");
map.put("004", "赵六");
map.keySet().stream().forEach(s -> {
System.out.print(s + " ");
});
System.out.println(); // 换行
map.values().stream().forEach(s -> {
System.out.print(s + " ");
});
System.out.println(); // 换行
map.entrySet().stream().forEach((s) -> {
/* String key = s.getKey();
String value = s.getValue();
System.out.print(key + ":" + value + " ");*/
System.out.print(s + " ");
});
System.out.println(); // 换行
}
System.out.println(); // 换行
{
System.out.println("--------- 数组 stream 流 ---------");
// 数组--使用Stream.of()
String[] arr = {"张三", "李四", "王五", "赵六"};
System.out.println("--------- 方式一:Stream.of() 获取 Stream 流【注意:只有引用类型的数组才能使用Stream.of()获取Stream 流,基本数据类型的数组不会自动装箱】---------");
Stream.of(arr).forEach(s->{
System.out.print(s + " ");
});
System.out.println(); // 换行
System.out.println("--------- 方式二:Arrays.stream() 获取 Stream 流---------");
Arrays.stream(arr).forEach(s->{
System.out.print(s + " ");
});
System.out.println(); // 换行
}
System.out.println(); // 换行
{
System.out.println("--------- 一堆零散的数据获取 stream 流 ---------");
// 零散的数据的数据类型必须一致才行
Stream.of("a", "b", "c", "d").forEach(s -> {
System.out.print(s + " ");
});
System.out.println(); // 换行
}
}
2. Stream 流的中间方法
image.png
// 2.Stream 流的中间方法
private static void middleStream() {
System.out.println("--------- 2.Stream 流的中间方法 ---------");
List<String> list = new ArrayList<>();
Collections.addAll(list, "张三", "李四", "王五", "赵六", "张三", "李四");
// 1. 过滤 - filter
{
System.out.println("--------- 过滤 - filter ---------");
// 过滤出以“张”开头的字符串
list.stream().filter(s -> s.startsWith("张")).forEach(s -> {
System.out.print(s + " ");
});
}
System.out.println(); // 换行
// 2.获取前几个元素 - limit
{
System.out.println("--------- 获取前几个元素 - limit ---------");
// 获取前三个元素
list.stream().limit(3).forEach(s -> {
System.out.print(s + " ");
});
}
System.out.println(); // 换行
// 3.跳过前几个元素 - skip
{
System.out.println("--------- 跳过前几个元素 - skip ---------");
// 跳过前三个元素
list.stream().skip(3).forEach(s -> {
System.out.print(s + " ");
});
}
System.out.println(); // 换行
// 4.去重 - distinct 【底层是通过 HashSet 去重的】
{
System.out.println("--------- 去重 - distinct ---------");
list.stream().distinct().forEach(s -> {
System.out.print(s + " ");
});
}
System.out.println(); // 换行
// 5.连接流 - concat
{
System.out.println("--------- 连接流 - concat ---------");
List<String> list2 = new ArrayList<>();
Collections.addAll(list2, "张无忌", "王竹霞", "岳云鹏", "张曼玉");
Stream.concat(list.stream(), list2.stream()).forEach(s -> {
System.out.print(s + " ");
});
}
System.out.println(); // 换行
// 6.映射流 - map
{
System.out.println("--------- 映射流 - map ---------");
ArrayList<String> list3 = new ArrayList<>();
Collections.addAll(list3, "张无忌,20", "王竹霞,21", "岳云鹏,22", "张曼玉,23");
// s 是流中的每个元素
list3.stream().map(s -> {
// 转换后的数据类型变成了int
return Integer.parseInt(s.split(",")[1]);
}).forEach(s -> {
System.out.print(s + " ");
});
}
}
3. Stream 流的终结方法
image.png
注意 collect 收集到 map 集合里面时 key 不能重复,重复会报错,可以通过 Collectors.toMap 第三个参数指定重复了怎么处理。
// 3.Stream 流的终结方法
private static void finalStream() {
System.out.println(); // 换行
// list流操作与收集
System.out.println("--------- 3.Stream 流的终结方法---------");
ArrayList<String> list = new ArrayList<>();
Collections.addAll(list, "张三-男-26","张三-男-26", "张三-男-27", "李四-女-20", "王五-男-30", "赵六-男-22", "小紅-女-24");
// forEach 遍历
{
System.out.println("--------- forEach 遍历---------");
list.stream().forEach(s -> {
System.out.print(s + " ");
});
System.out.println(); // 换行
}
// count 遍历
{
long count = list.stream().count();
System.out.println("--------- count 统计 " + count + "---------");
}
// toArray
{
System.out.println("--------- toArray 转换为数组---------");
// toArray 转换为数组。注意这个 value 参数代表的是数组的长度,而不是数组的元素个数。需要给 toArray 一个指定类型的数组
String[] arr = list.stream().toArray(value -> new String[value]);
System.out.println(Arrays.toString(arr));
}
// collect - 将流的数据收集到集合里面
{
System.out.println("--------- collect 收集 - 将流的数据收集到集合里面---------");
// 需求:将所有男性收集到新的 List 集合里面
List<String> collectList = list.stream().filter(s -> "男".equals(s.split("-")[1])).collect(Collectors.toList());
System.out.println("将所有男性收集到新的 List:" + collectList);
// 需求:将所有男性收集到新的 set 集合里面 - 也就是去重
Set<String> setList = list.stream().filter(s -> "男".equals(s.split("-")[1])).collect(Collectors.toSet());
System.out.println("将所有男性收集到set 集合里面:" + setList);
// 需求:将所有男性和年龄收集到新的 map 集合里面 - 注意:key 不能重复,重复会报错
Map<String, Integer> map = list.stream().filter(s -> "男".equals(s.split("-")[1]))
.collect(Collectors.toMap(
// 生成键的规则 - s 代表流中的每个元素
s -> s.split("-")[0],
// 生成值的规则 - s 代表流中的每个元素
s -> Integer.parseInt(s.split("-")[2]),
// 合并规则 - 如果 key 重复了,如何处理,这里是覆盖
(existing, replacement) -> replacement // merge function (keep last value)
));
System.out.println("将所有男性收集到map 集合里面:" + map);
}
}
总结
image.png
参考:黑马 java 教程