banner



How To Create A New List Java

10 ways to create Lists in Java 11

Rizwan Ullah

Rizwan Ullah

Senior Java Developer at Sky | Agile Methodologies | Scrum | TDD | Micro Services | Amazon Web Services | Oracle Certified Java Professional | MSc…

Published Jun 14, 2020

We can create and initialize Lists in Java 11 in a number of ways. In fact, we're spoilt for choice when it comes to Lists and (for that matter) Collection creation.

We'll favor immutable lists, that is to say, once created you can not modify them. However, we have many instances where we require mutable lists. And in Java, we have many options to create both mutable and immutable lists.

Here is a 1-minute guide that shows 8 ways to create lists in Java 11. Admittedly, some of these methods were also possible before the arrival of Java 11, so the article can also serve as a reminder.

One way to initialize a list without any library is as in snippet 1.

List<String> mutableList = new ArrayList();    mutableList.add("E1"); mutableList.add("E2");  // 1              

Snippet 2 below shows how to create a mutable list using Guava library.

List<String> mutableListGuava = Lists.newArrayList("E1", "E2");  // 2              

We can Arrays.asList() to create a list that is not completely immutable because it does not have a restriction on set. Other than that we can not remove elements from it or add extra elements to it as shown in snippet 3 below.

List<String> listBackedByArrays = Arrays.asList("E1", "E2");  // 3              

We also have a few options to create immutable lists. We can create an immutable list using the Collections interface. But that takes a pre-constructed list as an argument as shown in snippet 4.

List<Integer> unmodif = Collections.unmodifiableList(new ArrayList());  // 4              

We can also use Collections (a utility class), ImmutableList (a utility class in Guava library) and ListUtils from Commons Collections to create immutable lists (snippet 5).

List<String> singleValueList = Collections.singletonList("E1");   // 5   List<String> immutableListUsingGuava = ImmutableList.of("E1", "E2");  // 6   List<String>  immutableListUsingCollectionUtils = ListUtils.unmodifiableList(Arrays.asList("E1", "E2"));   // 7              

Double braced initialization of the list is shown in snippet 8.

List<String> cities = new ArrayList() {{         add("New York");         add("Rio");         add("Tokyo");      }}  // 8              

Or we can simply use the Stream API and Collector interface to create a list from an array as shown in snippet 9.

List<String> list = Stream.of("foo", "bar").collect(Collectors.toList());  // 9              

And now, with Java11, we have a way of creating an immutable list using the List's fluent API without any external dependency (snippet 10).

List<String> immutableListJava11= List.of("E1", "E2");  // Snippet 10              

In order to compile the above code, you'll require Google Guava and Commons Collections utility libraries available from Maven central and add the following dependencies in your POM as shown below.

<dependencies>      <dependency>         <groupId>com.google.guava</groupId>         <artifactId>guava</artifactId>         <version>29.0-jre</version>     </dependency>      <dependency>         <groupId>org.apache.commons</groupId>         <artifactId>commons-collections4</artifactId>         <version>4.4</version>     </dependency>      <dependency>  </dependencies>              

Do you know any easier way? If so, please feel free comment :-)

  1. #javaprogrammer #java11 #API #Collections

How To Create A New List Java

Source: https://www.linkedin.com/pulse/list-creation-java-11-rizwan-ullah

Posted by: wolfewhisce.blogspot.com

0 Response to "How To Create A New List Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel