Meegh
Meegh

Reputation: 39

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?

Upvotes: 0

Views: 865

Answers (2)

Lino
Lino

Reputation: 19926

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

Upvotes: 1

Anastasios Vlasopoulos
Anastasios Vlasopoulos

Reputation: 1802

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())
}

Upvotes: 2

Related Questions