Convert Set to List without creating new List

前端 未结 15 898
鱼传尺愫
鱼传尺愫 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 found this working fine and useful to create a List from a Set.

    ArrayList < String > L1 = new ArrayList < String > ();
    L1.addAll(ActualMap.keySet());
    for (String x: L1) {
        System.out.println(x.toString());
    }
    
    0 讨论(0)
  • 2020-12-07 07:32

    You could use this one line change: Arrays.asList(set.toArray(new Object[set.size()]))

    Map<String, List> mainMap = new HashMap<String, List>();
    
    for(int i=0; i<something.size(); i++){
      Set set = getSet(...); 
      mainMap.put(differentKeyName, Arrays.asList(set.toArray(new Object[set.size()])));
    }  
    
    0 讨论(0)
  • 2020-12-07 07:32
    Map<String, List> mainMap = new HashMap<String, List>();
    
    for(int i=0; i<something.size(); i++){
      Set set = getSet(...); //return different result each time
      mainMap.put(differentKeyName, new ArrayList(set));
    }
    
    0 讨论(0)
提交回复
热议问题