可以理解的是,forEach输出了一个流以后,不能再次对它forEach
import java.util.*;
import java.util.stream.Stream;
/**
* @Author jiangxuzhao
* @Description
* @Date 2023/6/6
*/
public class Demo01GetStream {
public static void main(String[] args) {
// 通过.stream()获取
ArrayList<String> list = new ArrayList<>();
Stream<String> stream1 = list.stream();
HashSet<String> set = new HashSet<>();
Stream<String> stream2 = set.stream();
HashMap<String, Object> map = new HashMap<>();
Set<String> keySet = map.keySet();
Stream<String> stream3 = keySet.stream();
Collection<Object> values = map.values();
Stream<Object> stream4 = values.stream();
Set<Map.Entry<String, Object>> entries = map.entrySet();
Stream<Map.Entry<String, Object>> stream5 = entries.stream();
// Stream.of()静态方法
Integer[] arr={1,2,3};
Stream<Integer> stream6 = Stream.of(arr);
// Stream.of()底层调用的方法
Stream<Integer> stream7 = Arrays.stream(arr);
}
}
import java.util.stream.Stream;
/**
* @Author jiangxuzhao
* @Description 遍历
* @Date 2023/6/7
*/
public class Demo2Stream_forEach {
public static void main(String[] args) {
Stream<String> stream2 = Stream.of("张三", "李四", "王五");
// void forEach(Consumer super T> action) 接受一个范型T,返回void void accept(T t);
stream2.forEach(name-> System.out.print(name+" ")); // 张三 李四 王五
}
}
import java.util.stream.Stream;
/**
* @Author jiangxuzhao
* @Description
* @Date 2023/6/7
*/
public class Demo3Stream_filter {
public static void main(String[] args) {
Stream<String> stream3 = Stream.of("张三", "李四", "王五");
Stream<String> stream4 = stream3.filter(name -> name.startsWith("张")); //Stream流只能被消费使用一次,每次返回一个新流
stream4.forEach(System.out::println); // 张三
Stream<Integer> stream5 = Stream.of(28, 29, 30, 31);
stream5.filter(age->age>28).forEach(System.out::println); // 29 30 31
}
}
import java.util.stream.Stream;
/**
* @Author jiangxuzhao
* @Description 将一种类型的流转换成另一种类型的流
* @Date 2023/6/7
*/
public class Demo4Stream_map {
public static void main(String[] args) {
Stream<String> stream4 = Stream.of("1","2","3","4");
// 将Stream转换为Stream并输出
stream4.map(s->Integer.parseInt(s)+1).forEach(System.out::println);// 2 /n 3 /n 4 /n 5 /n
}
}
import java.util.ArrayList;
/**
* @Author jiangxuzhao
* @Description 统计Stream流中的元素个数
* @Date 2023/6/7
*/
public class Demo5Stream_count {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
long count = list.stream().count();
System.out.println(count); // 3
}
}
从Stream流收集生成Collection集合
import java.util.*;
import java.util.stream.Collectors;
/**
* @Author jiangxuzhao
* @Description 从Stream流收集生成Collection集合
* @Date 2023/6/7
*/
public class Demo6Steam_collect {
public static void main(String[] args) {
List<User> list = Arrays.asList(new User(19), new User(21), new User(22));
// Stream collect成list
Collection<User> collectList = list.stream().filter(user -> user.getId() > 20)
.collect(Collectors.toList());
System.out.println("collectList:"+collectList.toString());
// Stream collect成set
Set<User> collectSet = list.stream().filter(user -> user.getId() > 20)
.collect(Collectors.toSet());
System.out.println("collectSet:"+collectSet);
// Stream collect成map, key为id,value为User
Map<Integer, User> collectMap = list.stream().collect(Collectors.toMap(
user -> user.getId(),
user -> user
));
for(Map.Entry<Integer,User> entry:collectMap.entrySet()){
System.out.println(entry.getKey()+" "+entry.getValue());
}
}
}
class User{
Integer id;
public User(Integer id){
this.id=id;
}
public Integer getId(){
return this.id;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
'}';
}
}
collect高级用法:分组,max,min,average,sum
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* @Author jiangxuzhao
* @Description
* @Date 2023/6/7
*/
public class Demo7Stream_collect2 {
public static void main(String[] args) {
// 分组groupingBy分组,按照年龄进行分组
List<Stu> list = Arrays.asList(new Stu(19,8), new Stu(21,8), new Stu(22,9), new Stu(27,7));
// 自动收集成Map>集合
Map<Integer, List<Stu>> collectGroup = list.stream().collect(Collectors.groupingBy(stu -> stu.getAge()));
System.out.println(collectGroup.toString()); // {7=[Stu{id=27, age=7}], 8=[Stu{id=19, age=8}, Stu{id=21, age=8}], 9=[Stu{id=22, age=9}]}
// 收集年龄最大的值 collect(Collectors.maxBy())返回的是一个Optional对象,如果有多个满足条件的就返回一个
Optional<Stu> maxAgeOptional = list.stream().collect(Collectors.maxBy((p1, p2) -> p1.getAge() - p2.getAge()));
System.out.println(maxAgeOptional.get()); // Stu{id=22, age=9}
// 收集年龄最小的值 collect(Collectors.minBy())返回的是一个Optional对象,如果有多个满足条件的就返回一个
Optional<Stu> minAgeOptional = list.stream().collect(Collectors.minBy((p1, p2) -> p1.getAge() - p2.getAge()));
System.out.println(minAgeOptional.get()); // Stu{id=22, age=9}
// 收集年龄总和
Integer sumAge = list.stream().collect(Collectors.summingInt(Stu::getAge));
System.out.println(sumAge); // 32
// 收集年龄均值
Double aveAge = list.stream().collect(Collectors.averagingInt(Stu::getAge));
System.out.println(aveAge); // 8.0
}
}
class Stu{
Integer id;
Integer age;
public Stu(Integer id,Integer age){
this.id=id;
this.age=age;
}
public Integer getId(){
return this.id;
}
public Integer getAge(){
return this.age;
}
@Override
public String toString() {
return "Stu{" +
"id=" + id +
", age=" + age +
'}';
}
}
import java.util.stream.DoubleStream;
import java.util.stream.Stream;
/**
* @Author jiangxuzhao
* @Description 完成 Stream到DoubleStream的转换
* @Date 2023/6/7
*/
public class Demo8Stream_doubleMap {
public static void main(String[] args) {
Stream<String> stream8 = Stream.of("1", "2", "3");
DoubleStream doubleStream = stream8.mapToDouble(m -> Double.parseDouble(m));
doubleStream.forEach(System.out::println);
}
}
import java.util.stream.Stream;
/**
* @Author jiangxuzhao
* @Description
* @Date 2023/6/7
*/
public class Demo9Stream_sorted {
public static void main(String[] args) {
// 升序排列
Stream<Integer> stream9 = Stream.of(5, 4, 3, 2, 1).sorted((o1,o2)->o1-o2);
stream9.forEach(System.out::println);
}
}