Java Coding Problems Become an Expert Java Programmer by Solving over 200 Brand-New, Modern, Real-world Problems
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...
Autor principal: | |
---|---|
Formato: | Libro electrónico |
Idioma: | Inglés |
Publicado: |
Birmingham :
Packt Publishing, Limited
2024.
|
Edición: | 2nd ed |
Colección: | Expert insight.
|
Materias: | |
Ver en Biblioteca Universitat Ramon Llull: | https://discovery.url.edu/permalink/34CSUC_URL/1im36ta/alma991009807524906719 |
Tabla de Contenidos:
- Cover
- Copyright
- Contributors
- Table of Contents
- Preface
- Chapter 1: 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
- Chapter 2: 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
- Chapter 3: 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
- Chapter 4: 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
- Chapter 5: 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.