public static void main(String[] args) {
Random random = new Random();
List<Hero> heros = new ArrayList<Hero>();
for (int i = 0; i < 10; i++) {
heros.add(new Hero("hero" + i, random.nextInt(1000), random.nextInt(1000)));
}
System.out.println("初始化后的集合:");
System.out.println(heros);
System.out.println("查询条件:hp>100 && damage< 50");
System.out.println("通过传统操作方式找出数据");
for (Hero hero : heros) {
if (hero.getFloHp() > 100 && hero.getIntDamage() < 50) {
System.out.println(hero.getVcName());
}
}
System.out.println("通过聚合操作方式找出满足条件的数据");
heros
.stream()
.filter(hero-> hero.getFloHp()>100 && hero.getIntDamage()<50)
.forEach(hero-> System.out.println(hero.getVcName()));
}
管道源
Random random = new Random();
List<Hero> heros = new ArrayList<Hero>();
for (int i = 0; i < 5; i++) {
heros.add(new Hero("hero" + i, random.nextInt(1000), random.nextInt(1000)));
}
heros.stream().forEach(h -> System.out.println(h.getVcName()));
Hero[] hs = heros.toArray(new Hero[heros.size()]);
Arrays.stream(hs).forEach(h -> System.out.println(h.getVcName()));
中间操作
Random random = new Random();
List<Hero> heros = new ArrayList<Hero>();
for (int i = 0; i < 5; i++) {
heros.add(new Hero("hero" + i, random.nextInt(1000), random.nextInt(1000)));
}
heros.add(heros.get(0));
System.out.println("初始化集合后的数据----------------------");
System.out.println(heros);
System.out.println("满足条件 hp>100&&damage<50的数据");
heros
.stream()
.filter(h-> h.getFloHp()>100 && h.getIntDamage()<50)
.forEach(h-> System.out.println(h));
System.out.println("去除重复的数据");
heros
.stream()
.distinct()
.forEach(h-> System.out.println(h));
System.out.println("按照血量排序");
heros
.stream()
.sorted((h1, h2)-> h1.getFloHp() >= h2.getFloHp() ? 1 : -1)
.forEach(h -> System.out.println(h));
System.out.println("保留3个");
heros
.stream()
.limit(3)
.forEach(h -> System.out.println(h));
System.out.println("忽略前3个");
heros
.stream()
.skip(3)
.forEach(h -> System.out.println(h));
System.out.println("转换为double的stream");
heros
.stream()
.mapToDouble(Hero::getFloHp)
.forEach(h -> System.out.println(h));
System.out.println("转换为任意类型的Stream");
heros
.stream()
.map((h)-> h.getVcName() + " - " + h.getFloHp() + " - " + h.getIntDamage())
.forEach(h -> System.out.println(h));
结束操作
当进行结束操作后,流就被使用“光”了,无法再被操作。所以这必定是流的最后一个操作。 结束操作不会返回Stream,但是会返回int、float、String、 Collection或者像forEach,什么都不返回,。
结束操作才真正进行遍历行为,前面的中间操作也在这个时候,才真正的执行。
常见结束操作如下:
forEach() 遍历每个元素
toArray() 转换为数组
min(Comparator<T>) 取最小的元素
max(Comparator<T>) 取最大的元素
count() 总数
findFirst() 第一个元素
Random random = new Random();
List<Hero> heros = new ArrayList<Hero>();
for (int i = 0; i < 5; i++) {
heros.add(new Hero("hero" + i, random.nextInt(1000), random.nextInt(1000)));
}
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 minDamageHero = heros
.stream()
.min((h1, h2) -> h1.getIntDamage() - h2.getIntDamage())
.get();
System.out.println(minDamageHero);
System.out.println("返回伤害最高的那个英雄");
Hero maxDamageHero = heros
.stream()
.max((h1, h2) -> h1.getIntDamage() - h2.getIntDamage())
.get();
System.out.println(maxDamageHero);
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);