java基础——常用聚合操作

package lambda;

import character.Hero;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class TestAggregate {
    public static void main(String[] args){
        Random r = new Random();
        List<Hero> heros = new ArrayList<>();
        for(int i=0;i<8;i++){
            heros.add(new Hero("hero-"+i,r.nextInt(200),r.nextInt(100)));
        }
        heros.add(heros.get(0));
        System.out.println("初始化的集合:");
        System.out.println(heros);

        System.out.println("hp>100&&damage<50的集合:");
        heros
                .stream()
                .filter(h->h.hp>100&&h.damage<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.hp>h2.hp?1:-1)
                .forEach(h->System.out.println(h));

        System.out.println("保留前三个:");
        heros.stream()
                .limit(3)
                .forEach(h->System.out.println(h));

        System.out.println("忽略前三个:");
        heros
                .stream()
                .skip(3)
                .forEach(h->System.out.println(h));

        System.out.println("转换为double的Stream:");
        heros
                .stream()
                .mapToDouble(Hero::getHp)
                .forEach(h->System.out.println(h));

        System.out.println("转换为任意类型的stream:");
        heros
                .stream()
                .map((h)->h.name+" - "+h.hp+" - "+h.damage)
                .forEach(h->System.out.println(h));

        System.out.println("返回一个数组:");
        Object[] hs = heros
                .stream()
                .toArray();
        System.out.println(Arrays.toString(hs));

        System.out.println("返回伤害最低的:");
        Hero minHero = heros
                .stream()
                .min((h1,h2)->h1.damage-h2.damage)
                .get();
        System.out.println(minHero);

        System.out.println("返回伤害最高的:");
        Hero maxHero = heros
                .stream()
                .max((h1,h2)->h1.damage-h2.damage)
                .get();
        System.out.println(maxHero);

        System.out.println("流中的个数:");
        long count = heros
                .stream()
                .count();
        System.out.println(count);

        System.out.println("第一个英雄:");
        Hero first = heros
                .stream()
                .findFirst()
                .get();
        System.out.println(first);

        Hero h = heros
                .stream()
                .sorted((h1,h2)->h1.hp>h2.hp?-1:1)
                .skip(2)
                .findFirst()
                .get();
        System.out.println("血量第三高:"+h);

    }
}

发布了140 篇原创文章 · 获赞 11 · 访问量 1万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 我行我“速” 设计师: Amelia_0503

分享到微信朋友圈

×

扫一扫,手机浏览