Convert Set to List without creating new List

前端 未结 15 958
鱼传尺愫
鱼传尺愫 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:30

    I create simple static method:

    public static  List convertSetToList(Set set)
    {
        return new ArrayList(set);
    }
    

    ... or if you want to set type of List you can use:

    public static > List convertSetToList(Set set, Class clazz) throws InstantiationException, IllegalAccessException
    {
        L list = clazz.newInstance();
        list.addAll(set);
        return list;
    }
    

提交回复
热议问题