简体   繁体   English

java仅获取treeItem的子级值

[英]java get only children 's values of a treeItem

To display a treeItem Children I made: 要显示我制作的treeItem子代,请执行以下操作:

  System.out.println(
treeView.getSelectionModel().getSelectedItem().getChildren());

OUTPUT: 输出:

[TreeItem [ value: host1 ], TreeItem [ value: port1 ], TreeItem [ value: user1 ], TreeItem [ value: bd1 ]]

However, I only want to have a result with the values ('host1', 'port1'..) . 但是,我只想获得带有值的结果('host1', 'port1'..)

So how can I manipulte that output? 那么我该如何操纵该输出呢?

With java8 streams you can do it quite easily: 使用java8流,您可以很容易地做到这一点:

List<TreeItem> children = treeView.getSelectionModel().getSelectedItem().getChildren();
List<String> childrenValues = children.stream() // get stream from list
    .map(TreeItem::getValue) // equal to: item -> item.getValue()
    .collect(Collectors.toList()); // collect all to a single list
System.out.println(values);

Which should print the desired result 哪个应该打印期望的结果

getChildren() returns an ObservableList<TreeItem<T>> object. getChildren()返回一个ObservableList<TreeItem<T>>对象。 This is actually a list. 这实际上是一个列表。 If you want to print only the values, you should iterate over this list and print the value of each TreeItem object using the getValue() method. 如果只想打印值,则应遍历此列表,并使用getValue()方法打印每个TreeItem对象的值。

List<TreeItem<Object>> children = 
treeView.getSelectionModel().getSelectedItem().getChildren();

for (TreeItem<Object> child : children) {
    System.out.println(child.getValue())
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM