Convert Array to List In Java 8

By | July 31, 2018

In java 8 you can easily to convert array to list with stream.

Convert Array to List In Java 8

For example:

   String[] array = new String[10];

    List<String> listCollection = Arrays.stream(array).collect(Collectors.toList());
    ArrayList<String> arrayListCollection = Arrays.stream(array).collect(Collectors.toCollection(ArrayList::new));
    Set<String> setCollection = Arrays.stream(array).collect(Collectors.toSet());
    HashSet<String> hashSetCollection = Arrays.stream(array).collect(Collectors.toCollection(HashSet::new));
    TreeSet<String> treeSetCollection = Arrays.stream(array).collect(Collectors.toCollection(TreeSet::new));
    LinkedList<String> linkedListCollection = Arrays.stream(array).collect(Collectors.toCollection(LinkedList::new));
    LinkedHashSet<String> linkedHashSetCollection = Arrays.stream(array).collect(Collectors.toCollection(LinkedHashSet::new));
    ArrayDeque<String> arrayDequeCollection = Arrays.stream(array).collect(Collectors.toCollection(ArrayDeque::new));
    PriorityQueue<String> priorityQueueCollection = Arrays.stream(array).collect(Collectors.toCollection(PriorityQueue::new));

Convert Array to List In Java 8

Java 8 stream is very useful. You can do many thing with that. I hope this will help you to convert some array to list in java code.

BUT in simple case to convert Array to List just do like this :

Arrays.asList(yourArrayHere);

You also can search another example in google with this keyword : “Converting Array to List, How to convert a primitive Array to List, Java Array to List“.

Simple convert array to list in java

Leave a Reply

Your email address will not be published. Required fields are marked *