In Java, how do you declare a list with elements in it?

One approach that we can take is create an empty list and then iterate over that list and add elements. But there is a simpler approach that we can take using the Arrays class as below.

List<String> list = Arrays.asList("foo", "bar", "moo");
Advertisements