Book Image

Java Coding Problems

By : Anghel Leonard
Book Image

Java Coding Problems

By: Anghel Leonard

Overview of this book

The super-fast evolution of the JDK between versions 8 and 12 has increased the learning curve of modern Java, therefore has increased the time needed for placing developers in the Plateau of Productivity. Its new features and concepts can be adopted to solve a variety of modern-day problems. This book enables you to adopt an objective approach to common problems by explaining the correct practices and decisions with respect to complexity, performance, readability, and more. Java Coding Problems will help you complete your daily tasks and meet deadlines. You can count on the 300+ applications containing 1,000+ examples in this book to cover the common and fundamental areas of interest: strings, numbers, arrays, collections, data structures, date and time, immutability, type inference, Optional, Java I/O, Java Reflection, functional programming, concurrency and the HTTP Client API. Put your skills on steroids with problems that have been carefully crafted to highlight and cover the core knowledge that is accessed in daily work. In other words (no matter if your task is easy, medium or complex) having this knowledge under your tool belt is a must, not an option. By the end of this book, you will have gained a strong understanding of Java concepts and have the confidence to develop and choose the right solutions to your problems.
Table of Contents (15 chapters)
Free Chapter
1
Strings, Numbers, and Math
2
Objects, Immutability, and Switch Expressions
3
Working with Date and Time
5
Arrays, Collections, and Data Structures
6
Java I/O Paths, Files, Buffers, Scanning, and Formatting
7
Java Reflection Classes, Interfaces, Constructors, Methods, and Fields
8
Functional Style Programming - Fundamentals and Design Patterns
9
Functional Style Programming - a Deep Dive
10
Concurrency - Thread Pools, Callables, and Synchronizers
11
Concurrency - Deep Dive
13
The HTTP Client and WebSocket APIs

To get the most out of this book

You should have fundamental knowledge about the Java language. You should install the following:

  • An IDE (recommended, but not a must, is Apache NetBeans 11.x: https://netbeans.apache.org/)
  • JDK 12 and Maven 3.3.x
  • Additional third-party libraries will need to be installed at the right moment (nothing special)

Download the example code files

You can download the example code files for this book from your account at www.packt.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

  1. Log in or register at www.packt.com.
  2. Select the Support tab.
  3. Click on Code Downloads.
  4. Enter the name of the book in the Search box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR/7-Zip for Windows
  • Zipeg/iZip/UnRarX for Mac
  • 7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Java-Coding-Problems. In case there's an update to the code, it will be updated on the existing GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Download the color images

Code in action

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "If the current character exists in the Map instance, then simply increase its occurrences by 1 with (character, occurrences+1)."

A block of code is set as follows:

public Map<Character, Integer> countDuplicateCharacters(String str) {

Map<Character, Integer> result = new HashMap<>();

// or use for(char ch: str.toCharArray()) { ... }
for (int i = 0; i<str.length(); i++) {
char ch = str.charAt(i);

result.compute(ch, (k, v) -> (v == null) ? 1 : ++v);
}

return result;
}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

for (int i = 0; i < str.length(); i++) {
int cp = str.codePointAt(i);
String ch = String.valueOf(Character.toChars(cp));
if(Character.charCount(cp) == 2) { // 2 means a surrogate pair
i++;
}
}

Any command-line input or output is written as follows:

$ mkdir css
$ cd css

Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "In Java, the logical AND operator is represented as &&, the logical OR operator is represented as ||, and the logical XOR operator is represented as ^."

Warnings or important notes appear like this.
Tips and tricks appear like this.