VOOZH about

URL: https://www.javacodegeeks.com/2020/05/how-to-convert-array-to-list-in-java.html

⇱ How to convert Array to List in Java - Java Code Geeks


Hello Friends,

In this tutorial, we will learn, various ways in which we can convert an array to a List.

package com.blogspot.javasolutionsguide;

import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class ArrayToList {

 public static void main(String[] args) {

 //Before Java 8
 System.out.println("Before JAVA 8........................................");

 // 1.
 String[] names = new String[] {"Gaurav", "Sachin", "Yuvraj"};
 List<String> nameList = Arrays.asList(names);
 System.out.println("using Arrays.asList: " + nameList);

 //2
 List<Integer> list = Arrays.asList(1, 2, 3);
 System.out.println("Inline primitive array to Integer List using Arrays.asList(): "
 + list);

 //Java 8
 System.out.println("JAVA 8 ................................................");
 // 3. primitive array
 int intArray[] = {1, 2, 3};
 List<Integer> integerList1 = Arrays.stream(intArray).boxed()
 .collect(Collectors.toList());
 System.out.println("Primitive int array to Integer List using Array.stream(): "
 + integerList1);

 // 4. primitive array with IntStream
 List<Integer> integerList2 = IntStream.of(intArray).boxed()
 .collect(Collectors.toList());
 System.out.println("Primitive int array to Integer List using IntStream.of(): "
 + integerList2);

 // 5. Object type array to Integer List
 Integer integerArray[] = {1, 2, 3};
 List<Integer> integerList3 = Arrays.stream(integerArray)
 .collect(Collectors.toList());
 System.out.println("Object type array to Integer List using Arrays.stream: "
 + integerList3);

 // 6. Integer Array to ArrayList
 ArrayList<Integer> arrayList = Arrays.stream(integerArray)
 .collect(Collectors.toCollection(ArrayList::new));
 System.out.println("Integer type array to Integer ArrayList using Arrays.stream: "
 + arrayList);

 // 7. Integer Array to LinkedList
 LinkedList<Integer> linkedList = Stream.of(integerArray)
 .collect(Collectors.toCollection(LinkedList::new));
 System.out.println("Integer type array to Integer LinkedList using Arrays.stream: "
 + linkedList);

 // 8. Integer Array to Immutable List
 List<Integer> immutableList = Collections.unmodifiableList(Arrays.asList(integerArray));
 System.out.println("Integer type array to Immutable List using Arrays.stream: "
 + immutableList);

 //9. JAVA 9
 System.out.println("JAVA 9.........................................................");
 String[] playerNames = {"Sachin", "Dhoni", "Yuvraj"};
 List<String> players = List.of(playerNames);
 System.out.println("Array to List using Java 9 List.of() :" + players);

 //10. JAVA 10
 System.out.println("JAVA 10.........................................................");
 List<Integer> integerList = List.copyOf(Arrays.asList(integerArray));
 System.out.println("Array to List using Java 10 List.copyOf() :" + integerList);

 //11. Apache Commons
 System.out.println("Apache Commons...................................................");
 List<Integer> targetList = new ArrayList<>(6);
 CollectionUtils.addAll(targetList, integerArray);
 System.out.println("Array to List using Apache Common CollectionUtils.addAll() :"
 + integerList);

 //12 Google Guava
 System.out.println("Google Guava......................................................");
 Integer[] sourceArray = {1, 2, 3};
 List<Integer> targetList1 = Lists.newArrayList(sourceArray);
 System.out.println("Array to List using Google Guava Lists.newArrayList:"
 + integerList);
 }
}
External Dependency Used :
<dependency>
 <groupId>org.apache.commons</groupId>
 <artifactId>commons-collections4</artifactId
 <version>4.4</version>
 </dependency>
 <dependency>
 <groupId>org.apache.commons</groupId>
 <artifactId>commons-lang3</artifactId>
 <version>3.10</version>
 </dependency>
 <dependency>
 <groupId>com.google.guava</groupId>
 <artifactId>guava</artifactId>
 <version>16.0.1</version>
 </dependency>

All the code for this tutorial can be found in GitHub. So in this tutorial we learnt various ways in which we can convert array to List in java.That is all for this tutorial.Please subscribe the blog for more such tutorial.

Published on Java Code Geeks with permission by Gaurav Bhardwaj, partner at our JCG program. See the original article here: How to convert Array to List in Java

Opinions expressed by Java Code Geeks contributors are their own.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

👁 Photo of Gaurav Bhardwaj
Gaurav Bhardwaj
May 13th, 2020Last Updated: May 11th, 2020
1 199 2 minutes read

Gaurav Bhardwaj

Gaurav has done Masters in Computer Applications(MCA) and is working in Software development field for more than 10 years in Java/J2EE technologies. He is currently working with one of top MNC. He has worked on various frameworks like Struts, Spring, Spring Boot, Angular JS, JSF, Velocity, iBatis, MyBatis, Hibernate, JUnit, Mockito, Dozzer. He likes to explore new technologies and share his thoughts by writing a technical blog. He is the founder of JavaSolutionsGuide.blogspot.com.
Subscribe

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
6 years ago

I have provided latest java tutorials may be helpful at http://www.androidcoding.in/tutorials-java/

0
Reply
Back to top button
Close
wpDiscuz