Recently took over a colleague’s code, and returns a list, I tip UnsupportedOperationException this error when adding data, the operation does not support, why don’t you support? AsList (1,2); See this error to understand, the following details.

There are several ways to create a list:

1. Through new objects

List<Integer> list = new ArrayList<>();
// The most common way to interpret this is as a standard List
// You can use the following code
Copy the code

2. Use anonymous inner classes

List<Integer> list = new ArrayList<>() {{
    add(1);
    add(2);
}};
// Use anonymous inner classes
Copy the code

3. Create through Arrays

List<Integer> list = Arrays.asList(1.2);
// Arguments are of variable length
// Use: when no operation is performed, such as interface return
Copy the code

AsList () returns an ArrayList. It doesn’t look very different, but the returned ArrayList is a private inner class of Arrays. But the ArrayList doesn’t override the add() method internally. (It doesn’t override the parent method, which is called when add() is executed. AbstractList inside of the add () directly throw an UnsupportedOperationException an unsupported operation), only a few simple function.

When you write a class as an inner class, you want it to be called internally, not used publicly.

4. Create a single data List using Collections

List<Integer> list = Collections.singletonList(1);
// This is also a common method, with only one parameter and only one internal data
Copy the code

AbstractList SingletonList is a private inner class that also inherits AbstractList and does not support add(). One more note, since this is a list of single data, its size() returns 1.

Create List with Stream

List<Integer> list = Stream.of(1.2.3).collect(toList());
Collect (toList())
Copy the code

Stream is available in JDK1.8. Versions below 1.8 do not support this method. See toList() here. As you can see, the source code is also added using the New ArrayList, followed by the add() method.

Use static methods of the List interface

List<Integer> list = List.of(1.2.3);
// Static methods of the interface, also called default functions
Copy the code

Returns the new ImmutableCollections. List12 < > (e1)

ImmutableCollections are ImmutableCollections that cannot be added as well.

Static <E> List<E> of(E... Elements), why write a function for each different number of arguments, even though one is List12 and one is ListN, but the implementation inside is similar

Create an empty list using the Collections collection

List<Integer> list = Collections.emptyList();
// Return an empty List, immutable and unaddable
Copy the code

This is probably in case of error… HHH is really designed to prevent errors. You might want to use new ArrayList<>(). Not the same!

newArrayList<>() is constructed with an initial capacity of10Empty list of. Collections.emptylist () returns an emptyList(empty array), reducing memory overheadCopy the code

8. Use the Collections collection to create lists with replicas

/** * parameter n: how many o * parameter o: element to add */List<Integer> list = Collections.nCopies(n, o); Example:// There are 3 elements in the list, all of which are 1
/ / [1, 1, 1)
List<Integer> list = Collections.nCopies(3.1);

// List contains 4 elements, all 4 elements are "AAA"
// ["AAA", "AAA", "AAA", "AAA"]
List<String> list = Collections.nCopies(4."AAA");
Copy the code

AbstractList (addAll) ¶ AbstractList (addAll) is a private class that extends AbstractList without overwriting the add() method.

conclusion

Today not summary, above all said clearly, again see not understand come out be beaten! Off duty!!