Java pattern memo

Since it is a personal memo, it may be added appropriately or left unattended.

List.of rather than Arrays.asList

Starting with JDK9, it's better to use List.of than ʻArrays.asList. The List returned by ʻArrays.asList is just an array wrapper, fixed in size but capable of changing elements. On the other hand, the List returned by List.of cannot change its elements and becomes immutable.

n-> new int [n] is int [] :: new

This looks like a mysterious syntax, but that's what it is. Just as new String (data) can be written as String :: new.

Do not use Functional Interface fields

final Function<String, Price> CONVERT_STRING_TO_PRICE = 
  str -> str.substring(0, str.length() - 1).transfer(Price::of);

Define the field


It may be used like this.

But this is a normal method definition,

Price stringToPrice(String str) {
  return str.substring(0, str.length() - 1).transfer(Price::of);

It's better to use method references.


You can write comments in JavaDoc properly, and methods are easier to read than higher-order functions. Also, lambdas are eventually compiled into method definitions and method references, which is a bit of a waste.

Do not use Optional for fields

Since it is not Serializable, it is better to use a normal value with @Nullable etc.

Do not use Stream as a return value

Unless it is a method that manipulates Stream itself, it is better not to return Stream. As a standard, you can return Stream <T>, but something that returns a concrete type such as Stream <String> is not very good. Or when you want to perform "stream processing" with I / O like Files.lines.

Stream pattern

Use IntStream when you want to stream boolean [] or char []

IntStream.range(0, chars.length)->mapToObject(i -> chars[i])


for (var a : list) {
  if (condition(a)) {
    return true;
return false



for (var a : list) {
  if (!condition(a)) {
    return false;
return true



for (var a : list) {
  if (condition(a)) {
    return false;
return true


Do not put Stream or List in Optional

ʻOptional` is a special collection of elements 0 or 1, so you don't need to enclose the Stream or List further and you should be able to handle it with Stream.of () or emptyList ().


Normal system and abnormal system

Then the normal system, else the abnormal system

if (cond) {
} else {

However, when the abnormal system returns / throws in one line, put the abnormal system in if and escape early.

if (!cond) {
  throw error

!(aa && bb)

It is better to avoid inversion by enclosing it in large parentheses like ! (Aa && bb).

if (!(hasValue && isValid)) {


if (!hasValue || !isValid) {

You don't have to cache the results in your brain. However, if ʻelse` is attached

if (!hasValue || !isValid) {
} else {


if (hasValue && isValid) {
} else {

Does not have to be reversed. In this case, the policy of changing the normal system to then is also followed. Also, the IDE should do this kind of logic reversal, so it's better not to do it by hand.


When it has parameters, add @AllArgsConstructor and @Getter. @Value cannot be used for ʻenum`.

enum Hoge {

  private String message;

Integer.valueOf returns an Integer

ʻInteger and Long valueOf` return a wrapper object. So code like this is useless

int n = Integer.valueOf(str);

Use parseInt if you want to use it as a primitive

int n = Integer.parseInt(str);

Use UncheckedIOException to make IOException an unchecked exception

catch (IOException ex) {
  throw new UncehckedIOException(ex);

Add Visible For Testing when using package private for testing

String createId(String seed) {

Recommended Posts

Java pattern memo
Java memo
java anything memo
[Java] Strategy pattern
Java Silver memo
Java design pattern
java, maven memo
java callback pattern
[Java] Singleton pattern
Java SE 7 memo
java anything memo 2
[Java] Adapter pattern
Java specification memo
My DAO pattern (Java)
Java development environment memo
Java learning memo (method)
Java Kuche Day memo
[Java ~ Method ~] Study memo (5)
java se 8 programmer Ⅰ memo
Java paid private memo
[Java ~ Array ~] Study memo 4
Java learning memo (basic)
java lambda expression memo
(Memo) Java for statement
Java lambda expression [memo]
Java learning memo (interface)
[Java] Implicit inheritance memo
Java learning memo (inheritance)
Builder pattern (Effective Java)
java competitive programming memo
[Memo] Java Linked List
Java Lambda Command Pattern
Java design pattern summary
[Java] Variable name naming memo
Java memo (standard class) substring
Java learning memo (data type)
Java memo (standard class) length
[Java ~ Boolean value ~] Study memo (2)
Create a java method [Memo] [java11]
Java Silver exam preparation memo
Java learning memo (logical operator)
Enum Strategy pattern in Java
[Java] Draw a simple pattern
Java learning memo (abstract class)
Java study memo 2 with Progate
Java HashMap, entrySet [Personal memo]
Ruby design pattern template method pattern memo
Java learning memo (creating an array)
Personal memo: Metaprogramming with Java reflection
JCA (Java Cryptography Architecture) Usage Memo
[Java] Memo for naming class names
[Study session memo] Java Day Tokyo 2017
Java learning memo (while statement, do-while statement)
From Java to VB.NET-Writing Contrast Memo-
Java beginner's stumbling block [memo writing]
JNA (Java Native Access) pattern collection
Java beginner design pattern (Factory Method pattern)
[Java] Processing time measurement method memo
I tried using Java memo LocalDate