Java 中 List 与 Set 的转换

Java 中 List 与 Set 的转换

(1) List转换为set

 List<Long> ids=new ArrayList<>();
 HashSet<Long> set=new HashSet<>(ids);

需要注意的是: List转换为Set的时候,当有重复数据时,转换为出现数据丢失的情况,因为Set集合不允许有重复数据。

(2) Set转换为List

 HashSet<Long> set=new HashSet<>();
 List<Long> ids=new ArrayList<>(set);

原文链接:https://blog.csdn.net/qq_33036061/article/details/103968822

猜你喜欢

转载自blog.csdn.net/Shipley_Leo/article/details/131006542