JDK8新特性:Stream
JDK8最大的改变:
1. lambda表达式
2. Stream
1. Steam流的入门
什么是Stream?
也叫Stream流,是jdk8开始的一套API,用于操作集合或者数组中的数据
优点:
Stream流大量结合了Lambda的语法风格来创建,提供了一种更加强大、更加简单的操作集合或者数组中的数据,代码更简洁,可读性更好
package org.example;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List<String> names = new ArrayList<>();
Collections.addAll(names, "张三丰", "张无忌", "王五", "李四", "张三");
System.out.println(names);
System.out.println("--------------------------------");
// 需求: 将姓张的, 并且名字是3个字的人名放到一个新集合
/*
1. 集合和数组的API方法
*/
List<String> list = new ArrayList<>();
for (String name: names) {
if (name.length() == 3 && name.startsWith("张")) {
list.add(name);
}
}
System.out.println(list);
System.out.println("--------------------------------");
/*
2. 使用Steam流
*/
List<String> list2 = names.stream().filter(s->s.startsWith("张")).filter(a->a.length() == 3).collect(Collectors.toList());
System.out.println(list2);
}
}
掌握Stream流就是掌握Stream流的创建、中间方法、终结方法
2. Stream流的创建
package org.example;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
/*
1. List 获取 stream流
*/
List<String> names = new ArrayList<>();
Collections.addAll(names, "张三丰", "张无忌", "王五", "李四", "张三");
Stream<String> stream = names.stream();
stream.filter(s->s.contains("三")).forEach(System.out::println);
System.out.println("--------------------------------");
/*
2. Set 获取stream流
*/
Set<String> set = new HashSet<>();
set.addAll(names);
System.out.println(set);
System.out.println("--------------------------------");
Stream<String> stream1 = set.stream();
stream1.filter(s->s.contains("三")).forEach(System.out::println);
System.out.println("--------------------------------");
/*
3. Map 获取stream流
*/
Map<String, Integer> map = new HashMap<>();
map.put("张三丰", 1);
map.put("张无忌", 2);
map.put("王五", 3);
map.put("李四", 4);
map.put("张三", 5);
Set<String> keys = map.keySet();
Stream<String> stream2 = keys.stream();
Collection<Integer> values = map.values();
Stream<Integer> stream3 = values.stream();
Set<Map.Entry<String, Integer>> entries = map.entrySet();
Stream<Map.Entry<String, Integer>> kvs = entries.stream();
kvs.filter(e -> e.getKey().contains("三")).forEach(e -> {
System.out.println(e.getKey() + ' ' + e.getValue());
});
/*
4. 获取数组的stream流
*/
String[] arr = {"1", "2", "3"};
Stream<String> stream4 = Arrays.stream(arr);
Stream<String> stream5 = Stream.of(arr);
}
}
3. Stream流的中间方法
4. Stream流的终结方法
收集Stream流(只能收集一次):
package org.example;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("张三丰", 1);
map.put("张无忌", 2);
map.put("王五", 3);
map.put("李四", 4);
map.put("张三", 5);
Set<Map.Entry<String, Integer>> entries = map.entrySet();
Stream<Map.Entry<String, Integer>> kvs = entries.stream();
Stream<Map.Entry<String, Integer>> kvs1 = entries.stream();
Map<String, Integer> map1 = kvs.collect(Collectors.toMap(e -> e.getKey() + "t", e -> e.getValue() * 10));
System.out.println(map1);
/*
{王五t=30, 张三丰t=10, 张三t=50, 李四t=40, 张无忌t=20}
*/
// Object[] arr = kvs1.toArray();
Map.Entry<String, Integer>[] arr = kvs1.toArray(len -> new Map.Entry[len]);
System.out.println(Arrays.toString(arr));
/*
[李四=4, 张三=5, 王五=3, 张三丰=1, 张无忌=2]
*/
}
}
最后:
原文地址:https://blog.csdn.net/LFY20031120/article/details/144348578
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!