问题
I'm struggling to create the right logic to flatten an array. I essentially want to duplicate parent rows for each child item in a nested array. The number of nested arrays could vary. I've been creating Java lists bc I find them easy to work with, but open to any solution. The nature of this problem is I'm starting with some nested JSON that I want to convert into a flat csv to load into a database table. Thanks for the help.
Example:
[1,2,[A,B,[Cat,Dog]],3]
I've created the above as a List. Each item is either a string or another List.
Result:
[1,2,A,Cat,3],
[1,2,A,Dog,3],
[1,2,B,Cat,3],
[1,2,B,Dog,3]
Here's what I have so far. Obviously not working.
private static List<List<String>> processData(List<String> row, List<Object> data, List<List<String>> rowList) {
List<List<String>> tempRowList = new ArrayList<List<String>>();
for (Object i : data) {
if (i instanceof List<?>) {
flattenArray((List<Object>) i, row, rowList);
} else {
for (List<String> r : rowList) {
r.add(i.toString()); //add item to all rows
}
}
}
return rowList;
private static void flattenArray(List<Object> arrayObject, List<String> rowToCopy, List<List<String>> rowList) {
for (Object x : arrayObject) {
if (x instanceof List<?>) {
for (List<String> t : rowList) {
flattenArray((List<Object>) x, t, rowList);
}
} else {
List<String> newRow = new ArrayList<String>(rowToCopy); //copy row
List<Object> t = new ArrayList<Object>();
t.add(x);
List<List<String>> innerRowList = new ArrayList<List<String>>();
innerRowList.add(newRow);
processData(newRow, t, innerRowList); //pass in new copied row. object to add,
rowList.add(newRow);
}
}
rowList.remove(rowToCopy);
}
And i set everything up like this.
public static void main(String[] args) {
List<Object> data = new ArrayList<Object>();
List<List<String>> rowList = new ArrayList<List<String>>();
data.add("1");
data.add("2");
List<Object> l1 = new ArrayList<Object>();
l1.add("A");
l1.add("B");
List<Object> l2 = new ArrayList<Object>();
l2.add("dog");
l2.add("cat");
l1.add(l2);
data.add(l1);
data.add("3");
List<String> r0 = new ArrayList<String>();
rowList.add(r0);
System.out.println(data);
rowList = processData(r0, data, rowList);
System.out.println(rowList);
}
回答1:
I think this should work for you if you're using Java 8:
List<Object> a = new ArrayList<>();
List<Object> a1 = new ArrayList<>();
a1.add("v");
a1.add("w");
List<Object> a2 = new ArrayList<>();
a2.add("ww");
a.add("a");
a.add("b");
a.add("c");
a.add("d");
a.add(a1);
a.add(a2);
List<Object> b = new ArrayList<>();
a.stream().flatMap(x -> x instanceof String ? Stream.of(x) : ((List) x).stream()).forEach(b::add);
System.out.println(a);
System.out.println(b);
b
list will contain flattened a
list. Output is:
[a, b, c, d, [v, w], [ww]]
[a, b, c, d, v, w, ww]
来源:https://stackoverflow.com/questions/40218888/flatten-nested-arrays-java