共計 380 個字符,預計需要花費 1 分鐘才能閱讀完成。
可以通過以下方式將 Set 轉換為 List:
- 使用 ArrayList 構造函數:
Set<String> set = new HashSet<>();
// 添加元素到 set 中
List<String> list = new ArrayList<>(set);
- 使用 addAll 方法:
Set<String> set = new HashSet<>();
// 添加元素到 set 中
List<String> list = new ArrayList<>();
list.addAll(set);
- 使用 Stream API:
Set<String> set = new HashSet<>();
// 添加元素到 set 中
List<String> list = set.stream().collect(Collectors.toList());
這些方法都可以將 Set 轉換為 List,選擇其中一種適合你的需求即可。
丸趣 TV 網 – 提供最優質的資源集合!
正文完