[2019 oracle code one] exploring collectors

Exploring Collectors

Speakers: Venkat Subramaniam

For more blog posts, see The Oracle Code One table of contents


General

  • Common operations; filter, map, reduce
  • Filter – like a coin sorter. Will let some values through and discard others
  • Reduce – go from a stream to a non stream
  • Collect is a reduce operation
  • Functions should be pure – doesn’t change anything *and* doesn’t depend on anything that could change

Collectors – concepts

  • Don’t call add() in a forEach. Should be using a Collector
  • Can’t parallelize code when have shared mutability (ex: add() in forEach)
  • Can’t say “the code worked”. Can say “the code behaved”
  • If use ConcurrentList, it’s a ticking time bomb for when someone changes the list type.
  • Should write collect(Collectors.toList()) or collect(toList()). Already handles concurrency (when running with a parallel stream)
  • Venkat prefers using Collectors as a static import and just calling toList()
  • Collectors are recursive data structures. The second parameter is another Collector
  • Often need to chain collectors to do what want.
  • Ok to write code “the long way” and then refactor once have passing tests

Collectors – Code

  • Java 10+: toUnmodifiableList() – immutable list
  • partitioningBy – when need both the matching and non matching results. Avoids needing two passes of data to get result.
  • joining(“, “) – comma separated
  • groupingBy(Person::getName) – create map with key as name and value as list of Person objects. Conceptualize as buckets. Put items in bucket by key
  • groupingBy(Person::getName, mapping(Person::getAge, toList())) – map after group. Perform mapping right before throw data into bucket.
  • groupingBy(Person::getName, counting()) – value is # matching values
  • groupingBy(Person::getName, collectingAndThen(counting(), Long::intValue)) – transform the result of a collector to a different type

My take

I like that Venkat talked about how to write code “the long way” to explain the power of collectors. This was a good review. And good motivation as we update our OCP book (I have the streams chapter). I like the bucket analogy for groupingBy(). I didn’t know about collectingAndThen() The 45 minutes flew!

My Experience taking the new Java SE 11 Programmer II 1Z0-816 Exam

Back in March, I took the new Java SE 11 Programmer I 1Z0-815 Exam only 2 days after it was released. Going into the exam blind, I wasn’t too worried because the previous OCA 8 1Z0-808 exam had been such a breeze. Boy was I surprised! While I passed with a decent margin, I was shocked the level of difficulty of the Programmer I 1Z0-815 exam. It was nothing like the 1Z0-808 exam it inherits from, especially in terms of question difficulty!

This past month, Jeanne and I finished writing our new Java OCP 11 Programmer I Study Guide (now available for preorder), which meant it was time to turn our attention to our upcoming Java OCP 11 Programmer II Study Guide. Rather than go in blind, and especially given all of the new material, I decided to spend some time studying *before* taking the 1Z0-816 exam. Well, it paid off because I passed today with a quite a wide margin. Below are some of my impressions of the exam.

Level of Difficulty

This might sound crazy, and I’m sure I’m biased, but overall I found the 1Z0-816 OCP11 exam less difficult than the OCP 8 1Z0-809 exam it inherits from. Don’t get me wrong, it was a difficult exam, but I felt like there were so many topics and they were so broad, the exam rarely went into especially deep detail on some of them. For example, many of the questions regarding SQL injection had pretty clear answers. In most of the questions, I was able to eliminate completely “ridiculous” answers right away, getting the answer choices down to 2 (or 3 if it was pick 2, or 4 if it was pick 3, etc). In fact, some questions I didn’t even need to read the text to whittle down the answer choices. For example, if an answer choice is an invalid lambda expression, it clearly cannot be a valid answer. With that in mind, most questions boiled down eliminating bad answers, then reading the question text to know which of the two remaining choices was correct.

Better Focused

One of the best changes they made in the new 1Z0-815/1Z0-816 exam series was to move most of the core Java syntactical questions to the first exam. While they made the 1Z0-815 exam harder, it made the 1Z0-816 exam a lot clearer. For example, if a question appears to be about NIO.2 on the 1Z0-816 exam, then it’s about NIO.2! On the older 1Z0-809 exam, I always felt like they mixed common Java topics with advanced ones. For example, a question that appears to be about NIO.2 on the 1Z0-809 exam might actually be about constructor overloading or overriding methods. In other words, the 1Z0-816 exam is better because the questions are derived from the objectives more cleanly, and there aren’t as many trick questions. You still have to know a lot to pass, but at least they aren’t mixing topics as much as they did in previous exams.

Streams, Streams, Streams

While the exam seemed reasonable to me, I’m also very proficient in streams. It is an understatement to say they are all over the exam. If you don’t use them regularly, you’ll need a lot of practice before taking the exam. Remember, they can show up in almost any topic like NIO.2, Concurrency, Collections, etc.

Modules

Modules are on the exam but I found the questions a lot more straight-forward than the module questions I saw on the 1Z0-815 exam. I had a lot of trouble with the module questions on the 1Z0-815 exam, in part because a lot of them didn’t make sense or didn’t appear to have a correct answer. Given how early I took the exam, Jeanne suspects I might have been exposed to beta/experimental/broken questions. That said, I thought the module questions on the 1Z0-816 exam were a lot more fair than they were on the 1Z0-815 exam. You need to know a lot about modules, of course, but the topics the questions were testing were a lot clearer.

Still a Very Broad Exam

While questions within a topic were relatively straight-forward, the amount of topics you had to know for the 1Z0-816 exam dwarfs the 1Z0-809 exam. Annotations, Security, Local Type Inference, Private/Static Interface Methods, and Modules are completely new. You should read the Secure Coding Guideline and Annotations Trail prior to taking the exam. Unfortunately, there’s not one single source of material for modules so you have to study from what you can piece together on the web… that is until our new 1Z0-816 study guide is released!

So You Want to Take the Exam?

Great! If you’re not in a hurry, I would wait for our new study guides to come out. The first book is already on its way to print and the second book will be available early next year. You can use our OCP 8 Study Guide to take exam, but you will have to supplement it with a lot of reading from a dozen different sources. And as I said earlier, if you’re not using streams regularly, you will definitely need a lot of practice. Regardless of which path you take, we wish you the best in studying!

The 8 Nights of Java – Night 8

It’s been a fun 8 Nights of Java, but alas, tonight is the final post. Java 8 added one of the most radical, most drastic changes to date, even bigger than Java 5 or Java 7: lambdas and streams! The writers of Java updated nearly every Java API to include lambda expression and stream-based methods. More than the API changes, though, they present a new paradigm for thinking about programming. The closest thing to a lambda expression prior to Java 8 was an anonymous inner class, and it a lot of ways they are similar, but the syntax and usability of lambda expressions is unmatched.

We hope everyone had a wonderful holiday season and we hope that you have a Happy New Year! Don’t forget, we have a brand new OCA/OCP 8 book coming out in March 2017. Order yours now on Amazon.com!

Jump to: [Night 1 | Night 2 | Night 3 | Night 4 | Night 5 | Night 6 | Night 7 | Night 8]

Java 8 Notable Features

Oracle released Java 8 on March 18, 2014. If you’ve read the previous nights in this series, you might be wondering what the codename of Java 8 was. Oracle decided not to use one. Luckily, our friends at the Javaranch had some ideas. We like “Cuddles” ourselves.

Key new functionality included:

  • Functional interfaces
  • Lambda expressions
  • Streams
  • New date/time library
  • Project Nashorn

From Jeanne:

Streams and lambdas are one of my favorite additions to the language of all time. They make it easy to write expressive code. I haven’t needed the benefits of multi-core parallelization much yet, but it is nice to know it is there. Even without that, I find my stream based code to be shorter and easy to write/read. It took me a little while to get fluent but I went through that with Groovy before Java so I was ready when it came to Java. I was the first person on my team to write a Java 8 program for work. I commented almost every line so it could be used as a teaching example. That was fun!

The date/time library is also cool. I like that it is expressive and has so much more functionality than the Calendar API. Third time for a date API really was the charm. I just wish they retrofitted it to work directly with more libraries like JDBC. Then there’s Nashorn. A JavaScript console was a good idea, but I don’t think this one is it.

From Scott:

Two things I love about lambda expressions and streams in Java 8. First, they found a way to build functional programming into Java, by using functional interfaces. While the signature for some of the built-in functional interfaces can be… hard to read… using or creating lambda expressions rarely requires understanding the signature. That’s what is so cool about what they did. They added this conversion of lambda expressions to functional interfaces to anonymous inner classes, but from a developers perspective, all we have to know how to do is write a simple one-line lambda expression! Very cool they way they added this on without breaking the language.

The second thing I love is they included parallel streams in the very first version! It’s so awesome to be able to have built-in easy support for concurrency without having to create/manage/shutdown an executor service. One feature request for Java 9? I *really* would prefer a way to set the number of threads in a parallel stream, such as stream.parallel(10) for 10 threads. Currently, it’s controlled by the JVM based on the number of threads on the computer and unable to be changed by a developer. Here’s hoping they add it to a future version of Java!

There’s still so much more to love about Java 8! As I was saying on Night 7, Java 7 improved the Concurrency API while introducing NIO.2, but both became even more powerful in Java 8. Creating Runnable and Callable instances using anonymous inner classes was already common prior to Java 8, but it was at times verbose. You had to create a class definition, override a method, set up a generic return type for Callable, etc. While not difficult to do, it did take up multiple lines of bloated code, copied over and over again for each task. Lambda expressions fit perfectly in the Concurrency API, better than any other API (with the possible exception of Collections), in that it allowed developers to submit tasks to a thread executor in a single, simple line of code using a lambda expression. In other words, they made Concurrency, one of the arguably most difficult spaces for developers to work in, much easier to understand and implement.