Oracle Certified Professional Java SE 8 Programmer
1 Java Class Design
1-1 Implement encapsulation
1-2 Implement inheritance including visibility modifiers and composition
1-3 Implement polymorphism
1-4 Override hashCode, equals, and toString methods from Object class
1-5 Create and use singleton classes and immutable classes
1-6 Develop code that uses static keyword on initializers, variables, methods, and classes
2 Advanced Java Class Design
2-1 Develop code that uses abstract classes and methods
2-2 Develop code that uses the final keyword
2-3 Create inner classes including static inner class, local class, nested class, and anonymous inner class
2-4 Use enumerated types including methods, and constructors in an enum type
2-5 Develop code that declares, implements andor extends interfaces and use the atOverride annotation
2-6 Create and use Lambda expressions
3 Generics and Collections
3-1 Create and use a generic class
3-2 Create and use ArrayList, TreeSet, TreeMap, and ArrayDeque objects
3-3 Use java util Comparator and java lang Comparable interfaces
3-4 Collections Streams and Filters
3-5 Iterate using forEach methods of Streams and List
3-6 Describe Stream interface and Stream pipeline
3-7 Use method references with Streams
4 Lambda Built-in Functional Interfaces
4-1 Use the built-in interfaces included in the java util function package such as Predicate, Consumer, Function, and Supplier
4-2 Develop code that uses primitive versions of functional interfaces
4-3 Develop code that uses binary versions of functional interfaces
4-4 Develop code that uses the UnaryOperator interface
5 Java Stream API
5-1 Develop code to extract data from an object using map, peek, and flatMap methods
5-2 Search for data by using search methods of the Stream classes including findFirst, findAny, anyMatch, allMatch, noneMatch
5-3 Develop code that uses the Optional class
5-4 Develop code that uses Stream data methods and calculation methods
5-5 Sort a collection using Stream API
5-6 Save results to a collection using the collect method and grouppartition data using the Collectors class
5-7 Use flatMap() methods in the Stream API
6 Exceptions and Assertions
6-1 Use try-catch and throw statements
6-2 Use catch, multi-catch, and finally clauses
6-3 Use Autoclose resources with a try-with-resources statement
6-4 Create custom exceptions and Auto-closeable resources
6-5 Test invariants by using assertions
7 Use Java SE 8 DateTime API
7-1 Create and manage date-based and time-based events including a combination of date and time into a single object using LocalDate, LocalTime, LocalDateTime, Instant, Period, and Duration
7-2 Work with dates and times across time zones and manage changes resulting from daylight savings including Format date and times values
7-3 Define and create and manage date-based and time-based events using Instant, Period, Duration, and TemporalUnit
8 Java File IO (NIO 2)
8-1 Operate on file and directory paths using the Paths class
8-2 Check, delete, copy, and move files and directories using the Files class
8-3 Recursively access a directory tree using the DirectoryStream and FileVisitor interfaces
8-4 Find a file by using the PathMatcher interface, and use Java SE 8 IO improvements, including Files find(), Files walk(), and lines() methods
8-5 Observe the changes in a directory by using WatchService
9 Java Concurrency
9-1 Create worker threads using Runnable, Callable and use an ExecutorService to concurrently execute tasks
9-2 Identify potential threading problems among deadlock, starvation, livelock, and race conditions
9-3 Use synchronized keyword and java util concurrent atomic package to control the order of thread execution
9-4 Use java util concurrent collections and classes including CyclicBarrier and CopyOnWriteArrayList
9-5 Use parallel ForkJoin Framework
9-6 Use parallel Streams including reduction, decomposition, merging processes, pipelines, and performance
10 Building Database Applications with JDBC
10-1 Describe the interfaces that make up the core of the JDBC API including the Driver, Connection, Statement, and ResultSet interfaces and their relationship to provider implementations
10-2 Identify the components required to connect to a database using the DriverManager class including the JDBC URL
10-3 Submit queries and read results from the database including creating statements, returning result sets, iterating through the results, and properly closing result sets, statements, and connections
10-4 Use PreparedStatement to perform CRUD operations
10-5 Use CallableStatement to call stored procedures
10-6 Use Transactions including disabling auto-commit mode, committing and rolling back transactions
10-7 Use JDBC batch operations
10-8 Create and use RowSet objects using RowSetProvider and RowSetFactory
11 Localization
11-1 Read and set the locale by using the Locale object
11-2 Create and manage date- and time-based events by using Localization including formatting dates, numbers, and currency values
11-3 Work with dates, numbers, and currency values by using the NumberFormat and DateFormat classes and their derived classes such as DecimalFormat and SimpleDateFormat
11-4 Build a user interface for a localized application
11-5 Describe the advantages of localizing an application
Saving Results to a Collection and Grouping/Partitioning Data using the Collectors Class

Saving Results to a Collection and Grouping/Partitioning Data using the Collectors Class

The Java Stream API provides powerful methods to process and collect data. The collect method, combined with the Collectors class, allows you to save results to a collection and perform complex operations like grouping and partitioning data.

Key Concepts

1. collect Method

The collect method is a terminal operation that accumulates elements of a stream into a collection. It takes a Collector as an argument, which specifies how the elements should be collected.

2. Collectors Class

The Collectors class provides a set of static methods to create common collectors. These collectors can be used to perform operations like collecting elements into a list, set, or map, as well as grouping and partitioning data.

3. Grouping Data

Grouping data involves organizing elements of a stream into groups based on a classification function. The Collectors.groupingBy method is used to group elements into a map where the keys are the group identifiers and the values are lists of elements belonging to each group.

4. Partitioning Data

Partitioning data involves splitting elements of a stream into two groups based on a predicate. The Collectors.partitioningBy method is used to partition elements into a map where the keys are true and false, and the values are lists of elements that satisfy or do not satisfy the predicate, respectively.

Detailed Explanation

collect Method

Example:

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class CollectExample {
    public static void main(String[] args) {
        List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
        List<String> filteredNames = names.stream()
                                            .filter(name -> name.length() > 3)
                                            .collect(Collectors.toList());
        System.out.println(filteredNames); // Output: [Alice, Charlie]
    }
}
    

Collectors Class

Example:

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class CollectorsExample {
    public static void main(String[] args) {
        List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "Alice");
        Set<String> uniqueNames = names.stream()
                                        .collect(Collectors.toSet());
        System.out.println(uniqueNames); // Output: [Bob, Alice, Charlie]
    }
}
    

Grouping Data

Example:

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class GroupingExample {
    public static void main(String[] args) {
        List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David", "Eve");
        Map<Integer, List<String>> nameLengthGroups = names.stream()
                                                            .collect(Collectors.groupingBy(String::length));
        System.out.println(nameLengthGroups); // Output: {3=[Bob], 4=[Eve], 5=[Alice], 7=[Charlie, David]}
    }
}
    

Partitioning Data

Example:

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class PartitioningExample {
    public static void main(String[] args) {
        List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David", "Eve");
        Map<Boolean, List<String>> nameLengthPartition = names.stream()
                                                                .collect(Collectors.partitioningBy(name -> name.length() > 4));
        System.out.println(nameLengthPartition); // Output: {false=[Bob, Eve], true=[Alice, Charlie, David]}
    }
}
    

Examples and Analogies

collect Method: Packaging Items

Think of the collect method as a packaging machine that takes individual items (elements) and packages them into a box (collection). The type of box and how the items are arranged within it are specified by the collector.

Collectors Class: Custom Packaging

Consider the Collectors class as a set of tools that help you create custom packaging solutions. For example, you can choose to package items in a box with dividers (list), a box with compartments (set), or a box with labeled sections (map).

Grouping Data: Sorting Items

Visualize grouping data as sorting items into different bins based on their characteristics. For example, sorting fruits into bins labeled by color. Each bin contains fruits that share the same characteristic.

Partitioning Data: Sorting Items into Two Bins

Think of partitioning data as sorting items into two bins based on a simple rule. For example, sorting fruits into two bins: one for ripe fruits and one for unripe fruits. Each bin contains fruits that satisfy or do not satisfy the rule.

Conclusion

The collect method and the Collectors class are essential tools in the Java Stream API for saving results to a collection and performing complex operations like grouping and partitioning data. By understanding and effectively using these methods, you can write more concise and expressive code to process and organize data. This knowledge is crucial for mastering Java programming and passing the Oracle Certified Professional Java SE 8 Programmer exam.