Stream流 是用于对集合或者数组进行便捷操作的.
1. Stream流体验
案例:有一个List集合,从中找出姓张,且是3个字的名字,存入到一个新集合中去。
用传统方式写:
public class test {
public static void main(String[] args) {
List<String> names = new ArrayList<>();
Collections.addAll(names,"张三丰","张无忌","周芷若","赵敏","张强");
System.out.println(names);
List<String> list = new ArrayList<>();
for (String name : names){
if (name.startsWith("张") && name.length() == 3){
list.add(name);
}
}
System.out.println(list);
}
}
用Stream流 来写:
public class TestStream {
public static void main(String[] args) {
List<String> names = new ArrayList<>();
Collections.addAll(names,"张三丰","张无忌","周芷若","赵敏","张强");
System.out.println(names);
List<String> list =names.stream().filter(s -> s.startsWith("张")).filter(a -> a.length()==3).collect(Collectors.toList());
System.out.println(list);
}
}
2.Stream流的创建
主要掌握以下四点:
1.如何获取List集合的Stream流?
2.如何获取Set集合的Stream流?
3.如何获取Map集合的Stream流?
4.如何获取数组的Stream流?
代码演示:
public class StreamTest {
public static void main(String[] args) {
// 1、如何获取List集合的Stream流?
List<String> names = new ArrayList<>();
Collections.addAll(names, "张三丰","张无忌","周芷若","赵敏","张强");
Stream<String> stream = names.stream();
// 2、如何获取Set集合的Stream流?
Set<String> set = new HashSet<>();
Collections.addAll(set, "刘德华","张曼玉","蜘蛛精","马德","德玛西亚");
Stream<String> stream1 = set.stream();
stream1.filter(s -> s.contains("德")).forEach(s -> System.out.println(s));
// 3、如何获取Map集合的Stream流?
Map<String, Double> map = new HashMap<>();
map.put("古力娜扎", 172.3);
map.put("迪丽热巴", 168.3);
map.put("马尔扎哈", 166.3);
map.put("卡尔扎巴", 168.3);
Set<String> keys = map.keySet();
Stream<String> ks = keys.stream();
Collection<Double> values = map.values();
Stream<Double> vs = values.stream();
Set<Map.Entry<String, Double>> entries = map.entrySet();
Stream<Map.Entry<String, Double>> kvs = entries.stream();
kvs.filter(e -> e.getKey().contains("巴"))
.forEach(e -> System.out.println(e.getKey()+ "-->" + e.getValue()));
// 4、如何获取数组的Stream流?
String[] names2 = {"张翠山", "东方不败", "唐大山", "独孤求败"};
Stream<String> s1 = Arrays.stream(names2);
Stream<String> s2 = Stream.of(names2);
}
}
3.Stream流中间方法
中间方法指的是:调用完方法之后其结果是一个新的Stream流,于是可以继续调用方法,这样一来就可以支持链式编程。
public class TestStream {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
Collections.addAll(list, "jack", "rose", "tom", "张三", "李四", "王武","jack");
// 对流中的数据进行过滤
list.stream().filter(s -> {
// 过滤的依据"o"
return s.contains("o");
}).forEach(System.out::println);
// sorted 对元素进行排序
list.stream().sorted((o1, o2) -> o1.compareTo(o2)).forEach(System.out::println);
// 逻辑分页 limit获取前几个元素 skip跳过前几个元素
list.stream().skip(0).limit(2).forEach(System.out::println);
list.stream().skip(2).limit(2).forEach(System.out::println);
list.stream().skip(4).limit(2).forEach(System.out::println);
list.stream().skip(6).limit(2).forEach(System.out::println);
int pageSize = 2;
for (int page = 1; page <= 3; page++) {
int startIndex = (page - 1) * pageSize;
list.stream().skip(startIndex).limit(pageSize).forEach(System.out::println);
System.out.println("-----");
}
// 去除重复名字
list.stream().distinct().forEach(System.out::println);
// 映射map(对原有数据进行增强)
// 名字变大写
list.stream().map(s -> s.toUpperCase()).forEach(System.out::println);
}
}
4. 案例:
需求: 找出年龄大于18的学生,要求去除重复的名字,并按照年龄降序排序,再输出。
// 定义学生类
public class Student {
public int getAge;
private String name;
private Integer age;
public Student(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
public class Test {
public static void main(String[] args) {
// 创建列表对象
List<Student> students = new ArrayList<>();
// 创建学生对象
Student s1 = new Student("jack", 19);
Student s2 = new Student("rose", 22);
Student s3 = new Student("tom", 15);
Student s4 = new Student("rose", 22);
Student s5 = new Student("ls", 30);
// 添加到学生列表中
Collections.addAll(students, s1, s2, s3, s4, s5);
students.stream().filter(s -> s.getAge() > 18).sorted((o1, o2) -> o2.getAge() - o1.getAge()).
map(s -> "姓名:" + s.getName() + "年龄:" + s.getAge()).distinct().forEach(System.out::println);
}
}