Reputation: 3
I have class
public class ListItem {
private final String text;
private final BooleanProperty isSelected = new SimpleBooleanProperty();
public BooleanProperty selectedProperty() {
return isSelected ;
}
public final boolean isSelected() {
return selectedProperty().get();
}
public final void setSelected(boolean isSelected) {
selectedProperty().set(isSelected);
}
public ListItem(String text) {
this.text = text ;
}
public String getText() {
return text;
}
@Override
public String toString() {
return getText();
}
}
in other class I have this
private ObservableList<ListItem> ListData = FXCollections.observableArrayList();
some where in code i do this:
Stream<String> stream = Files.lines(Paths.get(FILENAME), Charset.forName("windows-1251") );
ListData = stream
.filter(line -> line.startsWith("File"))
.map(line -> line.substring(line.indexOf("=") + 1, line.length()))
.collect(Collectors.toCollection(????));
What I must write in ???? position for convert string stream values to ListItem values? This convertion is possible?
Upvotes: 0
Views: 231
Reputation: 82461
Since you've already created the ObservableList
, you could simply use forEach
to add every item to ListData
. Furthermore you need to use the constructor of ListItem
to wrap every element of the stream in a ListItem
:
stream
.filter(line -> line.startsWith("File"))
.map(line -> line.substring(line.indexOf("=") + 1)) // there's also a version of substring that only takes the start index
.map(ListItem::new) // equivalent to .map(line -> new ListItem(line))
.forEach(ListData::add);
In case the list may not be empty this needs to be preceeded by ListData.clear();
.
Upvotes: 1
Reputation: 11411
The collectionFactory
parameter of Collectors.toCollection()
creates a Collection
, which then has add()
called on it for each member of the stream.
You need to convert the Strings in your stream to ListItem
s -- use another map
for this, then you can just use Collectors.toList()
.
Upvotes: 0