Java 8 API添加了一个新的抽象称为流Stream,可以让你以一种声明的方式处理数据。
Stream 使用一种类似用 SQL 语句从数据库查询数据的直观方式来提供一种对 Java 集合运算和表达的高阶抽象。
Stream API可以极大提高Java程序员的生产力,让程序员写出高效率、干净、简洁的代码。
这种风格将要处理的元素集合看作一种流, 流在管道中传输, 并且可以在管道的节点上进行处理, 比如筛选, 排序,聚合等。
元素流在管道中经过中间操作(intermediate operation)的处理,最后由最终操作(terminal operation)得到前面处理的结果。
static ArrayList<people> peoples = new ArrayList<>();
static {
for (int i = 0; i < 100; i++) {
peoples.add(new people("sjy" + i, i + 10));
peoples.add(new people("sjy1" + i, i + 10));
}
}
static class people {
String name;
int age;
public int getAge() {
return age;
}
public people(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "people{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
Stream接口提供了很多方法,经常使用的有以下几种:迭代、条件过滤、将流转换成集合和聚合元素、获取指定数量的流等等
filter
filter() API给定的解释是:返回由与此给定谓词匹配的此流的元素组成的流。 方法用于通过设置的条件过滤出元素,其实就是条件过滤
条件:获取age大于等于100的用户信息
//filter是条件,参数使用Lambda表达式
List<people> collect = peoples.stream().filter(people -> people.getAge() >= 100).collect(Collectors.toList());
System.out.println(collect.size()); // 输出结果为20
map
map 方法用于映射每个元素到对应的结果,看代码就可以理解
条件:获取所有的年龄,获取所有的用户
//从代码可以看出getAge()方法的返回值是int类型,所以List
List<Integer> collect = peoples.stream().map(people -> {
return people.getAge();
}).collect(Collectors.toList());
System.out.println(collect);
//从代码可以看出toString()方法的返回值是String类型,所以List
List<String> collect1 = peoples.stream().map(people::toString).collect(Collectors.toList());
System.out.println(collect1);
sorted
sorted 方法用于对流进行排序
其实使用peoples.stream().forEach(System.out::println)遍历出的顺序跟添加时候的是不一样的,
stream() − 为集合创建串行流。
parallelStream() − 为集合创建并行流。
条件:按照年龄的顺序排序
System.out.println(peoples);
List<people> collect = peoples.stream().sorted(Comparator.comparingInt(people::getAge)).collect(Collectors.toList());
System.out.println(collect);
List<people> collect2 = peoples.stream().sorted((o1, o2) -> o2.getAge()-o1.getAge()).collect(Collectors.toList());
System.out.println(collect2);
limit
limit 方法用于获取指定数量的流
List<people> collect = peoples.stream().limit(3).collect(Collectors.toList());
System.out.println(collect.size());//输出结果 3
forEach
peoples.stream().forEach(people -> System.out.println(people.toString()));
collect
可以发现所有的方法后面都跟着这个collect方法。该方法的条件是Collector类,
Collectors 类实现了很多归约操作,例如将流转换成集合和聚合元素。Collectors 可用于返回列表或字符串:
其实就是执行map检索的元素的流转化成相应的数据格式,如Set List Map等
Set<Integer> collect = peoples.stream().map(people::getAge).collect(Collectors.toSet());
System.out.println(collect.size());
List<Integer> collect1 = peoples.stream().map(people::getAge).collect(Collectors.toList());
System.out.println(collect1.size());
添加一个分组的方法
条件:根据相同的年龄进行分组
大家知道对list泛型的某个属性进行分组的时候仅仅一行代码是不能完成的,就写出Java1.7之前的方法了,直接使用Java1.8中的新特性,是不是真香·······
Map<Integer, List<people>> collect = peoples.stream().collect(Collectors.groupingBy(people::getAge));
System.out.println(collect.size());
如果使用过MyBatis-plus的知道,这个跟MyBatis中的构造器很像,我们直接加一系列的条件,获得自己想要的结果。
public class Stream_Demo {
static ArrayList<people> peoples = new ArrayList<>();
static {
for (int i = 0; i < 100; i++) {
peoples.add(new people("sjy" + i, i + 10));
peoples.add(new people("sjy1" + i, i + 10));
}
}
static class people {
String name;
int age;
public int getAge() {
return age;
}
public people(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "people{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
public static void main(String[] args){
//filter是条件,参数使用Lambda表达式
/*List collect = peoples.stream().filter(people -> people.getAge() >= 100).collect(Collectors.toList());
System.out.println(collect.size());*/
/*List collect = peoples.stream().map(people -> {
return people.getAge();
}).collect(Collectors.toList());
System.out.println(collect);
List collect1 = peoples.stream().map(people::toString).collect(Collectors.toList());
System.out.println(collect1);*/
/*System.out.println(peoples);
List collect = peoples.stream().sorted(Comparator.comparingInt(people::getAge)).collect(Collectors.toList());
System.out.println(collect);
List collect2 = peoples.stream().sorted((o1, o2) -> o2.getAge()-o1.getAge()).collect(Collectors.toList());
System.out.println(collect2);*/
/*List collect = peoples.stream().limit(3).collect(Collectors.toList());
// System.out.println(collect.size());
long end,start;
peoples.stream().forEach(people::toString);*/
/*Set collect = peoples.stream().map(people::getAge).collect(Collectors.toSet());
System.out.println(collect.size());
List collect1 = peoples.stream().map(people::getAge).collect(Collectors.toList());
System.out.println(collect1.size());*/
Map<Integer, List<people>> collect = peoples.stream().collect(Collectors.groupingBy(people::getAge));
System.out.println(collect.size());
}
}