Effective Java Item 25 Select a list from an array First half

Choose a list from an array

The difference between arrays and generic types is

--The sequence is covariant and the generic type is invariant --Arrays are concrete (know their element types at run time and enforced), but generics are erasers (enforce type constraints only at compile time and discard type information at runtime).

E, ``` List , and List `` are technically known as nonreifiable types. Intuitively, a non-representable type is a type whose run-time representation is less informative than its compile-time representation. The only parameterized types that can be embodied are non-boundary wildcard types such as ``List ` `` And `` `Map ` ``. It is permissible to generate arrays of non-boundary wildcard type, but it is rarely useful. Prohibiting the generation of generic sequences can be annoying. For example, generic types are generally unable to return an array of their element types. It also means that you may get confusing warnings when using variadic methods in combination with generic types. That's because every time you call a variadic method, an array is generated to hold the variadic parameters. (In the code below, args corresponds to that)

static int sum(int... args) {
    int sum = 0;
    for (int arg : args) {
        sum += arg;
    }
    return sum;
}

If the element types of this array are not representable, you will be warned. There is little you can do about a warning other than suppressing it and not mixing generics and variadic arguments in your API.

If you get a generic sequence generation error, the best solution is usually to use the collection type List <E> rather than the array type ```E [] `` . You may sacrifice some performance or brevity, but instead get better type safety and interoperability.

Recommended Posts

Effective Java Item 25 Select a list from an array First half
[Java] Get a random value from an array
Take a look at Kotlin from an Effective Java perspective
[Java] Conversion from array to List
Sorting a list with an int array as an element (Java) (Comparator)
[Java] From two Lists to one array list
Create Scala Seq from Java, make Scala Seq a Java List
About Java Array List
Generate Stream from an array of primitive types in Java
Get a non-empty collection from an Optional stream in java
[Java] List type / Array type conversion
[Java] How to search for a value in an array (or list) with the contains method
[Note] What I learned in half a year from inexperienced (Java)
[Read Effective Java] Chapter 2 Item 4 "Force uninstantiation with a private constructor"
[Note] What I learned in half a year from inexperienced (Java) (3)
Java learning memo (creating an array)
Sort a List of Java objects
Run a batch file from Java
[Java] Declare and initialize an array
Access Teradata from a Java application
How to make a Java array
[Java] How to turn a two-dimensional array with an extended for statement
I want to ForEach an array with a Lambda expression in Java
[Java] Generate a narrowed list from multiple lists using the Stream API