Java 中將整數列表轉換為字串列表的程式


這是我們的整數列表 −

List<Integer> listInteger = Arrays.asList(25, 50, 75, 100, 125, 150);

現在,將整數列表轉換為字串列表 −

List<String> listString = listInteger.stream()
   .map(s -> String.valueOf(s))
   .collect(Collectors.toList());

下面是 Java 中將整數列表轉換為字串列表的程式 −

示例

import java.util.*;
import java.util.stream.*;
import java.util.function.*;
public class Demo {
   public static void main(String[] args) {
      List<Integer> listInteger = Arrays.asList(25, 50, 75, 100, 125, 150);
      System.out.println("List of Integer = " + listInteger);
      List<String> listString = listInteger.stream()
         .map(s -> String.valueOf(s))
         .collect(Collectors.toList());
      System.out.println("List of String (converted from List of Integer) = " + listString);
   }
}

輸出

List of Integer = [25, 50, 75, 100, 125, 150]
List of String (converted from List of Integer) = [25, 50, 75, 100, 125, 150]

更新日期: 2019-09-23

607 次檢視

開啟你的 職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.