Arrays.aslist mutable

In Java, you can initialize an array list in one line by using array literals and stream literals. Use the Arrays.asList[], List.of[], and Stream.of[] APIs to create an array list in a single line. Use the Array class apis to convert an array to a list. The list will be initialised by converting the array to an immutable list. UnsupportedOperationException will be thrown if you try to alter this immutable list. You can create an ArrayList from an immutable list to make a changeable list that is a mutable ArrayList.

With this post, we’ll look at how to create and initialise a java array list in just one line. A single line of code can produce both immutable and mutable array lists. We’ll learn how to create and initialise immutable and mutable lists in a single line in this article.

To convert an array to an immutable java list, use the java Arrays class. To convert a list of values to an immutable java list, use the Arrays.asList[] method. The java list is immutable and cannot be changed. UnsupportedOperationException will be thrown if you try to change the list. Arrays.asList[] allows you to construct a list with just one line of code.

package com.yawintutor; import java.util.Arrays; import java.util.List; public class ListOneLine { public static void main[String[] args] { List list = Arrays.asList["One", "Two", "Three"]; System.out.println[list]; } }

Output

[One, Two, Three]

From Java 9 onwards, the List.of[] API is available. The List.of[] api will create an immutable list from a list of items. The list can’t be changed. If you try to change it, an UnsupportedOperationException will be thrown. The List.Of[] API returns a collection of objects.

package com.yawintutor; import java.util.Arrays; import java.util.List; public class ListOneLine { public static void main[String[] args] { List list = List.of["One", "Two", "Three"]; System.out.println[list]; } }

Output

[One, Two, Three]

In Java 10, the var is introduced to refer any objects. The var can be used to assign a list of objects using List.of[] api. The var will work as like List list in the example below.

package com.yawintutor; import java.util.Arrays; import java.util.List; public class ListOneLine { public static void main[String[] args] { var list = List.of["One", "Two", "Three"]; System.out.println[list]; } }

Output

[One, Two, Three]

Java 8 onwards, the static import is allowed to import a static method. In the example below, the Arrays.asList static method is imported in the program. The asList method is called as like local method in this program.

package com.yawintutor; import static java.util.Arrays.asList; import java.util.List; public class ListOneLine { public static void main[String[] args] { List list = asList["One", "Two", "Three"]; System.out.println[list]; } }

Output

[One, Two, Three]

The Stream.of[] api is used to create a list in java. The Stream.of[] will create a stream of objects. The stream of objects can be collected as list of object using collect[] api in the stream.

package com.yawintutor; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class ListOneLine { public static void main[String[] args] { Stream stream = Stream.of["One", "Two", "Three"]; List list = stream.collect[Collectors.toList[]]; System.out.println[list]; } } package com.yawintutor; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class ListOneLine { public static void main[String[] args] { List list = Stream.of["One", "Two", "Three"].collect[Collectors.toList[]]; System.out.println[list]; } }

Output

[One, Two, Three]

To create a Java ArrayList, use the Arrays.asList[] API. The Java ArrayList is a changeable, mutable list. The list can be changed, for example, by adding or removing items. To make a mutable ArrayList object, the immutable list will be appended to the ArrayList constructor method.

package com.yawintutor; import java.util.ArrayList; import java.util.Arrays; public class ListOneLine { public static void main[String[] args] { ArrayList list = new ArrayList[Arrays.asList["One", "Two", "Three"]]; System.out.println[list]; } }

Output

[One, Two, Three]

The array list can be created using the List.of[] API by passing a list of immutable objects to the array list constructor method. The array list object is mutable, meaning it can be modified after it has been created.

package com.yawintutor; import java.util.ArrayList; import java.util.List; public class ListOneLine { public static void main[String[] args] { ArrayList list = new ArrayList[List.of["One", "Two", "Three"]]; System.out.println[list]; } }

Output

[One, Two, Three]

The array list can be created using Stream.of[] api in a single line. The stream api collect is used to convert to an array list object. The converted object is a mutable object.

package com.yawintutor; import java.util.ArrayList; import java.util.stream.Collectors; import java.util.stream.Stream; public class ListOneLine { public static void main[String[] args] { ArrayList list = Stream.of["One", "Two", "Three"] .collect[Collectors.toCollection[ArrayList::new]]; System.out.println[list]; } }

Output

[One, Two, Three]

The conventional method creates an array list that spans multiple lines. In a single line, the array list is declared and initialised. The add method will be used to add each item. A higher number of items in a larger number of lines will be introduced.

package com.yawintutor; import java.util.ArrayList; public class ListOneLine { public static void main[String[] args] { ArrayList list = new ArrayList[]; list.add["One"]; list.add["Two"]; list.add["Three"]; System.out.println[list]; } }

Output

[One, Two, Three]

The anonymous array list class will be used to create an array list and to add items to the array list as it is being created. The add method will be created using anonymous array list.

package com.yawintutor; import java.util.ArrayList; public class ListOneLine { public static void main[String[] args] { ArrayList list = new ArrayList[] {{ add["One"]; add["Two"]; add["Three"]; }}; System.out.println[list]; } }

Output

[One, Two, Three]

A quick guide to Inline lists Java. Inline Lists are created and initialised at the time of creation, in one line.

The most basic way of creating and initialising an ArrayList is to do it in multi-line manner.

List list = new ArrayList[]; list.add[1]; list.add[2]; list.add[3];

Code language: Java [java]

Alternatively, we can create a list inline using another collection.

Collection hashSet = Set.of[1, 2, 3]; List list = new ArrayList[hashSet];

Code language: Java [java]

First, we are initialising a HashSet instance Inline. Next we are using ArrayList constructor to create a list instance.

The Java Streams Collectors supports collecting steam elements into a list.

In the next example, we are creating a stream of three numbers and collecting it to a list using toList method.

List list = Stream.of[1, 2, 3] .collect[Collectors.toList[]];

Code language: Java [java]

Alternatively, we can use toCollection collector by providing a supplier of ArrayList.

In the next example, we are creating a stream of three numbers and collecting them with toCollection collector by providing an ArrayList constructor.

List list = Stream.of[1, 2, 3] .collect[Collectors.toCollection[ArrayList::new]];

Code language: Java [java]

List list = new ArrayList[] { { add[1]; add[2]; add[3]; } };

Code language: Java [java]

Although this looks simple, we should avoid using anonymous subclass to initialise a list inline. It creates an anonymous subclass as well as an extra initialisation block.

We can create an Immutable List from another mutable or immutable list using unmodifiableList method of Collections class.

List list = Collections .unmodifiableList[anotherList];

Code language: Java [java]

Note that the output we get is an immutable list. Thus, we cannot modify it after creation.

A singleton list is a list with one and only one element.

List list = Collections.singletonList[1];

Code language: Java [java]

The singletonList method takes the element as an argument and builds an implicitly immutable singleton list.

Java 9 introduced factory methods in the List interface which can be used to create lists inline.

List list = List.of[1, 2, 3];

Code language: Java [java]

It is important to note that the list returned by the factory methods is immutable.

Most commonly used technique of creating lists inline is to use an array. In Java an array can be initialised inline. Thus, we can create an array inline and use it to create a list.

Integer[] array = new Integer[]{1, 2, 3}; List list = Arrays.asList[array];

Code language: Java [java]

Here, we are creating an array and using it with Arrays.asList method to get a List of integers.

When we create a list in this way, the original array and the list refer to the same elements. This is why the list we get is unmodifiable. However, if we make changes to original array those will be reflected in the list as well.

array[0] = 100;

Code language: Java [java]

For example, we modified the first element in the array. Next, we will print both the list and the array and confirm that both of them are modified.

List -> [100, 2, 3] Array -> [100, 2, 3]

The asList method on the Arrays class takes varargs. Hence, instead of creating an array, we can pass the elements as method arguments.

List list = Arrays.asList[1, 2, 3];

Code language: Java [java]

In this tutorial on Creating and Initialising Java Lists Inline, we covered various way of creating mutable, immutable, and singleton lists using Java.

Further Reading:

How to Initialise a HashSet Inline in Java
How to Initialise a HashMap Inline in Java

Video liên quan

Chủ Đề