Convert Set to List without creating new List

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

    Since it hasn't been mentioned so far, as of Java 10 you can use the new copyOf factory method:

    List.copyOf(set);
    

    From the Javadoc:

    Returns an unmodifiable List containing the elements of the given Collection, in its iteration order.

    Note that this creates a new list (ImmutableCollections$ListN to be precise) under the hood by

    1. calling Collection#toArray() on the given set and then
    2. putting these objects into a new array.

    So the newly allocated memory should be negligible.

    0 讨论(0)
  • 2020-12-07 07:09

    I would do :

    Map<String, Collection> mainMap = new HashMap<String, Collection>();
    
    for(int i=0; i<something.size(); i++){
      Set set = getSet(...); //return different result each time
      mainMap.put(differentKeyName,set);
    }
    
    0 讨论(0)
  • 2020-12-07 07:11

    Java 8 provides the option of using streams and you can get a list from Set<String> setString as:

    List<String> stringList = setString.stream().collect(Collectors.toList());
    

    Though the internal implementation as of now provides an instance of ArrayList:

    public static <T>
        Collector<T, ?, List<T>> toList() {
            return new CollectorImpl<>((Supplier<List<T>>) ArrayList::new, List::add,
                                       (left, right) -> { left.addAll(right); return left; },
                                       CH_ID);
        }
    

    but JDK does not guarantee it. As mentioned here:

    There are no guarantees on the type, mutability, serializability, or thread-safety of the List returned; if more control over the returned List is required, use toCollection(Supplier).

    In case you want to be sure always then you can request for an instance specifically as:

    List<String> stringArrayList = setString.stream()
                         .collect(Collectors.toCollection(ArrayList::new));
    
    0 讨论(0)
  • 2020-12-07 07:12

    You can use the List.addAll() method. It accepts a Collection as an argument, and your set is a Collection.

    List<String> mainList = new ArrayList<String>();
    mainList.addAll(set);
    

    EDIT: as respond to the edit of the question.
    It is easy to see that if you want to have a Map with Lists as values, in order to have k different values, you need to create k different lists.
    Thus: You cannot avoid creating these lists at all, the lists will have to be created.

    Possible work around:
    Declare your Map as a Map<String,Set> or Map<String,Collection> instead, and just insert your set.

    0 讨论(0)
  • 2020-12-07 07:19

    We can use following one liner in Java 8:

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

    Here is one small example:

    public static void main(String[] args) {
            Set<String> set = new TreeSet<>();
            set.add("A");
            set.add("B");
            set.add("C");
            List<String> list = set.stream().collect(Collectors.toList());
    }
    
    0 讨论(0)
  • 2020-12-07 07:20

    Use constructor to convert it:

    List<?> list = new ArrayList<?>(set);
    
    0 讨论(0)
提交回复
热议问题