Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arrays.asList: when a set is used as the argument we get back a list<set<Integer>> instead of List<Integer>

Set<Integer> iset = new HashSet<>();
iset.add(1);
List<Integer> ilist =  Arrays.asList(iset);

My intention was to convert the set of integers into a list of integers but the compiler complains that a List> cannot be converted to a List. Why does asList work this way and when would one use it? What is the correct way to convert a Set to List?

like image 329
releseabe Avatar asked May 07 '20 05:05

releseabe


People also ask

What does Arrays asList () do?

The asList() method of java. util. Arrays class is used to return a fixed-size list backed by the specified array. This method acts as a bridge between array-based and collection-based APIs, in combination with Collection.

What does array asList return?

asList returns a fixed-size list that is​ backed by the specified array; the returned list is serializable and allows random access.

Is Arrays asList same as ArrayList?

asList method returns a type of ArrayList that is different from java. util. ArrayList. The main difference is that the returned ArrayList only wraps an existing array — it doesn't implement the add and remove methods.

How is Arrays asList () different than the standard way of initialising list?

How is Arrays. asList() different than the standard way of initialising List? Explanation: List returned by Arrays. asList() is a fixed length list which doesn't allow us to add or remove element from it.


Video Answer


2 Answers

Because Arrays.asList treats iset as only one element. That's why this creates List<Set<Integer>>.

To make a list of the elements in a set, pass the set to the constructor of the list.

List<Integer> ilist = new ArrayList<Integer>(iset);

Different uses of Arrays.asList:

List<Integer> ilist1 = Arrays.asList(1); // Single element
List<Integer> ilist2 = Arrays.asList(1,2,3); // Multiple element
Integer a[] = new Integer[] { 10, 20, 30, 40 }; 
List<Integer> ilist3 = Arrays.asList(a); // Array 
like image 155
Eklavya Avatar answered Oct 20 '22 12:10

Eklavya


I hope the following example can help you to understand why this is happening.

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.HashSet;

public class practice {

    public static void main(String... string) {


        /**
         *  This will return a list of Integer, because if we see the implementation of Arrays.asList() 
         *  then this all puzzle will be solved.
         *  Arrays can process a Array collection of data.
         *  asList is generic implementation, returned object data type will be same as received dataType                     
         */
        Integer a[] = new Integer[] {1, 2, 3, 4};
        List<Integer> arrList =  Arrays.asList(a);


        /**
         * But in this case, Arrays.asList() is receiving a collection of Integer object and asList will try to return the 
         * same type of data as it received.
         * So it received Set and will return set 
         * and for more investigation apply debug points in ArrayList class under java.util.Arrays.ArrayList  
         */
        Set<Integer> iset = new HashSet<>();
        iset.add(1);
        List<Set<Integer>> ilist =  Arrays.asList(iset);

    }

}
like image 1
Dupinder Singh Avatar answered Oct 20 '22 13:10

Dupinder Singh