Hello! I have a stream of items much like this: `i...
# getting-started
o
Hello! I have a stream of items much like this:
items.stream().filter { it.someStuff.equals(search) }.findFirst().orElseThrow( *insert exception* )
I tried to put UnsupportedOperationException() but get problem with ExceptionSupplier. How should I do this?
t
ExceptionSupplier should be a function that takes no arguments and returns a subtype of Exception. In Kotlin you can express that in 2 different ways :
Copy code
orElseThrow { UnsupportedOperationException() }
OR
Copy code
orElseThrow(::UnsupportedOperationException)
The latter is a function reference that is equivalent to
Copy code
orElseThrow(UnsupportedOperationException::new)
is Java.
👌 2
o
Thanks!
m
If you use IntelliJ, it will often show you some simplifications. For example, I would expect it to tell you that
filter
followed by
findFirst
can be replaced by
find
.
👌 1
o
Yeah, use those simplifications all the time 😄