I have a m X n
two dimensional array of an Object say Foo
. So I have Foo[][] foosArray
. What is the best way to convert this into a List<Foo>
in Java?
This is a nice way of doing it for any two-dimensional array, assuming you want them in the following order:
[[array[0]-elems], [array[1]elems]...]
public <T> List<T> twoDArrayToList(T[][] twoDArray) {
List<T> list = new ArrayList<T>();
for (T[] array : twoDArray) {
list.addAll(Arrays.asList(array));
}
return list;
}
Since java-8
List<Foo> collection = Arrays.stream(array) //'array' is two-dimensional
.flatMap(Arrays::stream)
.collect(Collectors.toList());
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
yourList.add(foosArray[i][j]);
I think other tricks are unnecessary, because, anyway, they'll use this solution.
This can be done using Java 8 stream API this way:
String[][] dataSet = new String[][] {{...}, {...}, ...};
List<List<String> list = Arrays.stream(dataSet)
.map(Arrays::asList)
.collect(Collectors.toList());
Basically, you do three things:
The only way to transform it to a list is to iterate through the array and build the list as you go, like this:
ArrayList<Foo[]> list = new ArrayList<Foo[]>(foosArray.length);
for(Foo[] foo: foosArray){
list.add(foo);
}
Use java8 "flatMap" to play around. One way could be following
List<Foo> collection = Arrays.stream(array).flatMap(Arrays::stream).collect(Collectors.toList());
Here is a step by step solution to convert a 2D array of int (ie primitive data types) to a list. The explanations are in the code comments.
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Temp {
public static void main(String... args) {
System.out.println("Demo...");
int [] [] twoDimArray = new int[][]{
{1,2}, {3,5}, {8,15}
};
List< List<Integer> > nestedLists =
Arrays.
//Convert the 2d array into a stream.
stream(twoDimArray).
//Map each 1d array (internalArray) in 2d array to a List.
map(
//Stream all the elements of each 1d array and put them into a list of Integer.
internalArray -> Arrays.stream(internalArray).boxed().collect(Collectors.toList()
)
//Put all the lists from the previous step into one big list.
).collect(Collectors.toList());
nestedLists.forEach(System.out::println);
}
}
Output :
Demo...
[1, 2]
[3, 5]
[8, 15]
Please note that: while working on arrays conversion as a list there is difference between primitive array and Object array. ie) int[] and Integer[]
eg)
int [][] twoDArray = {
{1, 2, 3, 4, 40},
{5, 6, 7, 8, 50},
{9, 10, 11, 12, 60},
{13, 14, 15, 16, 70},
{17, 18, 19, 20, 80},
{21, 22, 23, 24, 90},
{25, 26, 27, 28, 100},
{29, 30, 31, 32, 110},
{33, 34, 35, 36, 120}};
List list = new ArrayList();
for (int[] array : twoDArray) {
//This will add int[] object into the list, and not the int values.
list.add(Arrays.asList(array));
}
and
Integer[][] twoDArray = {
{1, 2, 3, 4, 40},
{5, 6, 7, 8, 50},
{9, 10, 11, 12, 60},
{13, 14, 15, 16, 70},
{17, 18, 19, 20, 80},
{21, 22, 23, 24, 90},
{25, 26, 27, 28, 100},
{29, 30, 31, 32, 110},
{33, 34, 35, 36, 120}};
List list = new ArrayList();
for (Integer[] array : twoDArray) {
//This will add int values into the new list
// and that list will added to the main list
list.add(Arrays.asList(array));
}
To work on Keppil answer; you have to convert your primitive array to object array using How to convert int[] to Integer[] in Java?
Else add the int values one by one in the normal for loop.
int iLength = twoDArray.length;
List<List<Integer>> listOfLists = new ArrayList<>(iLength);
for (int i = 0; i < iLength; ++i) {
int jLength = twoDArray[0].length;
listOfLists.add(new ArrayList(jLength));
for (int j = 0; j < jLength; ++j) {
listOfLists.get(i).add(twoDArray[i][j]);
}
}
Also note that Arrays.asList(array) will give fixed-size list; so size cannot be modified .
another one technique.
//converting 2D array to string
String temp = Arrays.deepToString(fooArr).replaceAll("\\[", "").replaceAll("\\]", "");
List<String> fooList = new ArrayList<>(Arrays.asList(","));
ArrayList<Elelement[]> allElelementList = new ArrayList<>(allElelements.length);
allElelementList.addAll(Arrays.asList(allElelements));
allElelements is two-dimensional
Let's add some primitive type arrays processing. But you may pass to parameter any object. Method correctly process as int[][] as Object[][]:
public <T, E> List<E> twoDArrayToList(T twoDArray) {
if (twoDArray instanceof int[][])
return Arrays.stream((int[][])twoDArray)
.flatMapToInt(a->Arrays.stream(a))
.mapToObj(i->(E)Integer.valueOf(i))
.collect(Collectors.toList());
//… processing arrays of other primitive types
if (twoDArray instanceof Object[][])
return Arrays.stream((E[][])twoDArray)
.flatMap(Arrays::stream)
.collect(Collectors.toList());
return null; // better to throw appropriate exception
// if parameter is not two dimensional array
}
Eg
Integer[][] a = { { 1, 2, 3 }, { 4, 5, 6 }, { 9, 8, 9 }, };
List<List<Integer>> arr = Arrays.stream(a)
.map(Arrays::asList)
.collect(Collectors.toList());
In Java 8 , to convert from int[][]
to List<Integer>
, you can use the below code :
List<Integer> data = Arrays.stream(intervals)
.flatMapToInt(Arrays::stream)
.boxed()
.collect(Collectors.toList());
List<Foo> list = Arrays.stream(fooArray)
.flatMap(e -> Stream :: of).collect(Collectors.toList());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.