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
| public class Stream { public static void main(String[] args) { List<Integer> integerList = Arrays.asList(1, 2, 2, 5, 7, -3, 3); List<Integer> integers = integerList.stream().map(i -> i * i).distinct().collect(Collectors.toList()); for (Integer integer : integers) { System.out.println(integer); } IntSummaryStatistics stats = integerList.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()); long count = integerList.stream().filter(i -> i != 2).count(); System.out.println(count); Random random = new Random(); random.ints().limit(10).forEach(System.out::println); random.ints().limit(10).sorted().forEach(System.out::println); List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl"); String newString = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.joining(",")); System.out.println(newString); } }
|