如何在 Java 中过滤字符串流并映射到小写?执行排序。
java 8object oriented programmingprogramming更新于 2025/9/18 23:40:17
假设以下是字符串数组,我们已将其转换为 List −
Arrays.asList("DE", "GH", "JK", "MN", "PQ", "RS", "TU", "VW", "XY", "BC")
现在过滤字符串流并映射到小写 −
.stream()
.filter(a-> a.startsWith("V"))
.map(String::toLowerCase)
现在要排序,请使用 sorted() 并使用显示forEach()。
以下是过滤字符串流并映射到小写的示例 −
示例
import java.util.Arrays;
public class Demo {
public static void main(String[] args) throws Exception {
Arrays.asList("DE", "GH", "JK", "MN", "PQ", "RS", "TU", "VW", "XY", "BC")
.stream()
.filter(a-> a.startsWith("V"))
.map(String::toLowerCase)
.sorted()
.forEach(System.out::println);
}
}
输出
Vw

