banner Test 22

Test 22 - Free Java SE 25 certification Mock test - Exam Code 1Z0-831-Oracle certified Java SE 25 Professional

Collectors turn streams into useful results and they're probably the most difficult of the questions on the Java SE 25 Developer exam. This free 1Z0-831 mock test is all about collecting, reducing, grouping and partitioning data so you can look at a collector pipeline and work out what it will produce without actually running it.

You start off with the basic collectors: toList, toSet, toMap, joining, counting, summingInt, averagingDouble, minBy and maxBy. The questions are all about their rather small set of rules. toList returns an unmodifiable list, toList doesn't make any promise about mutability and toMap will throw an IllegalStateException if two elements have the same key unless you provide a merge function. You'll also see how a four arg toMap can specify the type of map you want to end up with (e.g. a TreeMap) and how joining will accept a delimiter, a prefix and a suffix.

Grouping is the key thing. Collectors.groupingBy will create a Map of the elements and you'll be looking at the contents of that when you combine it with various downstream collectors (counting, mapping, summingInt, averagingInt, filtering, flatMapping etc). There are tests of the three arg form which selects a TreeMap and of nested groupings which produce a map of maps. You'll see that the default result is a HashMap with no guarantee of ordering so well-written questions will always be predicated on the use of a sorted map or sorted set.

Partitioning comes next. collectors.partitioningBy will always return a map with exactly two keys (true and false) even if one of the groups is empty. That's a detail that frequently determines the answer when you're comparing it to the behaviour of groupingBy on a boolean predicate.

Reduction completes the topic. You'll be using reduce and the TeeingCollector which will take the two downstream results and combine them. You'll also be using summarisingInt for count, sum, min, average and max all in one pass. Collectors.collectingAndThen and the unmodifiable wrappers also appear.

Every explanation shows the intermediate map after each step of the classification. Be very aware of nulls as a number of the collectors will throw a NPE if they encounter a null key or null value. groupingBy will throw a NullPointerException if its classifier returns null and toMap will fail on a null value. Those NPEs will be in the answer choices and spotting them quickly will save you a lot of time on the timed exam when you've got a number of pipelines vying for your attention. Take the test, go through your mistakes and then re-build the more difficult pipelines on your own small data set. Collectors reward practice over memorisation and this test has plenty of opportunity to practice.

Welcome to your Free Java SE 25 – Test 22