Java 8 MOOC - Session 3 Summary
The final part of the Java 8 online course turns out to be significantly more difficult.
Join the DZone community and get the full member experience.Join For Free
last night was the final get-together to discuss the java 8 mooc . any event hosted in august in a city that is regularly over 40°c is going to face challenges, so it was great that we had attendees from earlier sessions plus new people too.
the aim of this session was to talk about lesson 3, but also to wrap up the course as a whole: to talk about what we liked and what we would have improved (about both the course itself and our user group events).
is the deterministic version, which will return you the first element in the stream (according to encounter order - see the section on
ordering in the documentation
). so, regardless of whether you run the operation in parallel or serial, if you’re looking for “a” and use
with this list:
["b", "z", "c", "a", "l", "k", "a", "h"]
you’ll get the element at index 3 - the first “a” in the list.
is non-deterministic, so will return you any element that matches your criteria - it could return the element at index 3, or the one at position 6. realistically, if the stream is on an ordered collection like a list, when you run
on a sequential stream, i expect it will return the same result as
. the real use-case for
is when you’re running this on a parallel stream. let’s take the above list, and assume that when you run this on a parallel stream it’s processed by two separate threads:
["b", "z", "c", "a", // processed by thread 1 "l", "k", "a", "h"] // processed by thread 2
it’s possible that thread 2 finds its “a” (the one at position 6) before thread 1 finds the one at position 3, so this will be value that’s returned. by configuring the stream to return any one of the values that matches the criteria, you can potentially execute the operation faster when running in parallel.
is (potentially) faster in parallel and (probably) returns the same value as
when running in serial, why not use that all the time? well, there are times when you really do want the first item. if you have a list of dvds ordered by year the film was released, and you want to find the original “king kong” (for example), you’ll want
to find the one released in 1933, not the one that was released in 1976 or the one from 2005.
is not always going to be slower than
, even in parallel. going back to our list:
["b", "z", "c", "a", "l", "k", "a", "h"]
for “h” could be the same performance for both methods.
collectors : maybe it’s just me who doesn’t really see the big picture for collectors. i’m perfectly content with the built in collectors like:
it’s easy to see what they do, and work out when you need to use them.
i’m also very happy to have discovered
a super-useful way to create comma separated values (csvs) that i use in my java 8 demo .
where things get a bit murky for me is where we start chaining up collectors:
(it should be obvious from my lack of clear example that i’m not 100% certain under which circumstances these are useful).
as a group, we think the chained collectors are kinda ugly - not because we’re against chaining (we like streams), but maybe because it’s another chain inside a param to a chain.
we think this is an area where some good, solid examples and a bit of daily use will make it much clearer to developers. we hope.
related to this, the course didn’t go into creating your own collectors at all. my personal (under-informed) opinion is that i guess most developers should be able to use either the out-of-the-box collectors (
etc) or use the collector chaining to build what they need. if you need a custom collector, perhaps you haven’t considered everything that’s already available to you. but as a group, we decided we would have liked to see this topic anyway so that we could get a deeper understanding of what collectors are and how they work.
exercises for lesson 3: well. what can we say? i really hope there are people reading this who haven’t finished the course yet, because the sevilla java user group would like to say to you: don’t despair, the lesson 3 exercises are substantially harder than those for lessons 1 and 2. honestly, the whole group considered it less of a learning curve and more of a massive cliff to climb.
i mean, it was great to have something so challenging to end on, but it probably would have been less ego-destroying if we could have got up to that level gradually instead of having it sprung on us.
the good thing about part 2 of the lesson 3 exercises was that we had three very different answers to discuss in the group. none of us were super happy with any of them, but we could see definite pros and cons of each approach, and that’s something you really want to learn in a course like this.
it was also really great to have a rough performance test to run on your own computer, so that you could really see the impact of your choices on the performance of the stream.
for more info
i’m going to add a shameless plug to a friend’s book here. i’ve been reading a lot about java 8 for this course, for my java 8 demo , and to generally get up to speed. my favourite book for getting to grips with lambdas and streams is java 8 lambdas: pragmatic functional programming this book also contains more info about collectors too, so maybe some of our questions around how to use these in more complex situation are answered in here.
we really enjoyed the mooc, and the sessions to get together to discuss it. we particularly liked that the meetups were a safe place to ask questions and discuss alternative solutions, and that we weren’t expected to be genius-level experts in order to participate fully.
if/when oracle re-runs the mooc, if you didn’t get a chance to take part this time i highly recommend signing up. and if you can find (or run) a local meetup to discuss it, it makes the experience much more fun.
Published at DZone with permission of Trisha Gee, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.