二个for循环吧,暂时没有想到其它好办法。
public class Test {
public static void main(String[] args) throws Exception {
String json = "[{\"parentID\": 0,\"sequence\": 0,\"id\": 1,\"leaf\": true,\"children\": []}, {\"parentID\": 0,\"sequence\": 0,\"id\": 2,\"leaf\": true,\"children\": []}, {\"parentID\": 1,\"sequence\": 0,\"id\": 3,\"leaf\": true,\"children\": []}, {\"parentID\": 2,\"sequence\": 0,\"id\": 4,\"leaf\": true,\"children\": []}, {\"parentID\": 2,\"sequence\": 0,\"id\": 5,\"leaf\": true,\"children\": []}]";
List rootList1 = JsonUtil.toList(json, Root.class);
Set idSet = new HashSet();
for(Root r : rootList1){
idSet.add(r.getId());
}
for(Root r : rootList1){
if(!idSet.contains(r.getParentID())){
System.out.println(r);
}
}
}
}
class Root{
private Integer parentID;
private Integer sequence;
private Integer id;
private boolean leaf;
private List children;
public Integer getParentID() {
return parentID;
}
public void setParentID(Integer parentID) {
this.parentID = parentID;
}
public Integer getSequence() {
return sequence;
}
public void setSequence(Integer sequence) {
this.sequence = sequence;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public boolean isLeaf() {
return leaf;
}
public void setLeaf(boolean leaf) {
this.leaf = leaf;
}
public List getChildren() {
return children;
}
public void setChildren(List children) {
this.children = children;
}
@Override
public String toString() {
return "Root [parentID=" + parentID + ", sequence=" + sequence
+ ", id=" + id + ", leaf=" + leaf + ", children=" + children
+ "]";
}
}