No because lambdas need a target type. The best you can do is cast the expression:
Item similarItem = ((Supplier- ) (() -> {
for (Item i : POSSIBLE_ITEMS) {
if (i.name.equals(this.name)) return i;
}
return null;
})).get();
You must use the correct Functional Interface for your particular lambda. As you can see, it is very clunky and not useful.
UPDATE
The above code is a direct translation of the JavaScript code. However, converting code directly will not always give the best result.
In Java you'd actually use streams to do what that code is doing:
Item similarItem = POSSIBLE_ITEMS.stream()
.filter(i -> i.name.equals(this.name))
.findFirst()
.orElse(null);
The above code assumes that POSSIBLE_ITEMS
is a Collection
, likely a List
. If it is an array, use this instead:
Item similarItem = Arrays.stream(POSSIBLE_ITEMS)
.filter(i -> i.name.equals(this.name))
.findFirst()
.orElse(null);