gpt4 book ai didi

java - 以树形 View 的形式过滤 ArrayList 的数据

转载 作者:行者123 更新时间:2023-12-01 22:58:39 25 4
gpt4 key购买 nike

我有一个 Detail.java 类具有一些属性 id、安全性、描述、项目。Items 是一个详细信息的 ArrayList

public class Detail {

private int id;
private boolean security;
private String description;
private List<Detail> items;

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public boolean isSecurity() {
return security;
}
public void setSecurity(boolean security) {
this.security = security;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public List<Detail> getItems() {
return items;
}
public void setItems(List<Detail> items) {
this.items = items;
}
}

这个 ArrayList( TreeView )包含我的数据

[
{
"id": 1,
"security": true,
"description": "description 1",
"items": [
{
"id": 11,
"security": true,
"description": "description 11"
},
{
"id": 12,
"security": false,
"description": "description 12",
"items": [
{
"id": 121,
"security": true,
"description": "description 121"
},
{
"id": 122,
"security": false,
"description": "description 122"
}
]
},
{
"id": 13,
"security": true,
"description": "description 13"
},
{
"id": 14,
"security": false,
"description": "description 14"
}
]
},
{
"id": 2,
"security": true,
"description": "description 2",
"items": [
{
"id": 21,
"security": true,
"description": "description 21"
},
{
"id": 22,
"security": false,
"description": "description 22"
},
{
"id": 23,
"security": true,
"description": "description 23"
},
{
"id": 24,
"security": false,
"description": "description 24"
}
]
}

]

我想通过过滤安全性为真的所有节点来过滤或创建这个ArrayList

最好的方法是什么?使用迭代器?使用第一个创建一个新的 arrayList和克隆对象?如何管理元素?

感谢您的帮助

最佳答案

最近看到这个问题,所以就试了一下。这个递归解决方案似乎对我有用:

public class Main {

// ArrayList to popluate with elements
private static List<Detail> myDetails2 = new ArrayList<>();

public static void main(String[] args) {

List<Detail> myDetails = new ArrayList<>();

// Building up some sample data
Detail detail = new Detail();
detail.setId(1);
detail.setSecurity(true);
detail.setItems(Collections.emptyList());

Detail detail5 = new Detail();
detail5.setId(6);
detail5.setSecurity(true);
detail5.setItems(Collections.emptyList());

Detail detail4 = new Detail();
detail4.setId(5);
detail4.setSecurity(true);
detail4.setItems(Arrays.asList(detail5));

Detail detail3 = new Detail();
detail3.setId(4);
detail3.setSecurity(true);
detail3.setItems(Arrays.asList(detail4));

Detail detail2 = new Detail();
detail2.setId(3);
detail2.setSecurity(true);
detail2.setItems(Arrays.asList(detail3));

Detail detail1 = new Detail();
detail1.setId(2);
detail1.setSecurity(false);
detail1.setItems(Arrays.asList(detail2));

myDetails.add(detail);
myDetails.add(detail1);

constructDetailsListWithSecurity(myDetails);
// Printing out the id of the elements that were added to the filtered ArrayList
myDetails2.forEach(d -> System.out.println("id: " + d.getId()));
}

public static void constructDetailsListWithSecurity(List<Detail> myDetails) {

if (myDetails.isEmpty()) {
return;
}

for (Detail detail : myDetails) {
if (detail.isSecurity()) {
myDetails2.add(detail);
}
if (detail.getItems() != null && !detail.getItems().isEmpty()) {
List<Detail> items = detail.getItems();
constructDetailsListWithSecurity(items);
}
}
}

}

我基本上只使用安全性设置为 true 的元素填充 ArrayList(我希望我能理解这一点,否则我们需要稍微更改一下逻辑)。对于这个解决方案,深度并不重要。这意味着它仍然可以处理 Detail 对象具有详细信息列表,而详细信息列表又具有详细信息列表等的情况。

关于java - 以树形 View 的形式过滤 ArrayList 的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58429374/

25 4 0