Streams lightbulb moment
Mar. 14th, 2016 03:59 pmI was trying to get my head round Java 8 streams. Here's a typical example:
So this seems cool, and you can make a stream parallel, so it might be a good way to implement a lot of things.
The lightbulb came on when I realised that you can only apply a predefined set of operations. The thing you're calling methods on is the stream, not the contents. So you might be able to re-write existing code in terms of filter, map, etc. but you can't just drop an existing chunk of code into a stream and get magic.
List<Integer> transactionsIds = transactions.stream()
.filter(t -> t.getType() == Transaction.GROCERY)
.sorted(comparing(Transaction::getValue).reversed())
.map(Transaction::getId)
.collect(toList()); "Get the grocery transactions, largest value first, and tell me their IDs."So this seems cool, and you can make a stream parallel, so it might be a good way to implement a lot of things.
The lightbulb came on when I realised that you can only apply a predefined set of operations. The thing you're calling methods on is the stream, not the contents. So you might be able to re-write existing code in terms of filter, map, etc. but you can't just drop an existing chunk of code into a stream and get magic.