Book Image

Java Coding Problems - Second Edition

By : Anghel Leonard
Book Image

Java Coding Problems - Second Edition

By: Anghel Leonard

Overview of this book

The super-fast evolution of the JDK between versions 12 and 21 has made the learning curve of modern Java steeper, and increased the time needed to learn it. This book will make your learning journey quicker and increase your willingness to try Java’s new features by explaining the correct practices and decisions related to complexity, performance, readability, and more. Java Coding Problems takes you through Java’s latest features but doesn’t always advocate the use of new solutions — instead, it focuses on revealing the trade-offs involved in deciding what the best solution is for a certain problem. There are more than two hundred brand new and carefully selected problems in this second edition, chosen to highlight and cover the core everyday challenges of a Java programmer. Apart from providing a comprehensive compendium of problem solutions based on real-world examples, this book will also give you the confidence to answer questions relating to matching particular streams and methods to various problems. By the end of this book you will have gained a strong understanding of Java’s new features and have the confidence to develop and choose the right solutions to your problems.
Table of Contents (16 chapters)
1
Text Blocks, Locales, Numbers, and Math
Free Chapter
2
Objects, Immutability, Switch Expressions, and Pattern Matching
14
Other Books You May Enjoy
15
Index

What this book covers

Chapter 1, Text Blocks, Locales, Numbers, and Math, includes 37 problems covering 4 main topics, text blocks, locales, numbers, and mathematical operations.

Chapter 2, Objects, Immutability, Switch Expressions, and Pattern Matching, includes 30 problems tackling, among other things, some less-known features of java.util.Objects, some interesting aspects of immutability, the newest features of switch expressions, and deep coverage of the pattern matching expressions; instanceof and switch.

Chapter 3, Working with Date and Time, includes 20 problems covering different date-time topics. These problems are mainly focused on the Calendar API and the JDK 8 Date/Time API. About the latter, we will cover some of the less-studied APIs like ChronoUnit, ChronoField, IsoFields, and TemporalAdjusters.

Chapter 4, Records and Record Patterns, includes 19 problems that cover in detail the Java records introduced in JDK 16 (JEP 395) and record patterns, which were introduced as a preview feature in JDK 19 (JEP 405), as a second preview feature in JDK 20 (JEP 432), and as a final feature in JDK 21 (JEP 440).

Chapter 5, Arrays, Collections, and Data Structures, includes 24 problems covering three main topics. We start with several problems meant to cover the new Vector API dedicated to data parallel processing. We continue with several data structures including Rope, Skip List, K-D Tree, Zipper, Binomial Heap, Fibonacci Heap, Pairing Heap, Huffman Coding, and so on. Finally, we discuss the three most popular join algorithms.

Chapter 6, Java I/O: Context-Specific Deserialization Filters, includes 13 problems related to Java serialization/deserialization processes. We start with classical problems like serializing/deserializing objects to byte[], String, and XML formats. We then continue with JDK 9 deserialization filters meant to prevent deserialization vulnerabilities, and we finish with JDK 17 context-specific deserialization filters.

Chapter 7, Foreign (Function) Memory API, includes 28 problems covering the Foreign Function Memory API and Foreign Linker API. We start with the classical approaches for calling foreign functions, relying on the JNI API and the open-source JNA/JNR libraries. Next, we introduce the new approach delivered under the code name Project Panama. We dissect the most relevant APIs such as Arena, MemorySegment, MemoryLayout, and so on. Finally, we focus on the Foreign Linker API and the Jextract tool for calling foreign functions that have different types of signatures, including callback functions.

Chapter 8, Sealed and Hidden Classes, includes 13 problems covering Sealed and Hidden Classes. The first 11 recipes will cover Sealed Classes, a very cool feature brought in by JDK 17 to sustain closed hierarchies. The last two problems cover Hidden Classes, a JDK 15 feature that allows frameworks to create and use runtime (dynamic) classes hidden to the JVM.

Chapter 9, Functional Style Programming – Extending APIs, includes 24 problems covering a wide range of functional programming topics. We will start by introducing the JDK 16 mapMulti() and continue with a handful of problems for working with predicates (Predicate), functions, and collectors.

Chapter 10, Concurrency – Virtual Threads and Structured Concurrency, includes 16 problems briefly introducing virtual threads and structured concurrency.

Chapter 11, Concurrency – Virtual Threads and Structured Concurrency: Diving Deeper, includes 18 problems meant to dive deep into how virtual threads and structured concurrency work and how they should be harnessed in your applications.

Chapter 12, Garbage Collectors and Dynamic CDS Archives, includes 15 problems covering Garbage Collectors and Application Class-Data Sharing (AppCDS).

Chapter 13, Socket API and Simple Web Server, includes 11 problems covering the Socket API and 8 problems covering JDK 18’s Simple Web Server. In the first 11 problems, we will discuss implementing socket-based applications such as blocking/non-blocking server/client applications, datagram-based applications, and multicast applications. In the second part of this chapter, we discuss Simple Web Server as a command-line tool.