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.



Using Arrays.asList

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<String> list = Arrays.asList("One", "Two", "Three");
		System.out.println(list);		
	}
}

Output

[One, Two, Three]


Using Java 9 List.of

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<String> list = List.of("One", "Two", "Three");
		System.out.println(list);
	}
}

Output

[One, Two, Three]


Using Java 10 List.of

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<string> 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]


Using asList

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<String> list = asList("One", "Two", "Three");
		System.out.println(list);
	}
}

Output

[One, Two, Three]


Using Stream.of()

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<String> stream = Stream.of("One", "Two", "Three");
		List<String> 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<String> list = Stream.of("One", "Two", "Three").collect(Collectors.toList());
		System.out.println(list);
	}
}

Output

[One, Two, Three]


ArrayList using Arrays.asList

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<String> list = new ArrayList<String>(Arrays.asList("One", "Two", "Three"));
		System.out.println(list);
	}
}

Output

[One, Two, Three]


ArrayList using List.of

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<String> list = new ArrayList<String>(List.of("One", "Two", "Three"));
		System.out.println(list);
	}
}

Output

[One, Two, Three]


ArrayList using Steam.of

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<String> list = Stream.of("One", "Two", "Three")
                .collect(Collectors.toCollection(ArrayList::new));
		System.out.println(list);
	}
}

Output

[One, Two, Three]


ArrayList by Conventional method

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<String> list = new ArrayList<String>();
		list.add("One");
		list.add("Two");
		list.add("Three");
		System.out.println(list);
	}
}

Output

[One, Two, Three]


ArrayList using add method

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<String> list = new ArrayList<String>() {{
			add("One");
			add("Two");
			add("Three");
		}};
		System.out.println(list);
	}
}

Output

[One, Two, Three]



Leave a Reply