package Stream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
public class StreamTest1 {
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流来解决这个需求
List<String> list2 = names.stream().filter(s->s.startsWith("张"))
.filter(a->a.length() == 3).collect(Collectors.toList());
System.out.println(list2); //[张无忌, 张三丰]
}
}
package Stream;
import java.util.*;
import java.util.stream.Stream;
public class StreamTest2 {
public static void main(String[] args) {
List<String> names = new ArrayList<>();
Collections.addAll(names,"张无忌","张三丰","周芷若","赵敏","张强");
Stream<String> stream = names.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));
Map<String, Double> map = new HashMap<>();
map.put("古力娜扎",168.5);
map.put("迪丽热巴",168.9);
map.put("马儿扎哈",167.3);
map.put("卡尔扎巴",162.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()));
String[] names2 = {"张无忌","张三丰","周芷若","赵敏","张强"};
Stream<String> s1 = Arrays.stream(names2);
Stream<Object> s2 = Stream.of(names2);
}
}
package Stream;
import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;
public class StreamTest3 {
public static void main(String[] args) {
List<Double> scores = new ArrayList<>();
Collections.addAll(scores,88.5,82.3,84.6,75.3,81.6,95.2);
// 需求1:找出成绩大于等于85分的数据,并升序后 在输出
// // Stream<T>filter(Predicate<?superT>predicate) 用于对流中的数据进行过滤。
// // Stream<T>sorted() 对元素进行升序排序
// // Stream<T>sorted(Comparator<?superI>comparator) 按照指定规则排序
scores.stream().filter(s->s>=85).sorted().forEach(s -> System.out.println(s));
//88.5
//95.2
List<Student> students = new ArrayList<>();
Student s1 = new Student("蜘蛛精",26,172.3);
Student s2 = new Student("蜘蛛精",26,172.3);
Student s3 = new Student("紫霞",23,167.6);
Student s4 = new Student("白晶晶",25,169.0);
Student s5 = new Student("牛魔王",35,182.5);
Student s6 = new Student("牛夫人",34,168.9);
Collections.addAll(students,s1,s2,s3,s4,s5,s6);
// 需求2:找出年龄大于23,且年龄小于等于30岁的学生,并按照年龄降序输出
// students.stream().filter(s -> s.getAge() > 23 && s.getAge() <= 30)
// .sorted().forEach(s -> System.out.println(s));//异常,不能直接用sorted方法
System.out.println("=====找出年龄大于23,且年龄小于等于30岁的学生,并按照年龄降序输出=====");
students.stream().filter(s -> s.getAge() > 23 && s.getAge() <= 30)
.sorted((o1, o2) -> o2.getAge() - o1.getAge())
.forEach(s -> System.out.println(s));
//Student{name='蜘蛛精', age=26, height=172.3}
//Student{name='蜘蛛精', age=26, height=172.3}
//Student{name='白晶晶', age=25, height=169.0}
// 如果出来的是地址则在Student类里重写toString方法
// 需求3:取出身高最高的前三名学生,并输出
// Stream<T>limit(long maxSize) 获取前几个元素
System.out.println("==========取出身高最高的前三名学生,并输出============");
students.stream().sorted((o1,o2) -> Double.compare(o2.getHeight(),o1.getHeight()))
.limit(3).forEach(System.out::println);
//Student{name='牛魔王', age=35, height=182.5}
//Student{name='蜘蛛精', age=26, height=172.3}
//Student{name='蜘蛛精', age=26, height=172.3}
// 需求4:取出身高倒数的2名学生,并输出
System.out.println("==========取出身高倒数的2名学生,并输出============");
// Stream<T>skip(long n) 跳过前几个元素
students.stream().sorted((o1, o2) -> Double.compare(o2.getHeight(),o1.getHeight()))
.skip(students.size() - 2).forEach(System.out::println);
//Student{name='牛夫人', age=34, height=168.9}
//Student{name='紫霞', age=23, height=167.6}
// 需求5:找出身高超过168的学生叫什么名字,要求去除重复的名字,在输出
System.out.println("====找出身高超过168的学生叫什么名字,要求去除重复的名字,在输出====");
// Stream<T> distinct() 去除流中重复的元素。
students.stream().filter(s -> s.getHeight() > 168).map(s->s.getName())
.distinct().forEach(s-> System.out.println(s));
//蜘蛛精
//白晶晶
//牛魔王
//牛夫人
System.out.println("=========================简化==========================");
// <R>Stream<R>map(Function<?super T,? extends R> mapper) 对元素进行加工,并返回对应的新流
students.stream().filter(s -> s.getHeight() > 168).map(Student::getName)
.distinct().forEach(System.out::println);
//蜘蛛精
//白晶晶
//牛魔王
//牛夫人
//distinct去重复,自定义类型的对象(希望内容一样就认为重复,重写hashCode,equals)
students.stream().filter(s -> s.getHeight() > 168)
.distinct().forEach(System.out::println);
Stream<String> st1 = Stream.of("张三", "李四");
Stream<String> st2 = Stream.of("王五", "麻子", "超人");
// staticT>Stream<T>concat(Stream a,Stream b) 合并a和b两个流为一个流
Stream<String> allSt = Stream.concat(st1, st2);
allSt.forEach(System.out::println);
//张三
//李四
//王五
//麻子
//超人
}
}
package Stream;
import java.util.*;
import java.util.stream.Collectors;
public class StreamTest4 {
public static void main(String[] args) {
List<Student> students = new ArrayList<>();
Student s1 = new Student("蜘蛛精",26,172.3);
Student s2 = new Student("蜘蛛精",26,172.3);
Student s3 = new Student("紫霞",23,167.6);
Student s4 = new Student("白晶晶",25,169.0);
Student s5 = new Student("牛魔王",35,182.5);
Student s6 = new Student("牛夫人",34,168.9);
Collections.addAll(students,s1,s2,s3,s4,s5,s6);
// 需求1,请计算出身高超过168的学生有几人
long size = students.stream().filter(s -> s.getHeight() > 168).count();
System.out.println(size);
// 需求2、请找出身高最高的学生对象,并输出
Student s = students.stream().max(((o1, o2) -> Double.compare(o1.getHeight(), o2.getHeight()))).get();
System.out.println(s);
// 需求3、请找出身高最矮的学生对象,并输出
Student ss = students.stream().min(((o1, o2) -> Double.compare(o1.getHeight(), o2.getHeight()))).get();
System.out.println(ss);
// 需求4、请找出身高超过170的学生对象,并放到一个心机和中去返回
List<Student> students1 = students.stream().filter(a -> a.getHeight() > 170).collect(Collectors.toList());
System.out.println(students1);
Set<Student> students2 = students.stream().filter(a -> a.getHeight() > 170).collect(Collectors.toSet());
System.out.println(students2);
// Stream只能收集一次
// 需求5、请找出身高超过170的学生对象,并把学生对象的名字和身高,存入到一个Map集合返回
Map<String, Double> map =
students.stream().filter(a -> a.getHeight() > 170)
.distinct().collect(Collectors.toMap(a -> a.getName(), a -> a.getHeight()));
System.out.println(map);
// Object[] arr = students.stream().filter(a -> a.getHeight() > 170).toArray();
Student[] arr = students.stream().filter(a -> a.getHeight() > 170).toArray(len -> new Student[len]);
System.out.println(Arrays.toString(arr));
}
}