Neither. According to the docs for List.of() it returns:
Returns an immutable list containing an arbitrary number of elements.
Note that it is the interface List that ArrayList and LinkedList implement
If we run this code:
List<Integer> listOf = List.of(1,2,3);
System.out.println(listOf.getClass());
We get:
class java.util.ImmutableCollections$ListN
Answer from GBlodgett on Stack OverflowWhat type of list is generated by List.of() in Java - Stack Overflow
How to make a new List in Java - Stack Overflow
[Java] When to use List, ArrayList etc. ?
collections - How to create list filled with methods in Java and iterate over it (using methods) - Stack Overflow
Can a Java List store primitive data types?
No, Lists can only store objects. Use wrapper classes like Integer or Double.
What is the main difference between a Java List and an array?
A List can grow or shrink in size, while an array has a fixed length once created.
When should I prefer LinkedList over ArrayList?
Choose LinkedList when frequent insertions or deletions are needed, especially in the middle of the list.
Videos
Neither. According to the docs for List.of() it returns:
Returns an immutable list containing an arbitrary number of elements.
Note that it is the interface List that ArrayList and LinkedList implement
If we run this code:
List<Integer> listOf = List.of(1,2,3);
System.out.println(listOf.getClass());
We get:
class java.util.ImmutableCollections$ListN
List<Employee> employeeList = List.of(john, camila, pat);
is more or less equals to
List<Employee> employeeList = Collections.unmodifiableList(Arrays.asList(john, camila, pat));
In case you warry about concrete implementation of the List, do it your own, this is much better.
List myList = new ArrayList();
or with generics (Java 7 or later)
List<MyType> myList = new ArrayList<>();
or with generics (Old java versions)
List<MyType> myList = new ArrayList<MyType>();
Additionally, if you want to create a list that has things in it (though it will be fixed size):
List<String> messages = Arrays.asList("Hello", "World!", "How", "Are", "You");
Hey,
so I'm a little bit confused here. There are a lot of lists and I don't really get what's the difference or when I should instantiate a List as an ArrayList or when to instantiate an ArrayList as a List etc. I hope you get what I mean. Here is a code example I wrote:
public class ListTest {
public static void main(String args[]){
List<String> list1 = new ArrayList<String>();
ArrayList<String> list2 = new ArrayList<String>();
list1.add("Hello");
list2.add("Hello");
System.out.println(list1.get(0));
System.out.println(list2.get(0));
}
}What I'm asking here what is the difference between List and ArrayList and why can I instantiate a List as an ArrayList? To me it looks like they do exactly the same.
I hope someone can ELI5 that to me.
In Java 8 and above you can create a List of Functional Interfaces to store the methods, as the following example shows:
// Create a list of Consumer
List<Consumer<String>> methodList= new ArrayList<>();
// Store the methods in the list
methodList.add(p -> method1(p));
methodList.add(p -> method2(p));
methodList.add(p -> method3(p));
methodList.forEach(f -> f.accept("Hello"));
Functions with no arguments you can use Runnable:
List<Runnable> methods = new ArrayList<>();
methods.add(() -> method1());
methods.add(() -> method2());
methods.add(() -> method3());
methods.forEach(f -> f.run());
In Java, you can do this with reflection by making a list of Method objects. However, an easier way is to define an interface for objects that have a method that takes an Object argument:
public interface MethodRunner {
public void run(Object arg);
}
List<MethodRunner> a = new ArrayList<>();
a.add(new MethodRunner() {
@Override
public void run(Object arg) {
myCustomMethod1(arg);
}
});
a.add(new MethodRunner() {
@Override
public void run(Object arg) {
myCustomMethod2(arg);
}
});
Object o = new Object();
for (MethodRunner mr : a) {
mr.run(o);
}