如何在 Java 中过滤字符串流并映射为小写?同时进行排序。


假如以下内容是 String 数组,我们已将其转换为 List −

Arrays.asList("DE", "GH", "JK", "MN", "PQ", "RS", "TU", "VW", "XY", "BC")

现在过滤 String 流并映射为小写 −

.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

更新日期:30-Jul-2019

3 千次以上的浏览量

开启你的 职业

通过完成课程获得认证

开始使用
广告
© . All rights reserved.