Java 8 API添加了一个新的抽象称为流Stream,可以让你以一种声明的方式处理数据。Stream 使用一种类似用 SQL 语句从数据库查询数据的直观方式来提供一种对 Java 集合运算和表达的高阶抽象。Stream API可以极大提高Java程序员的生产力,让程序员写出高效率、干净、简洁的代码。这种风格将要处理的元素集合看作一种流, 流在管道中传输, 并且可以在管道的节点上进行处理, 比如筛选, 排序,聚合等。元素流在管道中经过中间操作(intermediate operation)的处理,最后由最终操作(terminal operation)得到前面处理的结果。
上代码:
/**
* 创建学生实体类
* @author Administrator
*
*/
public class Student {
/**姓名*/
private String name;
/**学号*/
private int stuNo;
/**名次*/
private int orderNum;
/**分数*/
private int score;
public Student() {
super();
}
public Student(String name, int stuNo, int orderNum, int score) {
super();
this.name = name;
this.stuNo = stuNo;
this.orderNum = orderNum;
this.score = score;
}
}
Stream操作工具类
public class StremUtils {
/**
* 构造列表集合
* @return
*/
public static List<Student> initList() {
List<Student> stuList=new ArrayList<Student>();
Student stu1=new Student("林志玲",1,6,400);
Student stu2=new Student("张钧甯",2,1,650);
Student stu3=new Student("张碧晨",3,4,500);
Student stu4=new Student("赵丽颖",4,3,550);
Student stu5=new Student("刘萍萍",5,5,450);
Student stu6=new Student("刘翠翠",6,2,550);
stuList.add(stu1);
stuList.add(stu2);
stuList.add(stu3);
stuList.add(stu4);
stuList.add(stu5);
stuList.add(stu6);
return stuList;
}
/**
* 构造键值对集合
* @return
*/
public static Map<Integer,Student> initMap() {
Map<Integer,Student> map=new HashMap<Integer, Student>();
Student stu1=new Student("林志玲",1,6,400);
Student stu2=new Student("张钧甯",2,1,650);
Student stu3=new Student("张碧晨",3,4,500);
Student stu4=new Student("赵丽颖",4,3,550);
Student stu5=new Student("刘萍萍",5,5,450);
Student stu6=new Student("刘翠翠",6,2,600);
map.put(1,stu1);
map.put(2,stu2);
map.put(3,stu3);
map.put(4,stu4);
map.put(5,stu5);
map.put(6,stu6);
return map;
}
/**
* stream计算过滤fliter
*/
public static void streamFliter() {
List<Student> initList = initList();
//后去stream计算对象
Stream<Student> stream = initList.stream();
//计算出成绩高于550的学生
Stream<Student> filter = stream.filter(s->s.getScore()>=550);
//终止计算
filter.forEach(s->System.out.println(s.toString()));
}
/**
* stream计算元素映射map,获取指定字段
*/
public static void streamMap() {
List<Student> initList = initList();
//后去stream计算对象
Stream<Student> stream = initList.stream();
//过滤操作 输出学号和名字
stream.map(s->s.getStuNo()+":"+s.getName()).forEach(System.out::println);;
}
/**
* stream计算指定流limit
*/
public static void streamLimit() {
List<Student> initList = initList();
//后去stream计算对象
Stream<Student> stream = initList.stream();
//输出学号和名字
stream.limit(3).forEach(System.out::println);
}
/**
* stream计算排序sorted
*/
public static void streamSorted() {
List<Student> initList = initList();
//后去stream计算对象
Stream<Student> stream = initList.stream();
//按照排名升序输出
stream.sorted((s1,s2)->s1.getOrderNum()-s2.getOrderNum()).forEach(System.out::println);
}
/**
* stream计算并行处理parallel
*/
public static void streamParallel() {
List<Student> initList = initList();
//后去stream计算对象
Stream<Student> stream = initList.parallelStream();
//并行处理流,过滤姓名中含有姓张的
stream.filter(s->s.getName().contains("张")).forEach(System.out::println);
}
/**
* stream计算聚合操作Collectors
*/
public static void streamCollectors() {
List<Student> initList = initList();
//后去stream计算对象
Stream<Student> stream = initList.stream();
//将所有人的名字转为集合
stream.map(s->s.getName()).collect(Collectors.toList());
//将所有人的名字用#拼接
Stream<Student> stream1 = initList.stream();
String collect = stream1.map(s->s.getName()).collect(Collectors.joining("#"));
System.out.println(collect);
}
/**
* stream计算统计SummaryStatistics
* 主要用于int、double、long等基本类型上,它们可以用来产生类似如下的统计结
*/
public static void streamSummaryStatistics() {
List<Student> initList = initList();
//对所有学生的成绩求和
Stream<Student> stream = initList.stream();
IntSummaryStatistics summaryStatistics = stream.mapToInt(s->s.getScore()).summaryStatistics();
long sum = summaryStatistics.getSum();
System.out.println(sum);
}
}
/**
* 扁平化操作 将过个集合降阶到一个集合
*/
public static void streamFlatMap(){
List<List<String>> list = new ArrayList<>();
list.add(CollectionUtils.arrayToList(new String[]{"1","2"}));
list.add(CollectionUtils.arrayToList(new String[]{"3","4","5"}));
list.add(CollectionUtils.arrayToList(new String[]{"6","7","8","9"}));
List<String> collect = list.stream().flatMap(Collection::stream).collect(Collectors.toList());
collect.forEach(System.out::println);
}
运行
public static void main(String[] args) {
StremUtils.streamFliter();
StremUtils.streamMap();
StremUtils.streamLimit();
StremUtils.streamSorted();
StremUtils.streamParallel();
StremUtils.streamCollectors();
StremUtils.streamSummaryStatistics();
}