Java Coding Problems - Anghel Leonard - E-Book

Java Coding Problems E-Book

Anghel Leonard

0,0
39,59 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.
Mehr erfahren.
Beschreibung

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.

Jave 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.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 988

Veröffentlichungsjahr: 2024

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Java Coding Problems

Second Edition

Become an expert Java programmer by solving over 200 brand-new, modern, real-world problems

Anghel Leonard

BIRMINGHAM—MUMBAI

Java Coding Problems

Second Edition

Copyright © 2024 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

Senior Publishing Product Manager: Denim Pinto

Acquisition Editor – Peer Reviews: Gaurav Gavas

Project Editor: Namrata Katare

Senior Development Editor: Elliot Dallow

Copy Editor: Safis Editing

Technical Editor: Aniket Shetty

Proofreader: Safis Editing

Indexer: Rekha Nair

Presentation Designer: Ajay Patule

Developer Relations Marketing Executive: Vipanshu Parashar

First published: September 2019

Second edition: March 2024

Production reference: 1120324

Published by Packt Publishing Ltd.

Grosvenor House

11 St Paul’s Square

Birmingham

B3 1RB, UK.

ISBN 978-1-83763-394-4

www.packt.com

Contributors

About the author

Anghel Leonard is a chief technology strategist with more than 20 years of experience in the Java ecosystem. In his daily work, he is focused on architecting and developing Java distributed applications that empower robust architectures, clean code, and high performance. He is passionate about coaching, mentoring, and technical leadership.

He is the author of several books and videos and dozens of articles related to Java technologies.

About the reviewers

George Adams is a senior software engineer at Microsoft and the Java Champion and steering committee chair at Eclipse Adoptium. He was a co-founder of AdoptOpenJDK in 2016 and, since then, has led its community outreach efforts. He was instrumental in moving the project to the Eclipse Foundation. George also contributes to both the Homebrew project and the Node.js Foundation, where he is a core collaborator and plays an active role in several of the workgroups.

Ivar Grimstad is a Jakarta EE developer advocate at the Eclipse Foundation. He is a Java Champion and JUG Leader based in Sweden. He contributes to the Jakarta EE specifications and is the PMC Lead for Eclipse Enterprise for Java (EE4J). He is also a specification lead for Jakarta MVC and represents the Eclipse Foundation on the JCP Executive Committee. Ivar is also involved in a wide range of open-source projects and communities and is a frequent speaker at international developer conferences.

Join our community on Discord

Join our community’s Discord space for discussions with the author and other readers:

https://discord.gg/8mgytp5DGQ

Contents

Preface

Who this book is for

What this book covers

To get the most out of this book

Get in touch

Text Blocks, Locales, Numbers, and Math

Problems

1. Creating a multiline SQL, JSON, and HTML string

Before JDK 8

Starting with JDK 8

Introducing text blocks (JDK 13/15)

Hooking text blocks syntax

2. Exemplifying the usage of text block delimiters

3. Working with indentation in text blocks

Shifting the closing delimiter and/or the content

Using indentation methods

4. Removing incidental white spaces in text blocks

5. Using text blocks just for readability

6. Escaping quotes and line terminators in text blocks

7. Translating escape sequences programmatically

8. Formatting text blocks with variables/expressions

9. Adding comments in text blocks

10. Mixing ordinary string literals with text blocks

11. Mixing regular expression with text blocks

12. Checking if two text blocks are isomorphic

13. Concatenating strings versus StringBuilder

JDK 8

JDK 11

14. Converting int to String

15. Introducing string templates

What’s a string template?

The STR template processor

The FMT template processor

The RAW template processor

16. Writing a custom template processor

17. Creating a Locale

18. Customizing localized date-time formats

19. Restoring Always-Strict Floating-Point semantics

20. Computing mathematical absolute value for int/long and result overflow

21. Computing the quotient of the arguments and result overflow

22. Computing the largest/smallest value that is less/greater than or equal to the algebraic quotient

23. Getting integral and fractional parts from a double

24. Testing if a double number is an integer

25. Hooking Java (un)signed integers in a nutshell

26. Returning the flooring/ceiling modulus

27. Collecting all prime factors of a given number

28. Computing the square root of a number using the Babylonian method

29. Rounding a float number to specified decimals

30. Clamping a value between min and max

31. Multiply two integers without using loops, multiplication, bitwise, division, and operators

32. Using TAU

What is TAU?

33. Selecting a pseudo-random number generator

Choosing an algorithm by name

Choosing an algorithm by property

34. Filling a long array with pseudo-random numbers

35. Creating a stream of pseudo-random generators

36. Getting a legacy pseudo-random generator from new ones of JDK 17

37. Using pseudo-random generators in a thread-safe fashion (multithreaded environments)

Summary

Objects, Immutability, Switch Expressions, and Pattern Matching

Problems

38. Explain and exemplifying UTF-8, UTF-16, and UTF-32

Introducing ASCII encoding scheme (or single-byte encoding)

Introducing multi-byte encoding

Unicode

Java and Unicode

JDK 18 defaults the charset to UTF-8

39. Checking a sub-range in the range from 0 to length

40. Returning an identity string

41. Hooking unnamed classes and instance main methods

42. Adding code snippets in Java API documentation

Adding attributes

Using markup comments and regions

Highlighting

Linking

Modifying the code’s text

Using external snippets

Regions in external snippets

43. Invoking default methods from Proxy instances

JDK 8

JDK 9+, pre-JDK 16

JDK 16+

44. Converting between bytes and hex-encoded strings

JDK 17+

45. Exemplify the initialization-on-demand holder design pattern

Static vs. non-static blocks

Nested classes

Tackling the initialization-on-demand holder design pattern

JDK 16+

46. Adding nested classes in anonymous classes

JDK 16+

47. Exemplify erasure vs. overloading

Erasure in a nutshell

Erasure of generic types

Erasure and bridge methods

Type erasure and heap pollution

Polymorphic overloading in a nutshell

Erasure vs. overloading

48. Xlinting default constructors

49. Working with the receiver parameter

50. Implementing an immutable stack

51. Revealing a common mistake with Strings

52. Using the enhanced NullPointerException

WARNING 1! NPE when calling an instance method via a null object

WARNING 2! NPE when accessing (or modifying) the field of a null object

WARNING 3! NPE when null is passed in the method argument

WARNING 4! NPE when accessing the index value of a null array/collection

WARNING 5! NPE when accessing a field via a getter

53. Using yield in switch expressions

54. Tackling the case null clause in switch

55. Taking on the hard way to discover equals()

56. Hooking instanceof in a nutshell

57. Introducing pattern matching

The scope of binding variables in pattern matching

Guarded patterns

Type coverage

Current status of pattern matching

58. Introducing type pattern matching for instanceof

59. Handling the scope of a binding variable in type patterns for instanceof

60. Rewriting equals() via type patterns for instanceof

61. Tackling type patterns for instanceof and generics

62. Tackling type patterns for instanceof and streams

63. Introducing type pattern matching for switch

64. Adding guarded pattern labels in switch

65. Dealing with pattern label dominance in switch

66. Dealing with completeness (type coverage) in pattern labels for switch

67. Understanding the unconditional patterns and nulls in switch expressions

Summary

Working with Date and Time

Problems

68. Defining a day period

Before JDK 16

JDK 16+

69. Converting between Date and YearMonth

70. Converting between int and YearMonth

71. Converting week/year to Date

72. Checking for a leap year

73. Calculating the quarter of a given date

74. Getting the first and last day of a quarter

75. Extracting the months from a given quarter

76. Computing pregnancy due date

77. Implementing a stopwatch

78. Extracting the count of milliseconds since midnight

79. Splitting a date-time range into equal intervals

80. Explaining the difference between Clock.systemUTC() and Clock.systemDefaultZone()

81. Displaying the names of the days of the week

82. Getting the first and last day of the year

83. Getting the first and last day of the week

84. Calculating the middle of the month

85. Getting the number of quarters between two dates

86. Converting Calendar to LocalDateTime

87. Getting the number of weeks between two dates

Summary

Records and Record Patterns

Problems

88. Declaring a Java record

89. Introducing the canonical and compact constructors for records

Handling validation

Reassigning components

Defensive copies of the given components

90. Adding more artifacts in a record

91. Iterating what we cannot have in a record

A record cannot extend another class

A record cannot be extended

A record cannot be enriched with instance fields

A record cannot have private canonical constructors

A record cannot have setters

92. Defining multiple constructors in a record

93. Implementing interfaces in records

94. Understanding record serialization

How serialization/deserialization works

Serializing/deserializing gacContainer (a typical Java class)

Deserializing a malicious stream

Serializing/deserializing gacContainerR (a Java record)

Deserializing a malicious stream

Refactoring legacy serialization

95. Invoking the canonical constructor via reflection

96. Using records in streams

97. Introducing record patterns for instanceof

Nested records and record patterns

98. Introducing record patterns for switch

99. Tackling guarded record patterns

100. Using generic records in record patterns

Type argument inference

Type argument inference and nested records

101. Handling nulls in nested record patterns

102. Simplifying expressions via record patterns

103. Hooking unnamed patterns and variables

Unnamed patterns

Unnamed variables

In a catch block

In a for loop

In an assignment that ignores the result

In try-with-resources

In lambda expressions

104. Tackling records in Spring Boot

Using records in controllers

Using records with templates

Using records for configuration

Record and dependency injection

105. Tackling records in JPA

DTO via record constructor

DTO via record and JPA constructor expression

DTO via record and result transformer

DTO via record and JdbcTemplate

Team up Java records and @Embeddable

106. Tackling records in jOOQ

Summary

Arrays, Collections, and Data Structures

Problems

107. Introducing parallel computations with arrays

108. Covering the Vector API’s structure and terminology

The vector element type

The vector shape

The vector species

Vector lanes

Vector operations

Creating vectors

Creating vectors of zeros

Creating vectors of the same primitive value

Creating vectors from Java arrays

Creating vectors from memory segments

109. Summing two arrays via the Vector API

110. Summing two arrays unrolled via the Vector API

111. Benchmarking the Vector API

112. Applying the Vector API to compute FMA

113. Multiplying matrices via the Vector API

114. Hooking the image negative filter with the Vector API

115. Dissecting factory methods for collections

Factory methods for maps

Factory methods for lists

Factory methods for sets

116. Getting a list from a stream

117. Handling map capacity

118. Tackling Sequenced Collections

Applying the Sequenced Collections API to lists

Applying the Sequenced Collections API to ArrayList and LinkedList

Applying the Sequenced Collections API to sets

Applying the Sequenced Collections API to HashSet

Applying the Sequenced Collections API to LinkedHashSet

Applying the Sequenced Collections API to TreeSet

Applying the Sequenced Collections API to maps

Applying the Sequenced Collections API to LinkedHashMap

Applying the Sequenced Collections API to SortedMap (TreeMap)

119. Introducing the Rope data structure

Implementing indexAt(Node node, int index)

Implementing concat(Node node1, Node node2)

Implementing insert(Node node, int index, String str)

Implementing delete(Node node, int start, int end)

Implementing split(Node node, int index)

120. Introducing the Skip List data structure

Implementing contains(Integer data)

Implementing insert(Integer data)

Implementing delete(Integer data)

121. Introducing the K-D Tree data structure

Inserting into a K-D Tree

Finding the nearest neighbor

122. Introducing the Zipper data structure

123. Introducing the Binomial Heap data structure

Implementing insert(int key)

Implementing findMin()

Implementing extractMin()

Implementing decreaseKey(int key, int newKey)

Implementing delete(int key)

Implementing unionHeap(BinomialHeap heap)

124. Introducing the Fibonacci Heap data structure

125. Introducing the Pairing Heap data structure

126. Introducing the Huffman Coding data structure

Encoding the string

Decoding the string

127. Introducing the Splay Tree data structure

128. Introducing the Interval Tree data structure

Implementing insert(Interval interval)

129. Introducing the Unrolled Linked List data structure

130. Implementing join algorithms

Nested Loop Join

Hash Join

Sort Merge Join

Summary

Java I/O: Context-Specific Deserialization Filters

Problems

131. Serializing objects to byte arrays

132. Serializing objects to strings

133. Serializing objects to XML

134. Introducing JDK 9 deserialization filters

Pattern-based filters

Applying a pattern-based filter per application

Applying a pattern-based filter to all applications in a process

ObjectInputFilter-based filters

135. Implementing a custom pattern-based ObjectInputFilter

136. Implementing a custom class ObjectInputFilter

137. Implementing a custom method ObjectInputFilter

138. Implementing a custom lambda ObjectInputFilter

139. Avoiding StackOverflowError at deserialization

140. Avoiding DoS attacks at deserialization

141. Introducing JDK 17 easy filter creation

142. Tackling context-specific deserialization filters

Applying a Filter Factory per application

Applying a Filter Factory to all applications in a process

Applying a Filter Factory via ObjectInputFilter.Config

Implementing a Filter Factory

143. Monitoring deserialization via JFR

Summary

Foreign (Function) Memory API

Problems

144. Introducing Java Native Interface (JNI)

Generating the header (.h) file

Implementing the modern_challenge_Main.cpp

Compiling the C source code

Generating the native shared library

Finally, run the code

145. Introducing Java Native Access (JNA)

Implementing the .cpp and .h files

Compiling the C source code

Generating the native shared library

Finally, run the code

146. Introducing Java Native Runtime (JNR)

147. Motivating and introducing Project Panama

148. Introducing Panama’s architecture and terminology

149. Introducing Arena and MemorySegment

Introducing memory layouts (ValueLayout)

Allocating memory segments of value layouts

Setting/getting the content of a memory segment

Working with Java strings

150. Allocating arrays into memory segments

151. Understanding addresses (pointers)

152. Introducing the sequence layout

Introducing PathElement

Introducing VarHandle

Putting PathElement and VarHandle together

Working with nested sequence layouts

153. Shaping C-like structs into memory segments

Introducing StructLayout

154. Shaping C-like unions into memory segments

Introducing UnionLayout

155. Introducing PaddingLayout

Hooking size, alignment, stride, and padding

Hooking size

Hooking alignment

Hooking stride

Hooking padding

Adding implicit extra space (implicit padding) to validate alignment

Adding explicit extra space (explicit padding) to validate alignment

156. Copying and slicing memory segments

Copying a segment

Copying a part of the segment into another segment (1)

Copying a segment into an on-heap array

Copying an on-heap array into a segment

Copying a part of the segment into another segment (2)

Slicing a segment

Using asOverlappingSlice()

Using segmentOffset()

157. Tackling the slicing allocator

158. Introducing the slice handle

159. Introducing layout flattening

160. Introducing layout reshaping

161. Introducing the layout spreader

162. Introducing the memory segment view VarHandle

163. Streaming memory segments

164. Tackling mapped memory segments

165. Introducing the Foreign Linker API

166. Calling the sumTwoInt() foreign function

167. Calling the modf() foreign function

168. Calling the strcat() foreign function

169. Calling the bsearch() foreign function

170. Introducing Jextract

171. Generating native binding for modf()

Summary

Sealed and Hidden Classes

Problems

172. Creating an electrical panel (hierarchy of classes)

173. Closing the electrical panel before JDK 17

Applying the final modifier

Defining package-private constructors

Declaring classes/interfaces as non-public

Throwing everything in a module

Conclusion

174. Introducing JDK 17 sealed classes

175. Introducing the permits clause

Working with sealed classes in separate sources (same package)

Working with sealed classes in separate packages

176. Closing the electrical panel after JDK 17

177. Combining sealed classes and records

178. Hooking sealed classes and instanceof

179. Hooking sealed classes in switch

180. Reinterpreting the Visitor pattern via sealed classes and type pattern matching for switch

181. Getting info about sealed classes (using reflection)

182. Listing the top three benefits of sealed classes

183. Briefly introducing hidden classes

184. Creating a hidden class

Summary

Functional Style Programming – Extending APIs

Problems

185. Working with mapMulti()

186. Streaming custom code to map

187. Exemplifying a method reference vs. a lamda

Scenario 1: Calling printReset()

Scenario 2: Calling static printNoReset()

Conclusion

188. Hooking lambda laziness via Supplier/Consumer

189. Refactoring code to add lambda laziness

Fixing in imperative fashion

Fixing in functional fashion

190. Writing a Function<String, T> for parsing data

191. Composing predicates in a Stream’s filters

192. Filtering nested collections with Streams

193. Using BiPredicate

194. Building a dynamic predicate for a custom model

195. Building a dynamic predicate from a custom map of conditions

196. Logging in predicates

197. Extending Stream with containsAll() and containsAny()

Exposing containsAll/Any() via a custom interface

Exposing containsAll/Any() via an extension of Stream

198. Extending Stream with removeAll() and retainAll()

Exposing removeAll()/retainAll() via a custom interface

Exposing removeAll/retainAll() via an extension of Stream

199. Introducing stream comparators

Sorting via natural order

Reversing the natural order

Sorting and nulls

Writing custom comparators

200. Sorting a map

201. Filtering a map

202. Creating a custom collector via Collector.of()

Writing a custom collector that collects into a TreeSet

Writing a custom collector that collects into a LinkedHashSet

Writing a custom collector that excludes elements of another collector

Writing a custom collector that collects elements by type

Writing a custom collector for SplayTree

203. Throwing checked exceptions from lambdas

204. Implementing distinctBy() for the Stream API

205. Writing a custom collector that takes/skips a given number of elements

206. Implementing a Function that takes five (or any other arbitrary number of) arguments

207. Implementing a Consumer that takes five (or any other arbitrary number of) arguments

208. Partially applying a Function

Summary

Concurrency – Virtual Threads and Structured Concurrency

Problems

209. Explaining concurrency vs. parallelism

210. Introducing structured concurrency

211. Introducing virtual threads

What’s the problem with platform (OS) threads?

What are virtual threads?

Creating a virtual thread

How many virtual threads we can start

Backward compatibility

Avoiding fake conclusions (potentially myths)

212. Using the ExecutorService for virtual threads

213. Explaining how virtual threads work

Capturing virtual threads

Pinning virtual threads

214. Hooking virtual threads and sync code

215. Exemplifying thread context switching

Example 1

Example 2

Example 3

216. Introducing the ExecutorService invoke all/any for virtual threads – part 1

Working with invokeAll()

Working with invokeAny()

217. Introducing the ExecutorService invoke all/any for virtual threads – part 2

218. Hooking task state

219. Combining newVirtualThreadPerTaskExecutor() and streams

220. Introducing a scope object (StructuredTaskScope)

ExecutorService vs. StructuredTaskScope

221. Introducing ShutdownOnSuccess

222. Introducing ShutdownOnFailure

223. Combining StructuredTaskScope and streams

224. Observing and monitoring virtual threads

Using JFR

Using Java Management Extensions (JMX)

Running 10,000 tasks via the cached thread pool executor

Running 10,000 tasks via the fixed thread pool executor

Running 10,000 tasks via the virtual thread per task executor

Summary

Concurrency ‒ Virtual Threads and Structured Concurrency: Diving Deeper

Problems

225. Tackling continuations

Introducing continuations

Continuations and virtual threads

226. Tracing virtual thread states and transitions

NEW

STARTED

RUNNING

PARKING

PARKED/PINNED

YIELDING

RUNNABLE

TERMINATED

227. Extending StructuredTaskScope

228. Assembling StructuredTaskScope

229. Assembling StructuredTaskScope instances with timeout

230. Hooking ThreadLocal and virtual threads

231. Hooking ScopedValue and virtual threads

Thread-local variables’ shortcomings

Introducing scoped values

232. Using ScopedValue and executor services

233. Chaining and rebinding scoped values

Changing scoped values

Rebinding scoped values

234. Using ScopedValue and StructuredTaskScope

235. Using Semaphore instead of Executor

236. Avoiding pinning via locking

237. Solving the producer-consumer problem via virtual threads

238. Solving the producer-consumer problem via virtual threads (fixed via Semaphore)

239. Solving the producer-consumer problem via virtual threads (increase/decrease consumers)

240. Implementing an HTTP web server on top of virtual threads

241. Hooking CompletableFuture and virtual threads

242. Signaling virtual threads via wait() and notify()

Summary

Garbage Collectors and Dynamic CDS Archives

Problems

243. Hooking the garbage collector goal

244. Handling the garbage collector stages

245. Covering some garbage collector terminology

Epoch

Single and multiple passes

Serial and parallel

Stop-the-World (STW) and concurrent

Live set

Allocation rate

NUMA

Region-based

Generational garbage collection

246. Tracing the generational GC process

247. Choosing the correct garbage collector

248. Categorizing garbage collectors

Serial garbage collector

Parallel garbage collector

Garbage-First (G1) collector

Z Garbage Collector (ZGC)

Shenandoah Garbage Collector

Concurrent Mark Sweep (CMS) collector (deprecated)

249. Introducing G1

Design principles

250. Tackling G1 throughput improvements

Delaying the start of the Old generation

Focusing on easy pickings

Improving NUMA-aware memory allocation

Parallelized full-heap collections

Other improvements

251. Tackling G1 latency improvements

Merge parallel phases into a larger one

Reduction of metadata

Better work balancing

Better parallelization

Better reference scanning

Other improvements

252. Tackling G1 footprint improvements

Maintain only the needed metadata

Release memory

253. Introducing ZGC

ZGC is concurrent

ZGC and colored pointers

ZGC and load barriers

ZGC is region-based

254. Monitoring garbage collectors

255. Logging garbage collectors

256. Tuning garbage collectors

How to tune

Tuning the serial garbage collector

Tunning the parallel garbage collector

Tuning the G1 garbage collector

Tuning Z Garbage Collector

Tuning Metaspace (Metadata space)

257. Introducing Application Class Data Sharing (AppCDS, or Java’s Startup Booster)

Tackling a JDK class data archive

JDK 10/JDK 11

JDK 12+

Tackling application class data archive

Before JDK 13

JDK 13+

JDK 19+

Summary

Socket API and Simple Web Server

Problems

258. Introducing socket basics

259. Introducing TCP server/client applications

Blocking vs. non-blocking mechanisms

260. Introducing the Java Socket API

Introducing NetworkChannel

Tackling socket options

261. Writing a blocking TCP server/client application

Writing a single-thread blocking TCP echo server

Creating a new server socket channel

Configuring the blocking mechanism

Setting server socket channel options

Binding the server socket channel

Accepting connections

Transmitting data over a connection

Closing the channel

Putting it all together into the echo server

Writing a single-thread blocking TCP client

Creating a new (client) socket channel

Configuring the blocking mechanism

Setting client socket channel options

Connecting the client socket channel

Transmitting data over a connection

Closing the channel

Putting it all together into the client

Testing the blocking echo application

262. Writing a non-blocking TCP server/client application

Using the SelectionKey class

Using the Selector methods

Writing the non-blocking server

Writing the non-blocking client

Testing the non-blocking echo application

263. Writing UDP server/client applications

Writing a single-thread blocking UDP echo server

Creating a server datagram-oriented socket channel

Setting datagram-oriented socket channel options

Binding the server datagram-oriented socket channel

Transmitting data packets

Closing the channel

Putting it all together into the client

Writing a connectionless UDP client

Testing the UDP connectionless echo application

Writing a connected UDP client

264. Introducing multicasting

A brief overview of MembershipKey

265. Exploring network interfaces

266. Writing a UDP multicast server/client application

Writing a UDP multicast server

Writing a UDP multicast client

Blocking/unblocking datagrams

Testing the multicasting server/client application

267. Adding KEM to a TCP server/client application

Generating a public-private keypair by the receiver

Transmitting the public key to the sender

Generating the common secret key by the sender

Sending the encapsulation message to the receiver

Using the secret key to encrypt/decrypt messages

268. Reimplementing the legacy Socket API

269. Quick overview of SWS

Key abstractions of SWS

270. Exploring the SWS command-line tool

Starting SWS from the command line

Configuring SWS from the command line

Stopping SWS from the command line

271. Introducing the com.sun.net.httpserver API

Using a custom HttpHandler

Using a custom filter

Using a custom executor

272. Adapting request/exchange

273. Complementing a conditional HttpHandler with another handler

274. Implementing SWS for an in-memory file system

275. Implementing SWS for a zip file system

276. Implementing SWS for a Java runtime directory

Summary

Other Books You May Enjoy

Index

Landmarks

Cover

Index

Preface

The super-fast evolution of the JDK between versions 12 and 21 means two things: first, Java now has a host of powerful new features that you can adopt to solve a variety of modern-day problems; second, the learning curve of modern Java is becoming very steep.

This book enables you to take an objective approach to solving common problems by explaining the correct practices and decisions you need to make with respect to complexity, performance, readability, and more.

Java Coding Problems, Second Edition, will help you complete your daily tasks and meet deadlines, all while becoming a more proficient and self-sufficient Java developer. You can count on the 270+ (all brand-new for this edition) problems in this book to cover the most common and fundamental areas of interest: strings, numbers, arrays, collections, the Foreign Function and Memory API, data structures, date and time, pattern matching, sealed/hidden classes, functional programming, virtual threads, Structured Concurrency, Garbage Collectors, Dynamic CDS Archives, the Socket API, and Simple Web Server.

Put your skills on steroids with problems that have been carefully crafted to highlight and teach the core knowledge needed for daily work. In other words (no matter if your task is simple, middling, or complex), having this knowledge in 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 you’ll have the confidence to develop and choose the right solutions to all your Java problems.

While this book is entirely stand-alone, and you do not need anything else to get the most out of it, many of the topics covered in this book are also explored in Java Coding Problems, First Edition. If you haven’t already read it, and you wish to get even more practice in, then consider picking that book up for a completely different set of Java problems.

Who this book is for

Java Coding Problems, Second Edition, is especially useful for late-beginner-to-intermediate Java developers looking to level-up their knowledge by solving real-world problems. However, the problems looked at within these pages will be encountered in the daily work of any Java developer, from beginner all the way to advanced practitioner.

Therefore, it is recommended that you are familiar with the Java fundamentals and have at least a foundational working knowledge of the language.

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.

To get the most out of this book

You should have a fundamental understanding of the Java language. You should also install the following:

An IDE (the recommended, but not essential, choice is Apache NetBeans 20.x: https://netbeans.apache.org/front/main/).JDK 21 and the Maven latest release.Additional third-party libraries will need to be installed to fully follow along with some problems and chapters (nothing too difficult or special).

Download the example code files

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Java-Coding-Problems-Second-Edition. 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

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://packt.link/gbp/9781837633944.

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: “For instance, let’s add the Patient and Appointment records as well.”

A block of code is set as follows:

if (staff instanceof Resident(String name, Doctor dr)) { return"Cabinet of " + dr.specialty() + ". Doctor: " + dr.name() + ", Resident: " + name; }

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

if (staff instanceof Resident(String name, Doctor dr)) { return"Cabinet of " + dr.specialty() + ". Doctor: " + dr.name() + ", Resident: " + name; }

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

2023-02-07T05:26:17.374159500Z 2023-02-07T05:26:17.384811300Z 2023-02-07T05:26:17.384811300Z

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: “The compiler recognizes a Java record via the record keyword.”

Warnings or important notes appear like this.

Tips and tricks appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: Email feedback@packtpub.com, and mention the book’s title in the subject of your message. If you have questions about any aspect of this book, please email us at questions@packtpub.com.

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book we would be grateful if you would report this to us. Please visit, http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at copyright@packtpub.com with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit http://authors.packtpub.com.

Share your thoughts

Once you’ve read Java Coding Problems, Second Edition, we’d love to hear your thoughts! Please click here to go straight to the Amazon review page for this book and share your feedback.

Your review is important to us and the tech community and will help us make sure we’re delivering excellent quality content.

Download a free PDF copy of this book

Thanks for purchasing this book!

Do you like to read on the go but are unable to carry your print books everywhere?

Is your eBook purchase not compatible with the device of your choice?

Don’t worry, now with every Packt book you get a DRM-free PDF version of that book at no cost.

Read anywhere, any place, on any device. Search, copy, and paste code from your favorite technical books directly into your application. 

The perks don’t stop there, you can get exclusive access to discounts, newsletters, and great free content in your inbox daily

Follow these simple steps to get the benefits:

Scan the QR code or visit the link below

https://packt.link/free-ebook/9781837633944

Submit your proof of purchaseThat’s it! We’ll send your free PDF and other benefits to your email directly


Tausende von E-Books und Hörbücher

Ihre Zahl wächst ständig und Sie haben eine Fixpreisgarantie.