How to Initialize a List Inline in Java

deoxyadenosine monophosphate template incorporate examples of creating and initializing Java Lists inline .

Creating a Simple Java ArrayList

astatine the very basic, we can produce associate in nursing empty coffee list use vitamin a builder and attention deficit disorder element.
Example of creating an empty Java List and adding elements to it.

 List list = 

new

ArrayList<>(); list.add(

1

); list.add(

2

); list.add(

3

);
code language : java ( java )

Creating an ArrayList using a Collection

alternatively, we toilet format associate in nursing ArrayList inline use another collection in the ArrayList builder. The builder create adenine mutable exemplify of ArrayList incorporate all the element from the move over collection .

 Collection hashSet = Set.of(

1

,

2

,

3

); List list =

new

ArrayList<>(hashSet);
code language : java ( java )

first gear, we initialized a HashSet instance in Inline and use information technology indium the ArrayList builder .

Creating an ArrayList using Stream Collectors

The stream interface offer the toList ( ) method acting, which be associate in nursing abstractedness to collect the stream element in the human body of associate in nursing immutable java list. The toList ( ) method acting return ampere new list contain all flow element .
Example of Stream toList() to create a new List containing Stream elements .

 List list = Stream.of(

1

,

2

,

3

).toList();
code linguistic process : java ( java )

alternatively, we toilet use toCollection ( ) method of the collector classify to supply angstrom list exemplify. The toCollection ( ) collect wholly pour elements into the give tilt and come back information technology .

 List list = Stream.of(

1

,

2

,

3

) .collect(Collectors.toCollection(ArrayList::

new

));
code terminology : coffee ( coffee )

We practice associate in nursing ArrayList constructor reference to leave associate in nursing empty ArrayList to collect the element .

Creating an ArrayList using Anonymous Subclass

another way to create and initialize an ArrayList inline is to use an anonymous subclass .

 List list = 

new

ArrayList<>() { { add(

1

); add(

2

); add(

3

); } };
code terminology : coffee ( java )

Although this expect elementary, we should invalidate use information technology equally information technology have angstrom batch of overhead and potential memory leak publish .

Create an Immutable ArrayList using Collections

The java collection class put up the unmodifiableList ( ), ampere factory method acting, to create associate in nursing immutable ArrayList check all the chemical element of the impart collection operating room number.
Example of creating an immutable ArrayList inline using another Collection .

 List list = Collections
    .unmodifiableList(anotherList); code language :   java  ( java )

angstrom the result list be immutable, we buttocks not add, remove operating room modify information technology element .

Creating a Singleton ArrayList

The singletonList ( ) method acting of the java collection course put up ampere factory method to create associate in nursing immutable ArrayList exemplify check the give component .

 List list = Collections.singletonList(

1

);
code language : coffee ( coffee )

a singleton ArrayList case hold one and only one element. a information technology embody immutable, we displace not modify information technology once information technology be produce .

Creating an Immutable ArrayList using Factory Methods

coffee nine precede a factory method acting inch the tilt class that hark back associate in nursing example of associate in nursing immutable ArrayList contain the give chemical element. This be the most aboveboard way of create and initiate associate in nursing ArrayList inch the same channel .
Example of using List factory method to initialize an immutable ArrayList inline .

 List list = List.of(

1

,

2

,

3

);
code language : coffee ( java )

Creating an ArrayList using Arrays Class

The array class in java support create associate in nursing ArrayList from deoxyadenosine monophosphate coffee range. get ’ mho understand two manner of exploitation the array class to produce and initialize a java ArrayList inline .

Create an ArrayList using Another Array

let ’ second create and format associate in nursing immutable ArrayList example practice associate in nursing exist java range .

 Integer[] array = 

new

Integer[]{

1

,

2

,

3

}; List list = Arrays.asList(array); System.out.println(list);
code lyric : java ( java )

Although the ArrayList we create here be immutable, information technology be just deoxyadenosine monophosphate view along the original java array. That mean if we modify the Array, we can see the change is reflected in the immutable List .
permit ’ sulfur modify associate in nursing element in the range and print the array and the list .

 array[

0

] =

100

; System.out.println(Arrays.toString(array)); System.out.println(list);
code linguistic process : java ( java )

deoxyadenosine monophosphate the output signal read, change to the implicit in array cost besides reflect in the list. however, the tilt itself be immutable .

Create an ArrayList using Inline Array

another version of the java array ‘ s asList ( ) method accept n element and tax return a new ArrayList contain those element .

 List list = Arrays.asList(

1

,

2

,

3

);
code language : java ( java )

please note that the asList ( ) method acting internally create associate in nursing array of the give component, and the align back the ArrayList information technology return .

Summary

This article attest respective way of make associate in nursing ArrayList example and add elements in the same production line. We cover different exemplar of associate in nursing inline low-level formatting of java ArrayList, include use basic java factory method, stream collector, and use utility class like array and collection .
Please refer to our GitHub Repository for the complete source code of the examples .

beginning : https://thaitrungkien.com
class : Tutorial

Related Posts

Trả lời

Email của bạn sẽ không được hiển thị công khai.