Java 8 Stream流练习

布满荆棘的人生 2023-07-17 15:45 56阅读 0赞

题目和答案均来自 Java8实战

题目

(1) 找出2011年发生的所有交易,并按交易额排序(从低到高)。
(2) 交易员都在哪些不同的城市工作过?
(3) 查找所有来自于剑桥的交易员,并按姓名排序。
(4) 返回所有交易员的姓名字符串,按字母顺序排序。
(5) 有没有交易员是在米兰工作的?
(6) 打印生活在剑桥的交易员的所有交易额。
(7) 所有交易中,最高的交易额是多少?
(8) 找到交易额最小的交易。

所用到的类

Trader

  1. @Data
  2. @AllArgsConstructor
  3. public class Trader {
  4. /** * 姓名 * */
  5. private final String name ;
  6. /** * 工作地点 * */
  7. private final String city;
  8. }

Transaction

  1. @Data
  2. @AllArgsConstructor
  3. public class Transaction {
  4. /** * 交易员 * */
  5. private final Trader trader;
  6. /** * 发生交易年限 * */
  7. private final int year;
  8. /** * 交易额 * */
  9. private final int value;
  10. }

初始化数据

  1. public class Test {
  2. Trader raoul = new Trader("Raoul", "Cambridge");
  3. Trader mario = new Trader("Mario","Milan");
  4. Trader alan = new Trader("Alan","Cambridge");
  5. Trader brian = new Trader("Brian","Cambridge");
  6. List<Transaction> transactions = Arrays.asList(
  7. new Transaction(brian, 2011, 300),
  8. new Transaction(raoul, 2012, 1000),
  9. new Transaction(raoul, 2011, 400),
  10. new Transaction(mario, 2012, 710),
  11. new Transaction(mario, 2012, 700),
  12. new Transaction(alan, 2012, 950)
  13. );
  14. }

答案

  1. @org.junit.Test
  2. public void test() {
  3. // 1. 找出2011年发生的所有交易,并按交易额排序(从低到高)
  4. List<Transaction> tr2011 =
  5. transactions.stream()
  6. .filter(transaction -> transaction.getYear() == 2011)
  7. .sorted(comparing(Transaction::getValue))
  8. .collect(toList());
  9. // 2. 交易员都在哪些不同的城市工作过?
  10. List<String> cities =
  11. transactions.stream()
  12. .map(transaction -> transaction.getTrader().getCity())
  13. .distinct()
  14. .collect(toList());
  15. //第二种写法
  16. Set<String> cities1 =
  17. transactions.stream()
  18. .map(transaction -> transaction.getTrader().getCity())
  19. .collect(toSet());
  20. // 3. 查找所有来自于剑桥的交易员,并按姓名排序
  21. List<Trader> traders =
  22. transactions.stream()
  23. .map(Transaction::getTrader)
  24. .filter(trader -> trader.getCity().equals("Cambridge"))
  25. .distinct()
  26. .sorted(comparing(Trader::getName))
  27. .collect(toList());
  28. // 4. 返回所有交易员的姓名字符串,按字母顺序排序
  29. String traderStr =
  30. transactions.stream()
  31. .map(transaction -> transaction.getTrader().getName())
  32. .distinct()
  33. .sorted()
  34. .reduce("", (n1, n2) -> n1 + n2);
  35. String traderStr1 =
  36. transactions.stream()
  37. .map(transaction -> transaction.getTrader().getName())
  38. .distinct()
  39. .sorted()
  40. .collect(joining());
  41. // 5. 有没有交易员是在米兰工作的?
  42. boolean milanBased =
  43. transactions.stream()
  44. .anyMatch(transaction -> transaction.getTrader()
  45. .getCity()
  46. .equals("Milan"));
  47. // 6. 打印生活在剑桥的交易员的所有交易额
  48. transactions.stream()
  49. .filter(t -> "Cambridge".equals(t.getTrader().getCity()))
  50. .map(Transaction::getValue)
  51. .forEach(System.out::println);
  52. // 7. 所有交易中,最高的交易额是多少?
  53. Optional<Integer> highestValue =
  54. transactions.stream()
  55. .map(Transaction::getValue)
  56. .reduce(Integer::max);
  57. // 8. 找到交易额最小的交易。
  58. Optional<Transaction> smallestTransaction =
  59. transactions.stream()
  60. .reduce((t1, t2) ->
  61. t1.getValue() < t2.getValue() ? t1 : t2);
  62. Optional<Transaction> smallestTransaction1 =
  63. transactions.stream()
  64. .min(comparing(Transaction::getValue));
  65. }

发表评论

表情:
评论列表 (有 0 条评论,56人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Java8——Stream

    `Stream`是数据渠道,用于操作集合、数组等生成的元素序列。 `Stream`操作的三个步骤: 创建Stream 中间操作 终止操作 一、获取st

    相关 Java8 Stream操作

    java8中的流式操作是一个很重要的内容 ![stream主要流操作][stream] 上图列出了stream主要流操作,我们可以看到,其实流操作可以类比我们的sql