Convert Set to List without creating new List

前端 未结 15 931
鱼传尺愫
鱼传尺愫 2020-12-07 06:43

I am using this code to convert a Set to a List:

Map> mainMap = new HashMap<>();

for (int i         


        
15条回答
  •  爱一瞬间的悲伤
    2020-12-07 07:19

    We can use following one liner in Java 8:

    List list = set.stream().collect(Collectors.toList());
    

    Here is one small example:

    public static void main(String[] args) {
            Set set = new TreeSet<>();
            set.add("A");
            set.add("B");
            set.add("C");
            List list = set.stream().collect(Collectors.toList());
    }
    

提交回复
热议问题