Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Java Coding Problems

You're reading from   Java Coding Problems Improve your Java Programming skills by solving real-world coding challenges

Arrow left icon
Product type Paperback
Published in Sep 2019
Publisher Packt
ISBN-13 9781789801415
Length 816 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Anghel Leonard Anghel Leonard
Author Profile Icon Anghel Leonard
Anghel Leonard
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Preface 1. Strings, Numbers, and Math 2. Objects, Immutability, and Switch Expressions FREE CHAPTER 3. Working with Date and Time 4. Type Inference 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 12. Optional 13. The HTTP Client and WebSocket APIs 14. Other Books You May Enjoy

What this book covers

Chapter 1, Strings, Numbers, and Math, includes 39 problems that involve strings, numbers, and mathematical operations. The chapter starts with a bunch of classical problems for strings such as counting duplicates, reversing a string, and removing white spaces. The chapter continues with problems dedicated to numbers and mathematical operations such as summing two large numbers, operation overflow, comparing two unsigned numbers, computing the floor of a division and a modulus, and much more. Each problem is passed through several solutions, including Java 8 functional style. Moreover, the chapter covers problems that futures added in JDK 9, 10, 11, and 12.

Chapter 2, Objects, Immutability, and Switch Expressions, includes 18 problems that involve objects, immutability, and switch expressions. The chapter starts with several problems about dealing with null references. It continues with problems regarding checking indexes, equals() and hashCode(), and immutability (for example, writing immutable classes and passing/returning mutable objects from immutable classes). The last part of the chapter deals with cloning objects and JDK 12 switch expressions.

Chapter 3, Working with Date and Time, includes 20 problems that involve date and time. These problems are meant to cover a wide range of topics (converting, formatting, adding, subtracting, defining periods/durations, computing, and so on) via Date, Calendar, LocalDate, LocalTime, LocalDateTime, ZoneDateTime, OffsetDateTime, OffsetTime, Instant, and so on. By the end of this chapter, you will have no problems shaping date and time to conform to your application's needs.

Chapter 4, Type Inference, includes 21 problems that involve JEP 286, Java Local Variable Type Inference (LVTI), or the var type. These problems have been carefully crafted to reveal the best practices and common mistakes involved in using var. By the end of this chapter, you will have everything you need to know about var to push it in production.

Chapter 5, Arrays, Collections, and Data Structures, includes 30 problems that involve arrays, collections, and several data structures. The aim is to provide solutions to a category of problems encountered in a wide range of applications, such as sorting, finding, comparing, ordering, reversing, filling, merging, copying, replacing, and so on. The provided solutions are implemented in Java 8-12 and they can be used as the base for solving other related problems as well. By the end of this chapter, you will have a solid base of knowledge that's useful for solving a lot of problems that involve arrays, collections, and data structures.

Chapter 6, Java I/O Paths, Files, Buffers, Scanning, and Formatting, includes 20 problems that involve Java I/O for files. From manipulating, walking, and watching paths to streaming files and efficient ways for reading/writing text and binary files, we will cover problems that are a must in the arsenal of any Java developer. With the skills gained from this chapter, you will be able to tackle most of the common problems that involve Java I/O files.

Chapter 7, Java Reflection Classes, Interfaces, Constructors, Methods, and Fields, includes 17 problems that involve the Java Reflection API. From classical topics, such as inspecting and instantiating Java artifacts (for example, modules, packages, classes, interfaces, super-classes, constructors, methods, annotations, arrays, and so on), to synthetic and bridge constructs or nest-based access control (JDK 11), this chapter provides solid coverage of the Java Reflection API.

Chapter 8, Functional Style Programming – Fundamentals and Design Patterns, includes 11 problems that involve Java functional programming. The chapter starts with a problem designed to acquaint you completely with functional interfaces. It continues with a suite of design patterns from GoF interpreted in Java functional style.

Chapter 9, Functional Style Programming – Deep Dive, includes 22 problems that involve Java functional programming. Here, we focus on several problems that involve classical operations encountered in streams (for example, filters, and maps), and we discuss infinite streams, null-safe streams, and default methods. A comprehensive list of problems covers grouping, partitioning, and collectors, including the JDK 12 teeing() collector and the matter of writing a custom collector. In addition, takeWhile(), dropWhile(), composing functions, predicates and comparators, testing and debugging lambdas, and other cool topics are discussed as well.

Chapter 10, Concurrency – Thread Pools, Callables, and Synchronizers, includes 14 problems that involve Java concurrency. This chapter starts with several fundamental problems about the thread life cycle and object-/class-level locking. It continues with a bunch of problems about thread pools in Java, including JDK 8 work-stealing thread pools. Afterward, we have problems dedicated to Callable and Future. Next, we dedicate several problems to Java synchronizers (for example, barrier, semaphore, and exchanger). By the end of this chapter, you should be familiar with the main coordinates of Java concurrency and be ready to continue with a set of advanced problems.

Chapter 11, Concurrency – Deep Dive, includes 13 problems that involve Java concurrency. This chapter covers problems about fork/join frameworks, CompletableFuture, ReentrantLock, ReentrantReadWriteLock, StampedLock, atomic variables, task cancelation, interruptible methods, thread-local locks, and deadlocks. Completing this chapter will guarantee the achievement of the considerable amount of concurrency knowledge needed by any Java developer.

Chapter 12, Optional, includes 24 problems meant to draw several rules for working with Optional. The problems and solutions presented in this section are based on the Brian Goetz' (Java's language architect) definition—Optional is intended to provide a limited mechanism for library method return types where there needed to be a clear way to represent no result, and using null for such was overwhelmingly likely to cause errors. But where there are rules, there are exceptions as well. Therefore, do not conclude that the rules (or practices) presented here should be followed (or avoided) at all costs. Like always, the solution depends on the problem.

Chapter 13, HTTP Client and WebSocket APIs, includes 20 problems meant to cover the HTTP Client and WebSocket APIs. Remember HttpUrlConnection? Well, JDK 11 comes with the HTTP Client API as a reinvention of HttpUrlConnection. The HTTP Client API is easy to use and supports HTTP/2 (default) and HTTP/1.1. For backward compatibility, the HTTP Client API will automatically downgrade from HTTP/2 to HTTP 1.1 when the server doesn't support HTTP/2. Moreover, the HTTP Client API supports synchronous and asynchronous programming models and relies on streams to transfer data (reactive streams). It also supports the WebSocket protocol used in real-time web applications to provide client-server communication with low message overhead.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime