我有一个对象数组,它基本上是一个 Arraylist。输入和输出数据如下所示
data= [
{
id:1,
parent:0
},
{
id:2,
parent:0
},
{
id:3,
parent:1
},
{
id:4,
parent:1
},
{
id:5,
parent:3
},
{
id:6,
parent:3
}
]
如果任何对象的父对象等于 id,那么它将成为子对象,结果应该是这样的。
[
{
id:1,
parent:0,
children:[
{
id:3,
parent:1,
children:[ {
id:5,
parent:3
},
{
id:6,
parent:3
}
]
},
{
id:4,
parent:1
}
]
},
{
id:2,
parent:0
}
]
因为我无法使用 for 循环访问对象数组的内部元素,所以我无法对其设置条件。我尝试使用这样的东西
for(Map <String,Object> individual_object: data) {
}
这该怎么做?
我的完整代码:
try {
taxonDao.openCurrentSession();
List<Object[]> taxonList = taxonDao.list(parent, classificationId, taxonIds, expand_taxon);
List res = new ArrayList();
Map<String, Object> m1 = new HashMap<String, Object>();
TaxonUI ui = new TaxonUI();
Map<Long, Map<String, Object>> m2 = new HashMap<Long, Map<String, Object>>();
for (Object[] t : taxonList) {
Map<String, Object> m = new HashMap<String, Object>();
ui.setId((Long) t[0]);
ui.setTaxonid((Long) t[0]);
ui.setClassification((Long) t[4]);
ui.setPath((String) t[3]);
ui.setText((String) t[1]);
ui.setRank((Integer) t[2]);
if(t[5]!=null){
ui.setParent((Long)t[5]);
}
m.put("id", ui.getId());
m.put("taxonid", ui.getId());
m.put("text", ui.getText());
m.put("rank", ui.getRank());
m.put("path", ui.getPath());
m.put("classification", ui.getClassification());
m.put("parent", ui.getParent());
res.add(m);
}
return res;
} catch (Exception e) {
throw e;
} finally {
taxonDao.closeCurrentSession();
}
首先你需要一个 Item 类来存储 id 和 parent id:
(toString() 用于调试,因此您可以打印输入和输出列表以验证结果)
public class Item {
private final int parentId;
private final int id;
private final java.util.List<Item> children = new ArrayList<>();
public Item(int parentId, int id) {
this.parentId = parentId;
this.id = id;
}
public void addChild(Item child) {
children.add(child);
}
@Override
public String toString() {
String result = "id: " + id + ", parent: " + parentId;
if (children.isEmpty() == false) {
result += ", children: " + children;
}
return result;
}
public int getId() {
return id;
}
public int getParentId() {
return parentId;
}
}
然后像往常一样,一个 Main 类和 main 方法来准备输入并处理它:
public class Main {
public static void main(String[] args) {
java.util.List<Item> inputItems = createInputItems();
java.util.List<Item> oututItems = processItemsToParentChildren(inputItems);
System.out.println(oututItems);
}
创建输入法很简单:
private static List<Item> createInputItems() {
java.util.List<Item> result = new ArrayList<>();
result.add(new Item(0, 1));
result.add(new Item(0, 2));
result.add(new Item(1, 3));
result.add(new Item(1, 4));
result.add(new Item(3, 5));
result.add(new Item(3, 6));
return result;
}
然后你需要一个方法来将 id 映射到对应的 item:
private static Map<Integer, Item> prepareIdItemMap(List<Item> items) {
HashMap<Integer, Item> result = new HashMap<>();
for (Item eachItem : items) {
result.put(Integer.valueOf(eachItem.getId()), eachItem);
}
return result;
}
然后是关键部分,将正确的子项添加到其父项,或者如果父项 id 为 0,则添加到列表的根:
private static List<Item> processItemsToParentChildren(List<Item> items) {
java.util.List<Item> result = new ArrayList<>();
Map<Integer, Item> idItemMap = prepareIdItemMap(items);
for (Item eachItem : items) {
int parentId = eachItem.getParentId();
if (parentId == 0) {
result.add(eachItem);
} else {
idItemMap.get(Integer.valueOf(parentId)).addChild(eachItem);
}
}
return result;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句