简单Java流使用
2021/11/3 20:41:40
本文主要是介绍简单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<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 + " }"; } }
这篇关于简单Java流使用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-082024年常用的情绪识别API
- 2025-01-07如何利用看板工具优化品牌内容创作与审批,确保按时发布?
- 2025-01-07百万架构师第十一课:源码分析:Spring 源码分析:Spring源码分析前篇|JavaGuide
- 2025-01-07质量检测标准严苛,这 6 款办公软件达标了吗?
- 2025-01-07提升品牌活动管理的效率:看板工具助力品牌活动日历的可视化管理
- 2025-01-07宠物商场的精准营销秘籍:揭秘看板软件的力量
- 2025-01-07“30了,资深骑手” | 程序员能有什么好出路?
- 2025-01-07宠物公园的营销秘籍:看板软件如何帮你精准触达目标客户?
- 2025-01-07从任务分解到资源优化:甘特图工具全解析
- 2025-01-07企业升级必备指南:从传统办公软件到SaaS工具的转型攻略