I tried to write the following code as a stream:
AbstractDevice myDevice = null; for (AbstractDevice device : session.getWorkplace().getDevices()) { if (device.getPluginconfig().getPluginType().getId() == 1) { myDevice = device; } }
this code works great.
But when I rewrite it like this, it no longer works:
myDevice = session.getWorkplace().getDevices().stream() .filter(s -> s.getPluginconfig().getPluginType().getId() == 1) .findFirst().get();
Optional , which I return from the stream, has no values ββin it. Why?
EDIT
When I try to do this (I still get two devices from getDevices() ):
List<AbstractDevice> testList = session.getWorkplace().getDevices() .stream().collect(Collectors.toList());
testList empty. So it seems that something went wrong with the flow of my List devices?
This is a JavaEE application, and I get my Devices from the corresponding object:
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH}) @JoinTable(name = "Workplace_AbstractDevice", joinColumns = { @JoinColumn(name = "Workplace", referencedColumnName = "ID") }, inverseJoinColumns = { @JoinColumn(name = "AbstractDevice", referencedColumnName = "ID") }) @OrderColumn private List<AbstractDevice> devices = new ArrayList<AbstractDevice>(); public List<AbstractDevice> getDevices() { return devices; }
source share