
There are multiple ways in which one can initialize an ArrayList in Java with values, just like Arrays, let us take a look at a few of them.
Using an Arrays.asList()
If you already have an array that is declared and initialized, you can create Initialize an ArrayList using it.
String[] countriesArray = {"USA","China","Canada","Japan","Australia"};
List<String> arrayList = new ArrayList<>(Arrays.asList(countriesArray));
You can do this in one step as well,
List<String> arrayList = new ArrayList&ly;>(Arrays.asList("USA","China","Canada","Japan","Australia"));
Java 8: Using the Stream.of(T values) method:
ArrayList<String> arrayList = new ArrayList<>(
Stream.of("Australia", "India", "Sweden").collect(Collectors.toList())
);
Java 9: Using the List.of(Element elements) method.
ArrayList<String> arrayList = new ArrayList<>(List.of("Apple", "Banana", "Orange"));
Java 10: Using the List.copyOf() method.
ArrayList countriesList = new ArrayList<>(Arrays.asList("Canada", "UK", "France"));
ArrayList newList = new ArrayList<>(List.copyOf(countriesList));
Using Java clone() method
ArrayList<String> cityList = new ArrayList<>(Arrays.asList("NYC", "Austin", "Chicago"));
ArrayList<String> cityListClone = (ArrayList<String>) cityList.clone();
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- CRUD operations in Spring Boot + JDBC
- Java Check Leap Year - Programs with Code Examples
- [fix] Java JDBC ConnectException: Connection refused
- How to add hours and minutes to Java Instant
- Java Program: Random Number Generator
- Java: The value of the local variable string is not used
- How to get list of all Java versions installed on macOS
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Java + Spring JDBC Template + Gradle Example
- Convert String to LocalDate in Java
- Remove Trailing zeros BigDecimal Java
- Java 8 Predicate Functional Interface isEqual() Method Example
- How to Hardcode Date in Java with Examples
- Java 8: Predicate negate() default Function Example
- Java: Collect Stream as ArrayList or LinkedList
- The Motivation Behind Generics in Java Programming
- How to Add/Subtract Days to the Current Date in Java
- Error: Can not find the tag library descriptor for
- Setting up JUnit 5 dependency with Maven Example
- Run Java Code Every Second
- How to create a tar.gz file using Java
- [Fix] java: integer number too large compilation error
- Java 8: Find the Max value in a List
- Your JBoss Application Server 7 is running However you have not yet added any users to be able to access the admin console
- Convert Java Array to ArrayList Code Example
More Posts:
- How to pip install Python Modules in VSCode - Python
- RabbitMQ Java Spring Boot Application Properties List - Java
- Responsive Web Design with CSS Media Queries: A Beginner's Tutorial [Updated for 2023] - CSS
- Docker Alpine Linux and Apache2 Example - Docker
- How to delete a file using Python code example - Python
- FCM Messages! Testing Notifcation from Microsoft to investigate this problem [Microsoft Teams] - Microsoft
- How to delete all text after a character or string in Notepad++ - NotepadPlusPlus
- Call PHP function on Button click using jquery ajax - PHP