Java8 Stream流常用方法及例子
交易员和交易的实体类的定义如下:
Trader.class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | public class Trader { private String name; private String city; public Trader(String n, String c) { this .name = n; this .city = c; } public String getName() { return this .name; } public String getCity() { return this .city; } public void setCity(String newCity) { this .city = newCity; } public String toString() { return "Trader:" + this .name + " in " + this .city; } } |
Transaction.class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | public class Transaction { private Trader trader; private int year; private int value; public Transaction(Trader trader, int year, int value) { this .trader = trader; this .year = year; this .value = value; } public Trader getTrader() { return this .trader; } public int getYear() { return this .year; } public int getValue() { return this .value; } public String toString() { return "{" + this .trader + ", " + "year: " + this .year + ", " + "value:" + this .value + "}" ; } } |
问题:
1 2 3 4 5 6 7 8 | ( 1 ) 找出 2011 年发生的所有交易,并按交易额排序(从低到高)。 ( 2 ) 交易员都在哪些不同的城市工作过? ( 3 ) 查找所有来自于剑桥的交易员,并按姓名排序。 ( 4 ) 返回所有交易员的姓名字符串,按字母顺序排序。 ( 5 ) 有没有交易员是在米兰工作的? ( 6 ) 打印生活在剑桥的交易员的所有交易额。 ( 7 ) 所有交易中,最高的交易额是多少? ( 8 ) 找到交易额最小的交易。 |
解答:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | import java.util.Arrays; import java.util.List; import java.util.Optional; import static java.util.Comparator.comparing; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList; public class PuttingIntoPractice { public static void main(String[] args) { Trader raoul = new Trader( "Raoul" , "Cambridge" ); Trader mario = new Trader( "Mario" , "Milan" ); Trader alan = new Trader( "Alan" , "Cambridge" ); Trader brian = new Trader( "Brian" , "Cambridge" ); List<Transaction> transactions = Arrays.asList( new Transaction(brian, 2011 , 300 ), new Transaction(raoul, 2012 , 1000 ), new Transaction(raoul, 2011 , 400 ), new Transaction(mario, 2012 , 710 ), new Transaction(mario, 2012 , 700 ), new Transaction(alan, 2012 , 950 ) ); // Query 1: Find all transactions from year 2011 and sort them by value (small to high). List<Transaction> tr2011 = transactions.stream() .filter(transaction -> transaction.getYear() == 2011 ) .sorted(comparing(Transaction::getValue)) .collect(toList()); System.out.println(tr2011); // Query 2: What are all the unique cities where the traders work? List<String> cities = transactions.stream() .map(transaction -> transaction.getTrader().getCity()) .distinct() .collect(toList()); System.out.println(cities); // Query 3: Find all traders from Cambridge and sort them by name. List<Trader> traders = transactions.stream() .map(Transaction::getTrader) .filter(trader -> trader.getCity().equals( "Cambridge" )) .distinct() .sorted(comparing(Trader::getName)) .collect(toList()); System.out.println(traders); // Query 4: Return a string of all traders’ names sorted alphabetically. String traderStr = transactions.stream() .map(transaction -> transaction.getTrader().getName()) .distinct() .sorted() .reduce( "" , (n1, n2) -> n1 + n2); System.out.println(traderStr); // 请注意,此解决方案效率不高(所有字符串都被反复连接,每次迭代的时候都要建立一个新的String对象)。 // 这里有一个更为高效的解决方案,它像下面这样使用joining(其内部会用到StringBuilder): String traderStr_2 = transactions.stream() .map(transaction -> transaction.getTrader().getName()) .distinct() .sorted() .collect(joining()); // Query 5: Are there any trader based in Milan? boolean milanBased = transactions.stream() .anyMatch(transaction -> transaction.getTrader() .getCity() .equals( "Milan" ) ); System.out.println(milanBased); // Query 6: Update all transactions so that the traders from Milan are set to Cambridge. transactions.stream() .map(Transaction::getTrader) .filter(trader -> trader.getCity().equals( "Milan" )) .forEach(trader -> trader.setCity( "Cambridge" )); System.out.println(transactions); // Query 7: What's the highest value in all the transactions? int highestValue = transactions.stream() .map(Transaction::getValue) .reduce( 0 , Integer::max); System.out.println(highestValue); // tips:流支持min和max方法,它们可以接受一个Comparator作为参数,指定计算最小或最大值时要比较哪个键值: Optional<Transaction> smallestTransaction = transactions.stream() .min(comparing(Transaction::getValue)); } } |
本博客文章均已测试验证,欢迎评论、交流、点赞。
部分文章来源于网络,如有侵权请联系删除。
转载请注明原文链接:https://www.cnblogs.com/sueyyyy/p/12142733.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
2019-01-02 使用mysql将手机号、身份证号等敏感字段替换成*号
2019-01-02 Java使用J4L识别验证码
2019-01-02 FastStone Capture激活码