Easy-to-use asList

The arrays.aslist () method is often used when developing or writing test cases to quickly and easily convert Arrays to a List. Such as:

List<String> list = Arrays.asList("Book", "Pen", "Desk", "Cup");Copy the code

When we statically reference arrays.aslist () :

import static java.util.Arrays.asList;Copy the code

You can write it like this:

List<String> list = asList("Book", "Pen", "Desk", "Cup");Copy the code

Hidden pit

Primitive types are not genericizable

Execute the following test case:

@Test
public void size() {
  int[] nums = {1, 2, 3, 4, 5, 6};
  List list = asList(nums);
  assertEquals(nums.length, list.size());
}Copy the code

Result: failed:

java.lang.AssertionError: 
Expected :6
Actual   :1Copy the code

Why does an array of six elements have only one element when converted to a List?

Source code doesn’t lie, so let’s look at the code:

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

The asList() method takes a generic parameter. It cannot genericize a basic int, so the function treats the entire array as a whole (array is a reference type and can be genericized). The result is a List, not a List.

If we need a List, we can do it in one of two ways:

@test public void listForInt() {public void listForInt() {public void listForInt(); List<Integer> list = asList(nums); assertEquals(nums.length, list.size()); List = asList(1, 2, 3, 4, 5, 6); assertEquals(6, list.size()); }Copy the code

In both cases, the result is a List.

Do not modify the

I am ready to do the normal operation of List, but I find that the operation cannot be done:

@Test
public void listAdd() {
  List<String> list = asList("Book", "Pen", "Desk", "Cup");
  list.add("Box");
  assertEquals(5, list.size());
}Copy the code

The result is as follows:

java.lang.UnsupportedOperationException
    at java.util.AbstractList.add(AbstractList.java:148)
    at java.util.AbstractList.add(AbstractList.java:108)
    at com.larry.basic.AsListTest.listAdd(AsListTest.java:42)Copy the code

The asList() method returns an ArrayList, but it is not the same as the usual ArrayList:

The most common one we use is java.util.ArrayList, which is a List of mutable arrays. And Java. Util. Arrays. The ArrayList is the Arrays of a static inner class, for the final array at the bottom of the List. They are not the same class.

Java. Util. Arrays. The ArrayList without rewriting the add/remove/clear, therefore calls the parent class AbstractList method, and the parent class method is as follows:

public boolean add(E e) {
  add(size(), e);
  return true;
}
public void add(int index, E element) {
  throw new UnsupportedOperationException();
}
public E remove(int index) {
  throw new UnsupportedOperationException();
}Copy the code

So, these methods are actually not to call, would throw UnsupportedOperationException.

Side effects of modifying operation SET

But is the result of asList() really immutable? Not really. ArrayList overrides the add/remove/clear method, but overrides the set() method:

@Override
public E set(int index, E element) {
  E oldValue = a[index];
  a[index] = element;
  return oldValue;
}Copy the code

We can replace the elements here. And this is actually pretty straightforward, because the bottom is final, the size is immutable, but the elements of the array are mutable. Because of this feature, the following problems may arise:

@Test
public void listSet() {
  String[] arr = {"Book", "Pen", "Desk", "Cup"};
  List<String> list = asList(arr);
  list.set(0, "New Book");
  assertEquals("New Book", list.get(0));
  assertEquals("Book", arr[0]);
}Copy the code

When you change the first element of the List, you change the first element of the array, because they all refer to the same array address. If you don’t pay attention, you can produce and expect different results.

To create a new List, use the following method:

List<String> list = new ArrayList<String>(asList(arr));Copy the code

Because new ArrayList() copies a new array using the array.copyof () method.

conclusion

Be careful with simple, common things.

Welcome to pay attention to the public < pumpkin slow >, will continue to update for you…