Stream的特性
- 流不存储数据,只是处理数据。
- 惰性执行,只要达到目标即中断执行, 例如 || 和 && 运算符, || 运算符即是惰性执行。
- 通常使用使用for或者迭代器迭代集合为外部迭代,Stream则为内部迭代。
Stream的分类
- 串行流 stream()
- 并行流 parallelStream()
Stream的创建
-
创建一个空的流
Stream<Object> empty = Stream.empty();
-
为List创建流
//串行流 Stream<Object> stream = new ArrayList<>().stream(); //并行流 Stream<Object> stream = new ArrayList<>().parallelStream();
-
为数组创建流
//串行流 Stream<String> stream = Stream.of(new String[]{}); Stream<String> stream = Stream.of("1","2","3","4","5","6","7"); Stream<String> stream = Arrays.stream(new String[]{}); Stream<String> stream = Arrays.stream(new String[]{},0,1); //并行流 Stream<String> stream = Stream.of(new String[]{}).parallel(); Stream<String> stream = Stream.of("1","2","3","4","5").parallel(); Stream<String> stream = Arrays.stream(new String[]{}).parallel(); Stream<String> stream = Arrays.stream(new String[]{},0,1).parallel();
Stream的使用
- forEach() 用于迭代流中的元素
//迭代list new ArrayList<>().forEach(System.out :: println); //迭代数组 Stream.of(new String[]{}).forEach(System.out::println);
- map() 用于返回流中每个元素的运算结果
//获取list集合中每个元素的Hash码 Stream<Integer> map = new ArrayList<>().stream().map(object -> object.hashCode());
- filter() 用于过滤流中的每个元素返回过滤后的结果
//过滤list中的每个元素,返回长度大于3的元素 Stream<String> filter = new ArrayList<String>().stream().filter(chars -> chars.length() > 3);
- limit() 用于截取流中指定数量的元素
//返回list中的前7位元素 Stream<String> limit = new ArrayList<String>().stream().limit(7);
- sorted() 用于对流中的元素进行排序
//生成20位随机数, 升序排序 IntStream sorted = new Random().ints().limit(20).sorted();
- collect() 将流类型转换为其他类型
//将流转换为list List<Object> collect = Stream.empty().collect(Collectors.toList()); //将流中的元素以|分割,转换为字符串 String collect = Stream.empty().map(object -> object.toString()).collect(Collectors.joining("|"));
Stream的实例
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class App {
private final static ArrayList<User> userList = new ArrayList<>();
static{
userList.add(new App().new User("索之奇",23, "man", 100, "Programmer"));
userList.add(new App().new User("白眼狼",23, "girl", 100, "Teacher"));
userList.add(new App().new User("张三丰",83, "man", 100, "TaoistPriest"));
userList.add(new App().new User("李四光",23, "man", 100, "Geologist"));
}
public static void main(String[] args) {
//迭代list集合, 打印每个元素
userList.forEach(user -> System.out.println(user));
System.out.println("==============分隔==============");
//将list集合中所有用户的年龄变为18, 返回新的集合
Stream<User> map = userList.stream().map(user -> {user.setAge(18);return user;});
map.collect(Collectors.toList()).forEach(user -> System.out.println(user));
System.out.println("==============分隔==============");
//将list集合中所有男性用户的年龄变为81, 返回新的集合
Stream<User> filterMap = userList.stream().filter(user -> user.getSex().equals("man"));
Stream<User> map2 = filterMap.map(user -> {user.setAge(81);return user;});
map2.collect(Collectors.toList()).forEach(user -> System.out.println(user));
System.out.println("==============分隔==============");
//从list集合中截取一条用户
List<User> limitCollect = userList.stream().limit(1).collect(Collectors.toList());
limitCollect.forEach(user -> System.out.println(user));
System.out.println("==============分隔==============");
//对List中的用户按照年龄升序排序(注意!!!数据模型User必须实现Comparable并且覆写compareTo()函数)
Stream<User> sorted = userList.stream().sorted();
sorted.forEach(user -> System.out.println(user));
System.out.println("==============分隔==============");
//将List集合转换为字符串, 以 | 分隔
String collect = userList.stream().map(user -> user.toString()).collect(Collectors.joining("|"));
System.out.println(collect);
}
public class User implements Comparable<User>{
private String name;
private Integer age;
private String sex;
private Integer levelOfAppearance;
private String occupation;
public User(String name, Integer age, String sex, Integer levelOfAppearance, String occupation) {
super();
this.name = name;
this.age = age;
this.sex = sex;
this.levelOfAppearance = levelOfAppearance;
this.occupation = occupation;
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + ", sex=" + sex + ", levelOfAppearance=" + levelOfAppearance
+ ", occupation=" + occupation + "]";
}
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 String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getLevelOfAppearance() {
return levelOfAppearance;
}
public void setLevelOfAppearance(Integer levelOfAppearance) {
this.levelOfAppearance = levelOfAppearance;
}
public String getOccupation() {
return occupation;
}
public void setOccupation(String occupation) {
this.occupation = occupation;
}
@Override
public int compareTo(User o) {
return this.age.compareTo(o.age);
}
}
}