找出2011年的所有交易并按交易额排序(从低到高)

List tr2011 =
transactions.stream()
.filter(transaction -> transaction.getYear() == 2011) ←─给filter传递一个谓词来选择2011年的交易
.sorted(comparing(Transaction::getValue)) ←─按照交易额进行排序
.collect(toList()); ←─将生成的Stream中的所有元素收集到一个List中

交易员都在哪些不同的城市工作过

List cities =
transactions.stream()
.map(transaction -> transaction.getTrader().getCity()) ←─提取与交易相关的每位交易员的所在城市
.distinct() ←─只选择互不相同的城市
.collect(toList());

这里还有一个新招:你可以去掉distinct(),改用toSet(),这样就会把流转换为集合

Set cities =
transactions.stream()
.map(transaction -> transaction.getTrader().getCity())
.collect(toSet());

查找所有来自于剑桥的交易员,并按姓名排序

List traders =
transactions.stream()
.map(Transaction::getTrader) ←─从交易中提取所有交易员
.filter(trader -> trader.getCity().equals(“Cambridge”)) ←─仅选择位于剑桥的交易员
.distinct() ←─确保没有任何重复
.sorted(comparing(Trader::getName)) ←─对生成的交易员流按照姓名进行排序
.collect(toList());

返回所有交易员的姓名字符串,按字母顺序排序

String traderStr =
transactions.stream()
.map(transaction -> transaction.getTrader().getName()) ←─提取所有交易员姓名,生成一个Strings构成的Stream
.distinct() ←─只选择不相同的姓名
.sorted() ←─对姓名按字母顺序排序
.reduce(“”, (n1, n2) -> n1 + n2); ←─逐个拼接每个名字,得到一个将所有名字连接起来的String

请注意,此解决方案效率不高(所有字符串都被反复连接,每次迭代的时候都要建立一个新的String对象)。下一章中,你将看到一个更为高效的解决方案,它像下面这样使用joining(其内部会用到StringBuilder):

String traderStr =
transactions.stream()
.map(transaction -> transaction.getTrader().getName())
.distinct()
.sorted()
.collect(joining());

有没有交易员是在米兰工作的

boolean milanBased =
transactions.stream()
.anyMatch(transaction -> transaction.getTrader()
.getCity()
.equals(“Milan”)); ←─把一个谓词传递给anyMatch,检查是否有交易员在米兰工作

打印生活在剑桥的交易员的所有交易额

transactions.stream()
.filter(t -> “Cambridge”.equals(t.getTrader().getCity())) ←─选择住在剑桥的交易员所进行的交易
.map(Transaction::getValue) ←─提取这些交易的交易额
.forEach(System.out::println); ←─打印每个值

所有交易中,最高的交易额是多少

Optional highestValue =
transactions.stream()
.map(Transaction::getValue) ←─提取每项交易的交易额
.reduce(Integer::max); ←─计算生成的流中的最大值

找到交易额最小的交易

Optional smallestTransaction =
transactions.stream()
.reduce((t1, t2) ->
t1.getValue() < t2.getValue() ? t1 : t2); ←─通过反复比较每个交易的交易额,找出最小的交易

你还可以做得更好。流支持min和max方法,它们可以接受一个Comparator作为参数,指定计算最小或最大值时要比较哪个键值:

Optional smallestTransaction =
transactions.stream()
.min(comparing(Transaction::getValue));