GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — JavaInterview/java/src/Java8Test/StreamTest.java at master · damaohongtu/JavaInterview Expand file tree/ Copy pathStreamTest.javaMore file actions 57 lines (43 loc) · 2.25 KB / Copy pathStreamTest.javaFile metadata and controls57 lines (43 loc) · 2.25 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 package Java8Test; import java.util.Arrays; import java.util.IntSummaryStatistics; import java.util.List; import java.util.Random; import java.util.stream.Collectors; /** * @Classname StreamTest * @Description TODO * @Date 19-7-5 上午11:00 * @Created by mao<[email protected]> */ public class StreamTest { public static void main(String[] args){ List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl"); List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5); List<Integer> integers = Arrays.asList(1,2,13,4,15,6,17,8,19); Random random = new Random(); System.out.println("使用 Java 8: "); System.out.println("列表: " +strings); long count = strings.stream().filter(string->string.isEmpty()).count(); System.out.println("空字符串数量为: " + count); count = strings.stream().filter(string -> string.length() == 3).count(); System.out.println("字符串长度为 3 的数量为: " + count); List<String> filtered = strings.stream().filter(string ->!string.isEmpty()).collect(Collectors.toList()); System.out.println("筛选后的列表: " + filtered); String mergedString = strings.stream().filter(string ->!string.isEmpty()).collect(Collectors.joining(", ")); System.out.println("合并字符串: " + mergedString); List<Integer> squaresList = numbers.stream().map( i ->i*i).distinct().sorted().collect(Collectors.toList()); System.out.println("平方后并排序的列表: " + squaresList); System.out.println("列表: " +integers); IntSummaryStatistics stats = integers.stream().mapToInt((x) ->x).summaryStatistics(); System.out.println("列表中最大的数 : " + stats.getMax()); System.out.println("列表中最小的数 : " + stats.getMin()); System.out.println("所有数之和 : " + stats.getSum()); System.out.println("平均数 : " + stats.getAverage()); System.out.println("随机数: "); random.ints().limit(10).sorted().forEach(System.out::println); // 并行处理 count = strings.parallelStream().filter(string -> string.isEmpty()).count(); System.out.println("空字符串的数量为: " + count); } } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |