Convert matrix to List Java
In this post, we will see how to convert array to list in java. Show There are may ways to convert array to list: Table of Contents
By using Arrays.asList():This is an easy method to convert Array into list.We just need to pass array as parameter to asList() method.It is a Static method available in Arrays class so, called it by class name Arrays.asList(). Syntax:
It returns a fixed size List as of size of given array.Since returned List is fixed-size we cannot add more elements.If we do so it throws UnsupportedOperationException And T represent generics means any type of Array. Example:
OUTPUT: Elements of Array are: Using Collections.addAll() method :This method is available in Collections class.Remember that the array and List should be of same type. Syntax: public static boolean addAll(Collection c,T x) Where c is a collection into which value is to be insert and x is an array contains values to be insert in c.
OUTPUT: Elements of Array are: Using Java 8s StreamWe can use Java 8s Stream to convert an array to List.
OUTPUT: Elements of Array are: 1 3 5 6 10 Elements of list are: [1,3,5,6,10] Manual way using add() method:We use add() method inside loop to convert Array into List.
OUTPUT: Elements of Array are: [81] [82] [83] [84] [85] Elements of list are: [81,82,83,84,85] Thats all about Converting the array to List in java. |