Python 官方文档:入门教程 => 点击学习
目录前言示例示例 1:使用 filter() 方法过滤奇数示例 2:使用 map() 方法将每个字符串转换为大写示例 3:使用 flatMap() 方法将嵌套的列表展平为一个列表示例
Java Stream 是一种强大的数据处理工具,可以帮助开发人员快速高效地处理和转换数据流。使用 Stream 操作可以大大简化代码,使其更具可读性和可维护性,从而提高开发效率。本文将为您介绍 Java Stream 操作的所有方面,包括 filter、map、distinct、sorted 等操作,让您的代码行云流水,更加优雅。
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 使代码更出色的操作完全攻略的详细内容,更多关于Java Stream API 操作攻略的资料请关注编程网其它相关文章!
--结束END--
本文标题: Java Stream API 使代码更出色的操作完全攻略
本文链接: https://lsjlt.com/news/208887.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0