Can't I change Set to List without creating List?

Asked 2 years ago, Updated 2 years ago, 136 views

Map<String, List> mainMap = new HashMap<String, List>();

for(int i=0; i<something.size(); i++){
  Set set = getSet(...); //return different result each time
  List listOfNames = new ArrayList(set);
  mainMap.put(differentKeyName,listOfNames);
}

It's a code that changes Set to List, but I don't want to create a list every time I go around the loop, so what should I do?

performance list java set

2022-09-21 18:48

1 Answers

mainList.addAll(set); You can use the List.addAll() method like this.


2022-09-21 18:48

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.