OCP Exam Series VI – 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

Read More

OCP Exam Series V – 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

Read More

OCP Exam Series IV – 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

Read More

OCP Exam Series III – 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

Read More

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

Read More

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

Read More

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

Read More

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 Integer.compare(s1.length(), s2.length()); } }); Interfaces with a single abstract method are now

Read More