简体   繁体   中英

java get only children 's values of a treeItem

To display a treeItem Children I made:

  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'..) .

So how can I manipulte that output?

With java8 streams you can do it quite easily:

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. 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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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