Stream是一系列元素。
聚合操作分为三步:
1.创建一个Stream
2.中间操作
fliter/distinct/sorted/limit/skip
3.结束操作
forEach/toArray/min/max/count/findFirst
中间操作程序例子:
package 聚合操作;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import 匿名方法.Hero;
public class TestAggregate2 {
public static void main(String[] args) {
List heros = new ArrayList<>();
Random r = new Random();
for(int i=0;i<5;i++){
heros.add(new Hero("hero"+i,r.nextInt(1000),r.nextInt(100)));
}
heros.add(heros.get(0));
System.out.println("初始化后的数据:");
System.out.println(heros);
//1.filter
System.out.println("满足条件hp>100&&damage<50的数据");
heros
.stream()
.filter(h->h.hp>100&&h.damage<50)
.forEach(h->System.out.println("name :"+h.name+" hp:"+h.hp+" damage:"+h.damage));
//2.distinct 去除重复
System.out.println("去除重复的数据,去除标准是看equals");
heros
.stream()
.distinct()
.forEach(h->System.out.println(h));
//3.sorted
System.out.println("按照血量排序");
heros
.stream()
.sorted((h1,h2)->h1.hp>=h2.hp?1:-1)
.forEach(h->System.out.println(h));
//4.limit 保留
System.out.println("保留3个");
heros
.stream()
.limit(3)
.forEach(h->System.out.println(h));
//5.skip 忽略前3个
System.out.println("忽略前3个");
heros
.stream()
.skip(3)
.forEach(h->System.out.println(h));
//6.转换
System.out.println("转换为double的Stream");
heros
.stream()
.mapToDouble(Hero::getHp)
.forEach(h->System.out.println(h));
//7.
System.out.println("转换任意类型的Stream");
heros
.stream()
.map(h->h.name+" "+h.hp+" "+h.damage)
.forEach(h->System.out.println(h));
}
}
结束操作程序例子:
package 聚合操作;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import 匿名方法.Hero;
public class TestAggregate3 {
public static void main(String[] args) {
List heros = new ArrayList<>();
Random r = new Random();
for(int i=0;i<5;i++){
heros.add(new Hero("hero"+i,r.nextInt(1000),r.nextInt(100)));
}
System.out.println("初始化后的数据:");
System.out.println(heros);
System.out.println("遍历集合中的每个数据");
heros
.stream()
.forEach(h->System.out.println(h));
System.out.println("返回一个数组");
Object[] hs = heros
.stream()
.toArray();
System.out.println(Arrays.toString(hs));
System.out.println("返回伤害最低的那个英雄");
Hero Damagaminhero=
heros
.stream()
.min((h1,h2)->h1.damage-h2.damage)
.get();
System.out.println(Damagaminhero);
System.out.println("返回伤害最高的那个英雄");
Hero DamageMaxHero =
heros
.stream()
.max((h1,h2)->h1.damage-h2.damage)
.get();
System.out.println(DamageMaxHero);
System.out.println("流中数据的总数");
long count = heros
.stream()
.count();
System.out.println(count);
System.out.println("第一个英雄");
Hero firstHero =
heros
.stream()
.findFirst()
.get();
System.out.println(firstHero);
}
}
综合应用的简单示例:
package 聚合操作;
//首选准备10个Hero对象,hp和damage都是随机数。
//分别用传统方式和聚合操作的方式,把hp第三高的英雄名称打印出来
import java.util.*;
import 匿名方法.Hero;
public class TestAggregate4 {
public static void main(String[] args) {
Random r = new Random();
List heros = new ArrayList<>();
for(int i=0;i<10;i++){
heros.add(new Hero("hero"+i,r.nextInt(1000),r.nextInt(100)));
}
System.out.println(heros);
//1.传统方式,比较器
Comparator c = new Comparator(){
public int compare(Hero h1,Hero h2){
return h1.hp>=h2.hp?-1:1;
}
};
Collections.sort(heros,c);
System.out.println(heros.get(2));
//2.聚合方式
// Hero hs1 = heros
// .stream()
// .sorted((h1,h2)->h1.hp>=h2.hp?-1:1) //由大到小排序
// .skip(2)
// .findFirst()
// .get();
// System.out.println(hs1);
}
}