Java Stream 是一种强大的数据处理工具,可以帮助开发人员快速高效地处理和转换数据流。使用 Stream 操作可以大大简化代码,使其更具可读性和可维护性,从而提高开发效率。
filter():根据指定的 Predicate 保留符合条件的元素。
map():根据指定的 Function 映射每个元素,生成一个新的 Stream。
flatMap():将每个元素映射为一个 Stream,然后将这些 Stream 连接成一个 Stream。
distinct():返回一个去重后的 Stream。
sorted():对 Stream 进行排序。
peek():对每个元素执行指定的操作,但并不消费元素。
limit():返回一个截断后的 Stream。
skip():返回一个跳过指定元素后的 Stream。
forEach():对每个元素执行指定的操作。
toArray():将 Stream 转换为数组。
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class Example { public static void main(String[] args) { List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); List<Integer> evenNumbers = numbers.stream() .filter(n -> n % 2 == 0) .collect(Collectors.toList()); System.out.println("Even numbers: " + evenNumbers); } }
输出结果:
Even numbers: [2, 4, 6, 8, 10]
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class Example { public static void main(String[] args) { List<String> words = Arrays.asList("hello", "world", "java", "stream"); List<String> capitalizedWords = words.stream() .map(String::toUpperCase) .collect(Collectors.toList()); System.out.println("Capitalized words: " + capitalizedWords); } }
输出结果:
Capitalized words: [HELLO, WORLD, JAVA, STREAM]
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class Example { public static void main(String[] args) { List<List<Integer>> nestedNumbers = Arrays.asList( Arrays.asList(1, 2), Arrays.asList(3, 4, 5), Arrays.asList(6, 7, 8, 9) ); List<Integer> flattenedNumbers = nestedNumbers.stream() .flatMap(List::stream) .collect(Collectors.toList()); System.out.println("Flattened numbers: " + flattenedNumbers); } }
输出结果:
Flattened numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9]
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class Example { public static void main(String[] args) { List<Integer> numbers = Arrays.asList(1, 2, 3, 2, 1, 4, 5, 4, 3, 6); List<Integer> distinctNumbers = numbers.stream() .distinct() .collect(Collectors.toList()); System.out.println("Distinct numbers: " + distinctNumbers); } }
输出结果:
Distinct numbers: [1, 2, 3, 4, 5, 6]
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class Example { public static void main(String[] args) { List<Integer> numbers = Arrays.asList(5, 3, 8, 2, 9, 1, 7, 4, 6, 10); List<Integer> sortedNumbers = numbers.stream() .sorted() .collect(Collectors.toList()); System.out.println("Sorted numbers: " + sortedNumbers); } }
输出结果:
Sorted numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
import java.util.Arrays; import java.util.List; public class Example { public static void main(String[] args) { List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); int count = numbers.stream() .peek(System.out::println) .mapToInt(Integer::intValue) .sum(); System.out.println("Total count: " + count); } }
输出结果:
1
2
3
4
5
6
7
8
9
10
Total count: 55
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class Example { public static void main(String[] args) { List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); List<Integer> limitedNumbers = numbers.stream() .limit(5) .collect(Collectors.toList()); System.out.println("Limited numbers: " + limitedNumbers); } }
输出结果:
Limited numbers: [1, 2, 3, 4, 5]
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class Example { public static void main(String[] args) { List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); List<Integer> skippedNumbers = numbers.stream() .skip(5) .collect(Collectors.toList()); System.out.println("Skipped numbers: " + skippedNumbers); } }
输出结果:
Skipped numbers: [6, 7, 8, 9, 10]
import java.util.Arrays; import java.util.List; public class Example { public static void main(String[] args) { List<String> words = Arrays.asList("hello", "world", "java", "stream"); words.stream() .forEach(System.out::println); } }
输出结果:
hello
world
java
stream
import java.util.Arrays; import java.util.List; public class Example { public static void main(String[] args) { List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); Integer[] numberArray = numbers.stream() .toArray(Integer[]::new); System.out.println("Number array: " + Arrays.toString(numberArray)); } }
输出结果:
Number array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
위 내용은 Java Stream API로 코드를 개선하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!