作者:ian | 来源:互联网 | 2024-10-30 11:50
Java8引入了StreamAPI,这一新特性极大地增强了集合数据的处理能力。通过StreamAPI,开发者可以更加高效、简洁地进行集合数据的遍历、过滤和转换操作。本文将详细解析StreamAPI的核心概念和常见用法,帮助读者更好地理解和应用这一强大的工具。
java8新特性(4)— Stream流
遍历集合更强大
package com.common.jdk8;import java.util.*;
import java.util.stream.Collectors;
public class Jdk8Test4 {public static void main(String args[]){System.out.println("使用 Java 7: ");List<String> strings &#61; Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");System.out.println("列表: " &#43;strings);long count &#61; getCountEmptyStringUsingJava7(strings);System.out.println("空字符数量为: " &#43; count);count &#61; getCountLength3UsingJava7(strings);System.out.println("字符串长度为 3 的数量为: " &#43; count);List<String> filtered &#61; deleteEmptyStringsUsingJava7(strings);System.out.println("筛选后的列表: " &#43; filtered);String mergedString &#61; getMergedStringUsingJava7(strings,", ");System.out.println("合并字符串: " &#43; mergedString);List<Integer> numbers &#61; Arrays.asList(3, 2, 2, 3, 7, 3, 5);List<Integer> squaresList &#61; getSquares(numbers);System.out.println("平方数列表: " &#43; squaresList);List<Integer> integers &#61; Arrays.asList(1,2,13,4,15,6,17,8,19);System.out.println("列表: " &#43;integers);System.out.println("列表中最大的数 : " &#43; getMax(integers));System.out.println("列表中最小的数 : " &#43; getMin(integers));System.out.println("所有数之和 : " &#43; getSum(integers));System.out.println("平均数 : " &#43; getAverage(integers));System.out.println("随机数: ");Random random &#61; new Random();for(int i&#61;0; i < 10; i&#43;&#43;){System.out.println(random.nextInt());}System.out.println("使用 Java 8: ");System.out.println("列表: " &#43;strings);count &#61; strings.stream().filter(string->string.isEmpty()).count();System.out.println("空字符串数量为: " &#43; count);count &#61; strings.stream().filter(string -> string.length() &#61;&#61; 3).count();System.out.println("字符串长度为 3 的数量为: " &#43; count);filtered &#61; strings.stream().filter(string ->!string.isEmpty()).collect(Collectors.toList());System.out.println("筛选后的列表: " &#43; filtered);mergedString &#61; strings.stream().filter(string ->!string.isEmpty()).collect(Collectors.joining(", "));System.out.println("合并字符串: " &#43; mergedString);squaresList &#61; numbers.stream().map( i ->i*i).distinct().collect(Collectors.toList());System.out.println("Squares List: " &#43; squaresList);System.out.println("列表: " &#43;integers);IntSummaryStatistics stats &#61; integers.stream().mapToInt((x) ->x).summaryStatistics();System.out.println("列表中最大的数 : " &#43; stats.getMax());System.out.println("列表中最小的数 : " &#43; stats.getMin());System.out.println("所有数之和 : " &#43; stats.getSum());System.out.println("平均数 : " &#43; stats.getAverage());System.out.println("随机数: ");random.ints().limit(10).sorted().forEach(System.out::println);count &#61; strings.parallelStream().filter(string -> string.isEmpty()).count();System.out.println("空字符串的数量为: " &#43; count);}private static int getCountEmptyStringUsingJava7(List<String> strings){int count &#61; 0;for(String string: strings){if(string.isEmpty()){count&#43;&#43;;}}return count;}private static int getCountLength3UsingJava7(List<String> strings){int count &#61; 0;for(String string: strings){if(string.length() &#61;&#61; 3){count&#43;&#43;;}}return count;}private static List<String> deleteEmptyStringsUsingJava7(List<String> strings){List<String> filteredList &#61; new ArrayList<String>();for(String string: strings){if(!string.isEmpty()){filteredList.add(string);}}return filteredList;}private static String getMergedStringUsingJava7(List<String> strings, String separator){StringBuilder stringBuilder &#61; new StringBuilder();for(String string: strings){if(!string.isEmpty()){stringBuilder.append(string);stringBuilder.append(separator);}}String mergedString &#61; stringBuilder.toString();return mergedString.substring(0, mergedString.length()-2);}private static List<Integer> getSquares(List<Integer> numbers){List<Integer> squaresList &#61; new ArrayList<Integer>();for(Integer number: numbers){Integer square &#61; new Integer(number.intValue() * number.intValue());if(!squaresList.contains(square)){squaresList.add(square);}}return squaresList;}private static int getMax(List<Integer> numbers){int max &#61; numbers.get(0);for(int i&#61;1;i < numbers.size();i&#43;&#43;){Integer number &#61; numbers.get(i);if(number.intValue() > max){max &#61; number.intValue();}}return max;}private static int getMin(List<Integer> numbers){int min &#61; numbers.get(0);for(int i&#61;1;i < numbers.size();i&#43;&#43;){Integer number &#61; numbers.get(i);if(number.intValue() < min){min &#61; number.intValue();}}return min;}private static int getSum(List numbers){int sum &#61; (int)(numbers.get(0));for(int i&#61;1;i < numbers.size();i&#43;&#43;){sum &#43;&#61; (int)numbers.get(i);}return sum;}private static int getAverage(List<Integer> numbers){return getSum(numbers) / numbers.size();}
}
运行结果
使用 Java 7:列表: [abc, , bc, efg, abcd, , jkl]空字符数量为: 2字符串长度为 3 的数量为: 3筛选后的列表: [abc, bc, efg, abcd, jkl]合并字符串: abc, bc, efg, abcd, jkl平方数列表: [9, 4, 49, 25]列表: [1, 2, 13, 4, 15, 6, 17, 8, 19]列表中最大的数 : 19列表中最小的数 : 1所有数之和 : 85平均数 : 9随机数:-8845067758448462981181770977-268099100-20767820571256561986802112096-859138137-194403122-1546201209使用 Java 8:列表: [abc, , bc, efg, abcd, , jkl]空字符串数量为: 2字符串长度为 3 的数量为: 3筛选后的列表: [abc, bc, efg, abcd, jkl]合并字符串: abc, bc, efg, abcd, jklSquares List: [9, 4, 49, 25]列表: [1, 2, 13, 4, 15, 6, 17, 8, 19]列表中最大的数 : 19列表中最小的数 : 1所有数之和 : 85平均数 : 9.444444444444445随机数:-1118525426-847572210-632355909-447013487-1523942166699336748595529656337063410283633211283705449空字符串的数量为: 2