如何在 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.