1 2 3 4 5 6 7 8 | Arrays.stream(new String[] {"Hello", "World"}) .forEach(System.out::println); // 输出"Hello\nWorld"到控制台 int sum = Arrays.stream(new int[] {1, 2, 3}) .reduce((a, b) -> a + b) .getAsInt(); // "sum"的值是"6" |
1 2 3 4 5 6 | List<String> list = new ArrayList<>(); list.add("Hello"); list.add("World"); list.stream() .forEach(System.out::println); // 输出 Hello 和 World |
1 2 3 4 5 6 7 8 9 10 11 | Stream.of(1, 2, 3) .map(v -> v + 1) .flatMap(v -> Stream.of(v * 5, v * 10)) .forEach(System.out::println); //输出 10,20,15,30,20,40 Stream.of(1, 2, 3) .takeWhile(v -> v < 3) .dropWhile(v -> v < 2) .forEach(System.out::println); //输出 2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | Stream.of(1, 2, 3).reduce((v1, v2) -> v1 + v2) .ifPresent(System.out::println); // 输出 6 int result1 = Stream.of(1, 2, 3, 4, 5) .reduce(1, (v1, v2) -> v1 * v2); System.out.println(result1); // 输出 120 int result2 = Stream.of(1, 2, 3, 4, 5) .parallel() .reduce(0, (v1, v2) -> v1 + v2, (v1, v2) -> v1 + v2); System.out.println(result2); // 输出 15 |
1 2 3 | final Map<Character, List<String>> names = Stream.of("Alex", "Bob", "David", "Amy") .collect(Collectors.groupingBy(v -> v.charAt(0))); System.out.println(names); |
1 2 3 | String str = Stream.of("a", "b", "c") .collect(Collectors.joining(", ")); System.out.println(str); |
1 2 3 4 5 6 7 8 | double avgLength = Stream.of("hello", "world", "a") .collect(Collectors.averagingInt(String::length)); System.out.println(avgLength); final IntSummaryStatistics statistics = Stream.of("a", "b", "cd") .collect(Collectors.summarizingInt(String::length)); System.out.println(statistics.getAverage()); System.out.println(statistics.getCount()); |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |