ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

简单Java流使用

2021-11-03 19:30:21  阅读:177  来源: 互联网

标签:Toy Java String 简单 使用 Child new LocalDate public


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<Toy> 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<Toy> 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<String> cities = toys.stream().map(x -> x.getChild().getCity()).distinct().collect(Collectors.toList());
        cities.forEach(System.out::println);
    }

    //找出所有来自于Shanghai的孩子,并按照姓名排序
    public void listChildFromShanghai() {
        List<Child> 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() {
        List<String> childNameList = toys.stream().map(x -> x.getChild().getName()).distinct().sorted().collect(Collectors.toList());
        childNameList.forEach(System.out::println);
    }

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

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 + " }";
    }
}

标签:Toy,Java,String,简单,使用,Child,new,LocalDate,public
来源: https://blog.csdn.net/weixin_41782646/article/details/121128591

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有