栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

简单Java流使用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

简单Java流使用

public class StreamPractice {
    Child liMing = new Child("LiMing", "Shanghai");
    Child yeWen = new Child("IpMan", "FoShan");
    Child liLi = new Child("LiLi", "Shanghai");
    Child liChao = new Child("LiChao", "Shanghai");

    List toys = Arrays.asList(
            new Toy(liChao, LocalDate.of(2021, 1, 1), 50),
            new Toy(yeWen, LocalDate.of(2021, 2, 5), 60),
            new Toy(liLi, LocalDate.of(2020, 7, 12), 150),
            new Toy(liMing, LocalDate.of(2018, 10, 3), 1000),
            new Toy(liChao, LocalDate.of(2021, 2, 8), 20),
            new Toy(yeWen, LocalDate.of(2021, 4, 5), 73)
            );

    //找出所有玩具,并按照玩具的价格从低到高排列
    public void listToySortPrice() {
        List toyList = toys.stream().sorted((o1, o2) -> o1.getPrice() >= o2.getPrice() ? 1 : -1).filter(x -> x.getBuyTime().contains("2021")).collect(Collectors.toList());
        toyList.forEach(System.out::println);
    }

    //找出孩子们所属的城市
    public void listCities() {
        List cities = toys.stream().map(x -> x.getChild().getCity()).distinct().collect(Collectors.toList());
        cities.forEach(System.out::println);
    }

    //找出所有来自于Shanghai的孩子,并按照姓名排序
    public void listChildFromShanghai() {
        List childList = toys.stream().map(Toy::getChild).filter(x -> "Shanghai".equals(x.getCity())).distinct().sorted(Comparator.comparing(Child::getCity)).collect(Collectors.toList());
        childList.forEach(System.out::println);
    }

    //返回所有孩子的名字字符串(组成一个字符串),并按照字母顺序排序
    public void listChildNameSortedByWord() {
        String childNameString = toys.stream().map(x -> x.getChild().getName()).distinct().sorted().reduce("", (n1, n2) -> n1 + n2);
        System.out.println(childNameString);
    }

    //是否有孩子来自佛山
    public void isChildFromFoShan() {
        System.out.println(toys.stream().anyMatch(x -> "FoShan".equals(x.getChild().getCity())));
    }

    //打印生活在剑桥的孩子的玩具的价格
    public void printToyPriceWhichChildFromShanghai() {
        List priceInShanghai = toys.stream().filter(x -> "Shanghai".equals(x.getChild().getCity())).map(Toy::getPrice).collect(Collectors.toList());
        priceInShanghai.forEach(System.out::println);
        //可以直接写成下面的样子
        toys.stream().filter(x -> "Shanghai".equals(x.getChild().getCity())).map(Toy::getPrice).forEach(System.out::println);
    }

    //寻找所有玩具中,最高的价格是多少
    public void mostPriceInToys() {
        System.out.println(toys.stream().map(Toy::getPrice).reduce(Integer::max));
    }

    //找到价格最小玩具
    public void minPriceInToys() {
        System.out.println(toys.stream().reduce((t1, t2) -> t1.getPrice() < t2.getPrice() ? t1 : t2));
    }

    public static void main(String[] args) {
        StreamPractice sp = new StreamPractice();
        sp.listToySortPrice();
        sp.listCities();
        sp.listChildFromShanghai();
        sp.listChildNameSortedByWord();
        sp.isChildFromFoShan();
        sp.printToyPriceWhichChildFromShanghai();
        sp.mostPriceInToys();
        sp.minPriceInToys();
    }
}

class Child {
    private final String name;

    private final String city;

    public Child(String name, String city) {
        this.name = name;
        this.city = city;
    }

    public String getName() {
        return name;
    }

    public String getCity() {
        return city;
    }

    public String toString() {
        return "Child: " + this.name + " in " + this.city;
    }
}

class Toy {
    private final Child child;
    private final LocalDate buyTime;
    private final int price;

    public Toy(Child child, LocalDate buyTime, int price) {
        this.child = child;
        this.buyTime = buyTime;
        this.price = price;
    }

    public Child getChild() {
        return this.child;
    }

    public String getBuyTime() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return buyTime.format(formatter);
    }

    public int getPrice() {
        return this.price;
    }

    public String toString() {
        return "{" + this.child + ", " +
                "buy time: " + this.buyTime + ", " +
                "price: " + this.price + " }";
    }
}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/425253.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号