Difference Between Arrays.Aslist(Array) and New Arraylist<Integer>(Arrays.Aslist(Array))

Difference between Arrays.asList(array) and new ArrayListInteger(Arrays.asList(array))

  1. First, let's see what this does:

    Arrays.asList(ia)

    It takes an array ia and creates a wrapper that implements List<Integer>, which makes the original array available as a list. Nothing is copied and all, only a single wrapper object is created. Operations on the list wrapper are propagated to the original array. This means that if you shuffle the list wrapper, the original array is shuffled as well, if you overwrite an element, it gets overwritten in the original array, etc. Of course, some List operations aren't allowed on the wrapper, like adding or removing elements from the list, you can only read or overwrite the elements.

    Note that the list wrapper doesn't extend ArrayList - it's a different kind of object. ArrayLists have their own, internal array, in which they store their elements, and are able to resize the internal arrays etc. The wrapper doesn't have its own internal array, it only propagates operations to the array given to it.

  2. On the other hand, if you subsequently create a new array as

    new ArrayList<Integer>(Arrays.asList(ia))

    then you create new ArrayList, which is a full, independent copy of the original one. Although here you create the wrapper using Arrays.asList as well, it is used only during the construction of the new ArrayList and is garbage-collected afterwards. The structure of this new ArrayList is completely independent of the original array. It contains the same elements (both the original array and this new ArrayList reference the same integers in memory), but it creates a new, internal array, that holds the references. So when you shuffle it, add, remove elements etc., the original array is unchanged.

What is the difference between List.of and Arrays.asList?

Arrays.asList returns a mutable list while the list returned by List.of is immutable:

List<Integer> list = Arrays.asList(1, 2, null);
list.set(1, 10); // OK

List<Integer> list = List.of(1, 2, 3);
list.set(1, 10); // Fails with UnsupportedOperationException

Arrays.asList allows null elements while List.of doesn't:

List<Integer> list = Arrays.asList(1, 2, null); // OK
List<Integer> list = List.of(1, 2, null); // Fails with NullPointerException

contains behaves differently with nulls:

List<Integer> list = Arrays.asList(1, 2, 3);
list.contains(null); // Returns false

List<Integer> list = List.of(1, 2, 3);
list.contains(null); // Fails with NullPointerException

Arrays.asList returns a view of the passed array, so the changes to the array will be reflected in the list too. For List.of this is not true:

Integer[] array = {1,2,3};
List<Integer> list = Arrays.asList(array);
array[1] = 10;
System.out.println(list); // Prints [1, 10, 3]

Integer[] array = {1,2,3};
List<Integer> list = List.of(array);
array[1] = 10;
System.out.println(list); // Prints [1, 2, 3]

Can I create a list of array with Arrays.asList?

No. Arrays.asList takes a projection of your current array and flattens the first dimension into a List. Since you only have one dimension, all of those elements get collected into the list.

You could do it if you had an Integer[][].

Note: asList accepts a vararg of T (so T...), which is effectively T[]. If you substitute that with Integer[][], you get Integer[], since one dimension of your Integer[][] will satisfy T...'s type requirement.

Create ArrayList from array

new ArrayList<>(Arrays.asList(array));

Arrays.asList() vs Collections.singletonList()

Collections.singletonList(something) is immutable whereas Arrays.asList(something) is a fixed size List representation of an Array where the List and Array gets joined in the heap.

Arrays.asList(something) allows non-structural changes made to it, which gets reflected to both the List and the conjoined array. It throws UnsupportedOperationException for adding, removing elements although you can set an element for a particular index.

Any changes made to the List returned by Collections.singletonList(something) will result in UnsupportedOperationException.

Also, the capacity of the List returned by Collections.singletonList(something) will always be 1 unlike Arrays.asList(something) whose capacity will be the size of the backed array.

incompatible types: inference variable T has incompatible bounds

Arrays.asList is expecting a variable number of Object. int is not an Object, but int[] is, thus Arrays.asList(A) will create a List<int[]> with just one element.

You can use IntStream.of(A).boxed().collect(Collectors.toList());

How to add elements in List when used Arrays.asList()

Create a new ArrayList using the constructor:

List<String> list = new ArrayList<String>(Arrays.asList("a", "b"));

Java - Difference between fixed-sized list and list with initial capacity specified

Arrays.asList(array) returns an object of type java.util.Arrays.ArrayList, which does not support add and remove operations.

While the code below will return an object of type java.util.ArrayList, which supports add and remove operations.

List<Integer> list2 = new ArrayList<Integer>(10);`

Java arraylist cannot find constructor, using arrays.aslist

Yes. Autoboxing does not apply to arrays, only to primitives.

The error I get in eclipse is
The constructor ArrayList<Integer>(List<int[]>) is undefined

Thats because the constructor in ArrayList is defined as public ArrayList(Collection<? extends E> c). As you can see, it only accepts a subtype of Collection, which int is not.

Just change your code to:

public class .... {
public static void main(String[] args) {
Integer[] primes = formPrimes(15);
ArrayList<Integer> primes1 = new ArrayList<Integer>(Arrays.asList(primes));
// Rest of code...
}

public static Integer[] formPrimes(int n) {
// Code that returns an array of integers
}
}

and all should be well, assuming you return an Integer array from fromPrimes.

Update
From Andrew's comments, and after peeking into the source of Arrays.asList:

public static <T> List<T> asList(T... a) {
return new ArrayList<T>(a);
}

So what is really happening here is that Arrays.asList(new int[] {}) would actually return a List<int[]>, unlike Arrays.asList(new Integer[] {}) which would return aList<Integer>. Obviously the ArrayList constructor will not accept a List<int[]>, and hence the compiler complains.



Related Topics



Leave a reply



Submit