Java SE 8/11 Programmer II Exam Series: Streams

Introduction In this post, we will cover the streams in Java 8. We will discover the three parts of the pipeline: source, terminal, and intermediate operations. What is a stream pipeline? Stream operations are divided into intermediate and terminal operations and are combined to form stream pipelines. A stream pipeline consists of a source; followed by zero or more intermediate […]

Java SE 8/11 Programmer II Exam Series: Built-in Functional Interfaces

Introduction Functional interfaces are the basics of functional programming in Java 8. If you master the six basic interfaces, you can derive the rest and understand the streams. In this section, we will cover the common functional interfaces in java.util.function package: Supplier, Consumer, Predicate, Function, UnaryOperator, and BinaryOperator. Class Hierarchy The diagram shows the class […]

Java SE 8/11 Programmer II Exam Series: Java Collections Framework

Introduction The collection interfaces are divided into two groups: the most basic interface java.util.Collection, and other collection interfaces based on java.util.Map. In this section, we will cover four main interfaces in the Java Collections Framework: List, Set, Map, and Queue. List You usually use a list when you want an ordered collection that can contain duplicate entries. All […]

Java SE 8/11 Programmer II Exam Series: Generics

Introduction Generics enable types (classes and interfaces) to be parameters when defining classes, interfaces, and methods. It enables you to re-use the same code. In this section, we will cover Generic Classes, Generic Interfaces, Generic Methods, and Bounded Type Parameters. Type Parameter Naming Conventions By convention, type parameter names are single, uppercase letters. The most […]

Java SE 8/11 Programmer II: Design Patterns and Principles

Introduction This section covers the OCP Java SE 8 Programmer II exam objectives: design an interface, define functional interfaces, implement polymorphism, create and use singleton classes and immutable classes. Designing an Interface Let’s revise the interface rules in Java 8. An interface may extend another interface, and in doing so it inherits all of the […]

Java SE 8/11 Programmer II: Advanced Class Design

Introduction In this post, we discover the basics of advanced class design for the Java SE 8 Programmer II Exam. We cover the instanceof operator; implement equals, hashCode, and toString; create enumerations and nested classes. Virtual methods Virtual functions are an important part of the run-time polymorphism in object-oriented programming. They are just non-static methods. […]

Item 45: Use streams judiciously

Introduction Streams in Java 8 support functional-style operations on streams of elements. This API makes it easier to perform bulk operations, sequentially or in parallel. When used appropriately, streams can make programs shorter and clearer. When used inappropriately, they can make programs difficult to read and maintain. When to use streams? Streams are a good […]

Item 43: Prefer method references to lambdas

Introduction Method references are more concise than lambdas. If method references are shorter and clearer, use them; otherwise, utilize lambdas. Method references reduce the boilerplate code and thus improves readability. There are four kinds of method references, which are summarized below: Kind Example Reference to a static method ContainingClass::staticMethodName Reference to an instance method of […]

Item 44: Favor the use of standard functional interfaces

If one of the standard functional interfaces does the job, you should generally use it in preference to a purpose-built functional interface This will make your API easier to learn, by reducing its conceptual surface area, and will provide significant interoperability benefits Always annotate your functional interfaces with the @FunctionalInterface annotation @FunctionalInterface annotation is used […]

Item 42: Prefer lambdas to anonymous classes

Anonymous classes were adequate for the classic objected-oriented design patterns requiring function objects. Here’s a code snippet using an anonymous class. // Anonymous class instance as a function object – obsolete! Collections.sort(words, new Comparator<String>() { public int compare(String s1, String s2) { return, s2.length()); } }); Interfaces with a single abstract method are now […]