Stream API
Stream 流是 Java 8 新提供给开发者的一组操作集合的 API,将要处理的元素集合看作一种流, 流在管道中传输, 并且可以在管道的节点上进行处理, 比如筛选、排序、聚合等。
流
终止操作::allMatch,findFirst,count,max,forEach
流迭代
创建流
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
public class Main{
public static void main(String[] args) {
//
//直接创建
Stream<Integer> stream1 =Stream.of(1,2,3,4,5,6,7,8);
//数组转化
int[] num={1,2,3,4,5,6,7,8,9};
Stream<int[]> stream2=Stream.of(num);
//集合转化
List<Integer> list=new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
Stream<List<Integer>> stream3=Stream.of(list);
}
}
API为stream().foreach();
import java.util.ArrayList;
import java.util.List;
public class Main{
public static void main(String[] args) {
//
List<Integer> num=new ArrayList<>();
num.add(1);
num.add(2);
num.add(3);
num.add(4);
num.add(5);
num.stream()
.forEach(System.out::println);
num.stream()
.forEach(a->System.out.println(a));
}
}
流过滤
API为stream().filter();
import java.util.ArrayList;
import java.util.List;
public class Main{
public static void main(String[] args) {
//
List<Integer> num=new ArrayList<>();
num.add(1);
num.add(2);
num.add(3);
num.add(4);
num.add(5);
num.stream()
.filter(a->a>=3)
.forEach(System.out::println);
}
}
流的好处在于一次遍历执行所有操作
流数据映射
API为stream().map();
import java.util.ArrayList;
import java.util.List;
public class Main{
public static void main(String[] args) {
//
List<Integer> num=new ArrayList<>();
num.add(1);
num.add(2);
num.add(3);
num.add(4);
num.add(5);
num.stream()
.map(a->a*a)
//实际上是map(a->{return a*a;})
.forEach(System.out::println);
}
}
如果对对象进行映射,则
import java.util.ArrayList;
import java.util.List;
public class Main{
public static void main(String[] args) {
//
List<Node> num=new ArrayList<>();
num.add(new Node(1,"张三"));
num.add(new Node(2,"李四"));
num.add(new Node(3,"王五"));
num.add(new Node(4,"王二麻子"));
num.add(new Node(5,"二胖"));
num.stream()
.map(a->{
a.x=a.x*a.x;
return a;
})
.forEach(System.out::println);
}
}
class Node{
int x;
String name;
Node(int x,String name){
this.x=x;
this.name=name;
}
//重写tostring方法进行验证
@Override
public String toString() {
return "Node{" +
"x=" + x +
", name='" + name + '\'' +
'}';
}
}
流排序
API为stream().sort();
import java.util.ArrayList;
import java.util.List;
public class Main{
public static void main(String[] args) {
//
List<Node> num=new ArrayList<>();
num.add(new Node(1,"张三"));
num.add(new Node(2,"李四"));
num.add(new Node(3,"王五"));
num.add(new Node(4,"王二麻子"));
num.add(new Node(5,"二胖"));
num.stream()
// 以x为排序对象倒序
.sorted((o1,o2)->{
return o2.x-o1.x;
})
.forEach(System.out::println);
}
}
class Node{
int x;
String name;
Node(int x,String name){
this.x=x;
this.name=name;
}
@Override
public String toString() {
return "Node{" +
"x=" + x +
", name='" + name + '\'' +
'}';
}
}
流数据摘取
API为stream().limit(int x);
import java.util.ArrayList;
import java.util.List;
public class Main{
public static void main(String[] args) {
//
List<Node> num=new ArrayList<>();
num.add(new Node(1,"张三"));
num.add(new Node(2,"李四"));
num.add(new Node(3,"王五"));
num.add(new Node(4,"王二麻子"));
num.add(new Node(5,"二胖"));
num.stream()
.sorted((o1,o2)->{
return o2.x-o1.x;
})
.limit(3)
.forEach(System.out::println);
}
}
class Node{
int x;
String name;
Node(int x,String name){
this.x=x;
this.name=name;
}
@Override
public String toString() {
return "Node{" +
"x=" + x +
", name='" + name + '\'' +
'}';
}
}