Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. package Java_FP.salesFP;
  2.  
  3. import java.util.*;
  4. import java.util.function.Supplier;
  5. import java.util.stream.Collectors;
  6. import java.util.stream.Stream;
  7.  
  8. /**
  9. * Created by liuyufei on 19/10/16.
  10. */
  11. public class TodaySales {
  12.  
  13. static final List<Sale> sales = Arrays.asList(
  14. new Sale(Store.KANSAS_CITY, new Date(), Optional.of("Sarah"),
  15. Arrays.asList(
  16. new Item("carrot", 12.00)
  17. )),
  18. new Sale(Store.ST_LOUIS, new Date(), Optional.empty(),
  19. Arrays.asList(
  20. new Item("carrot", 12.00),
  21. new Item("lizard", 99.90),
  22. new Item("cookie", 1.99)
  23. )),
  24. new Sale(Store.ST_LOUIS, new Date(), Optional.of("Jamie"),
  25. Arrays.asList(
  26. new Item("banana", 3.49),
  27. new Item("cookie", 1.49)
  28. ))
  29. );
  30.  
  31. //cause stream only can be applied once
  32. //so each time we ask for a new stream to handle
  33. static Stream<Sale> saleStream() {
  34. return sales.stream();
  35. }
  36.  
  37. public static void main(String[] args) {
  38. //how many sales?
  39. long saleCount = saleStream().count();
  40. System.out.println(saleCount);
  41.  
  42. //any sales over $100,
  43. //anyMatch stop as soon as it encounter a sale total is bigger than 100
  44. //short-circuiting terminal operation
  45. boolean bigSaleDay = saleStream().anyMatch(sale -> sale.total() > 100.00);
  46. System.out.println("Big sale day? " + bigSaleDay);
  47.  
  48. //statistic about the sale
  49. DoubleSummaryStatistics stats =
  50. saleStream().mapToDouble(Sale::total).summaryStatistics();
  51. System.out.println("Max sale amount " + stats.getMax());
  52. System.out.println("Max sale amount " + stats);
  53.  
  54. //how many items were sold today?
  55. //flatMap used to combine all items stream together
  56. Supplier<Stream<Item>> itemStream = () ->
  57. saleStream().flatMap(sale -> sale.getItems().stream());
  58.  
  59. long itemCount = itemStream.get().count();
  60. System.out.println("Count of items " + itemCount);
  61.  
  62. //how many different items were sold today, by name
  63. long uniqueItemCount = itemStream.get().map(item -> item.getName()).distinct().count();
  64. System.out.println("Count of items " + uniqueItemCount);
  65.  
  66. //distinct item names
  67. List<String> uniqueItem = itemStream.get().map(item -> item.getName())
  68. .distinct().collect(Collectors.toList());
  69. System.out.println("Distinct items' name " + uniqueItem);
  70.  
  71. //join all distinct item names together
  72. String uniqueItemStr = itemStream.get().map(item -> item.getName())
  73. .distinct().collect(Collectors.joining(" & "));
  74. System.out.println("Distinct items' name " + uniqueItemStr);
  75.  
  76.  
  77. //summarize sales by store, collect to a Map
  78. Map<Store, DoubleSummaryStatistics> summary =
  79. saleStream().collect(Collectors.groupingBy(sale -> sale.getStore(),
  80. Collectors.summarizingDouble(Sale::total)));
  81. System.out.println("Summary by store: "+summary);
  82. summary.keySet().stream().forEach(store ->
  83. System.out.println(store + " stats "+summary.get(store))
  84. );
  85.  
  86.  
  87. System.out.println("-----------parallel-----------");
  88.  
  89. //summarize sales by store, collect to a Map in parallel
  90. Map<String, DoubleSummaryStatistics> summaryPara =
  91. saleStream().parallel().collect(Collectors.groupingBy(sale -> Thread.currentThread().getName(),
  92. Collectors.summarizingDouble(Sale::total)));
  93. System.out.println("Summary by store: "+summaryPara);
  94. summaryPara.keySet().stream().sorted().forEach(store ->
  95. System.out.println(store + " stats "+summaryPara.get(store))
  96. );
  97. }
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement