Item 28: Prefer lists to arrays

28. Use lists rather than arrays

package tryAny.effectiveJava;

public class GenericsTest5 {
    public static void main(String[] args) {

        Object[] objAry = new Long[1];
        objAry[0] = "aa";

        // Won't compile!
        // List<Object> ol = new ArrayList<Long>(); // Incompatible

    }
}
// Why generic array creation is illegal - won't compile!
List<String>[] stringLists = new List<String>[1];  // (1)
List<Integer> intList = List.of(42);               // (2)
Object[] objects = stringLists;                    // (3)
objects[0] = intList;                              // (4)
String s = stringLists[0].get(0);                  // (5)

Types such as * E and List are called ** non-reifiable **. The intuitive meaning of this word is that it has less information at runtime than at compile time. Only generic types that use the? Wildcard are reifiable, but arrays of generics that use wildcards have no practical value.

package tryAny.effectiveJava;

import java.util.Collection;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

public class Chooser1 {
    private final Object[] choiceArray;

    public Chooser1(Collection choices) {
        choiceArray = choices.toArray();
    }

    public Object choose() {
        Random rnd = ThreadLocalRandom.current();
        return choiceArray[rnd.nextInt(choiceArray.length)];
    }

}

According to Item29, I wrote the following in Generics. It's a bit verbose and performance degradation, but it doesn't throw a ClassCastException at runtime.

package tryAny.effectiveJava;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

//List-based Chooser - typesafe
public class Chooser2<T> {
    private final List<T> choiceList;

    public Chooser2(Collection<T> choices) {
        choiceList = new ArrayList<>(choices);
    }

    public T choose() {
        Random rnd = ThreadLocalRandom.current();
        return choiceList.get(rnd.nextInt(choiceList.size()));
    }
}

Recommended Posts

Item 28: Prefer lists to arrays
Item 65: Prefer interfaces to reflection
Item 43: Prefer method references to lambdas
Item 42: Prefer lambdas to anonymous classes
Item 39: Prefer annotations to naming patterns
Item 85: Prefer alternatives to Java serialization
Item 58: Prefer for-each loops to traditional for loops
Item 23: Prefer class hierarchies to tagged classes
Item 61: Prefer primitive types to boxed primitives
Corresponds to 17 arrays
Item 81: Prefer concurrency utilities to wait and notify
Item 80: Prefer executors, tasks, and streams to threads
Item 89: For instance control, prefer enum types to readResolve
Item 47: Prefer Collection to Stream as a return type
Study java arrays, lists, maps
Swift: I want to chain arrays
How to use arrays (personal memorandum)
Item 46: Prefer side-effect-free functions in streams
Java, arrays to start with beginners
[Processing × Java] How to use arrays