Immutable (immutable) List object conversion function in Java8

I used it for some reason, so instead of a memorandum.


@SafeVarargs
public static <E> List<E> toConstList(E... data) {
    int length = (data == null) ? 0 : data.length;

    switch (length) {
    case 0:
        return Collections.emptyList();
    case 1:
        return Collections.singletonList(data[0]);
    default:
        return Collections.unmodifiableList(Arrays.asList(data));
    }
}

Recommended Posts

Immutable (immutable) List object conversion function in Java8
[java] sort in list
[Java] List type / Array type conversion
List aggregation in Java (Collectors.groupingBy)
[Java, stream] Sort object list in Japanese by property name
Implement tagging function in form object
Implement PHP implode function in Java
List of members added in Java 9
List of types added in Java 9
[Java] Conversion from array to List
Java array / list / stream mutual conversion list
Java8 list conversion with Stream map
Implementation of like function in Java
Difference between final and Immutable in Java
[Easy-to-understand explanation! ] Reference type type conversion in Java
How to do base conversion in Java
Change List <Optional <T >> to Optional <List <T >> in Java
Arrylist and linked list difference in java
Process every arbitrary number in Java List
Do not declare variables in List in Java
Java type conversion
Partization in Java
Java memorandum (list)
Clone Java List.
Story of implementing update function in form object
Rock-paper-scissors in Java
Variable / immutable object
Pi in Java
[Java] Object class
[Java] Difference between "final variable" and "immutable object"
FizzBuzz in Java
About List [Java]
Sample code to convert List to List <String> in Java Stream
[Java] Get the file path in the folder with List
About full-width ⇔ half-width conversion of character strings in Java
Read JSON in Java
Interpreter implementation in Java
Make Blackjack in Java
Rock-paper-scissors app in Java
Constraint programming in Java
Put java8 in centos7
[Java] Full-width ⇔ half-width conversion
NVL-ish guy in Java
Combine arrays in Java
"Hello World" in Java
Callable Interface in Java
JAVA object mapping library
Azure functions in java
Java object size feeling
Simple htmlspecialchars in Java
Boyer-Moore implementation in Java
Hello World in Java
About Java Array List
webApi memorandum in java
Value object in 3 minutes
Type determination in Java
Ping commands in Java
Various threads in java
Heapsort implementation (in java)
ASCII art in Java
Compare Lists in Java