• Class in JavaListFive ways to do it
    • 1. The structureListAfter usingList.addInitialize the
    • 2. Use{{}}Double parenthesis syntax
    • Use 3.Arrays.asList
    • Use 4.Stream (JDK8)
    • 5. UseLists(JDK9)
  • reference

Class in JavaListFive ways to do it

Lists are often used in Java. Here are a few common initialization methods.

1. The structureListAfter usingList.addInitialize the

List<String> stringList = new LinkedList<>();
stringList.add("a");
stringList.add("b");
stringList.add("c");
Copy the code

This is the most common way and not very convenient to use.

2. Use{{}}Double parenthesis syntax

List<String> stringList = new LinkedList<String>(){{
    add("a");
    add("b");
    add("c");
}};
Copy the code

This way is relatively convenient.

The outer {} defines an anonymous inner class of LinkedList. The inner {} defines an instance initialization block. This code block is executed when the inner class is initialized. So you define an anonymous inner class and initialize it with the add element.

There are several disadvantages to this approach:

  • There is a loss of efficiency in using anonymous inner classes. Of course, in most cases, this efficiency is acceptable.
  • A static inner class holds a reference to its external class. If you need to return the List for use elsewhere, you can cause a memory leak.

Use 3.Arrays.asList

List<String> stringList = Arrays.asList("a"."b"."c");
Copy the code

This approach uses the static methods of java.util.Arrays. It is written more succinctly than the previous two, and there are no efficiency problems in constructing anonymous inner classes.

But there are a few caveats:

  • Arrays.asListReturns theArraysStatic inner classes (static inner classes do not hold references to their own external classes).

AbstractList this inner class implements RandomAccess using an array to store elements. But there is no support for adding or deleting elements. That’s something to be aware of. This isn’t a problem if you just use arrays.aslist to initialize constants.

  • Arrays.asListIf the argument is an array of primitive types, be aware that the return value may not be what you expected.
int[] intArray = new int[] {1.2.3};
Integer[] integerArray = new Integer[]{1.2.3};
 
List<int[] > intArrayList = Arrays.asList(intArray);
List<Integer> integerList = Arrays.asList(integerArray);
List<Integer> integerList2 = Arrays.asList(1.2.3);
Copy the code

AsList (intArray) returns List

instead of List

. This is not a problem at all, it just needs to be used with care. If you can use List and Integer as much as possible in Java, you can avoid using basic types such as ints and lower-level data structures such as [].

[]>

As an aside: Java can’t be called fully object-oriented after all. After all, there is such a thing as primitive data types. It is true that primitive data types are more efficient to use than their corresponding encapsulation types. But it also creates some confusion: should you use primitive or encapsulated types, and when should you use this and when should you use which? While Java gives the user more choice, there is a sense of throwing the puzzle at the user. In my opinion, one advantage of Java compared with C++ is that many things are more limited and clearly defined, which reduces ambiguity and makes it easier to understand. But the basic data types here feel like a vestige of Java as a language that ameliorates the shortcomings of C++.

Although this article is about initializing a List, the {{}} double parenthesis syntax here can also be used to initialize many other types, such as maps. AsList, on the other hand, can only be used to initialize List types.

Use 4.Stream (JDK8)

List<String> list = Stream.of("a"."b"."c").collect(Collectors.toList());
Copy the code

Stream with JDK8 is used for initialization. Simply initializing a List using a Stream is overkill.

5. UseLists(JDK9)

List<String> list = Lists.newArrayList("a"."b"."c");
Copy the code

This is as clean and clean as arrays.aslist.

reference

Double Brace Initialization

How to initialize List object in Java?