| 7 | |
| 8 | public class CollectionToStream { |
| 9 | public static void main(String[] args) { |
| 10 | List<Bubble> bubbles = Arrays.asList( |
| 11 | new Bubble(1), new Bubble(2), new Bubble(3)); |
| 12 | System.out.println( |
| 13 | bubbles.stream() |
| 14 | .mapToInt(b -> b.i) |
| 15 | .sum()); |
| 16 | |
| 17 | Set<String> w = new HashSet<>(Arrays.asList( |
| 18 | "It's a wonderful day for pie!".split(" "))); |
| 19 | w.stream() |
| 20 | .map(x -> x + " ") |
| 21 | .forEach(System.out::print); |
| 22 | System.out.println(); |
| 23 | |
| 24 | Map<String, Double> m = new HashMap<>(); |
| 25 | m.put("pi", 3.14159); |
| 26 | m.put("e", 2.718); |
| 27 | m.put("phi", 1.618); |
| 28 | m.entrySet().stream() |
| 29 | .map(e -> e.getKey() + ": " + e.getValue()) |
| 30 | .forEach(System.out::println); |
| 31 | } |
| 32 | } |
| 33 | /* Output: |
| 34 | 6 |