What I researched about Java 8

Overview

To upgrade Java knowledge that stopped at "1.4" I am learning about Java7,8,9. This time, I will only describe what I investigated about the functions added in "8".

What I looked up

Optional It is now possible to explicitly indicate whether the return value may contain null. You can also set what to do if there is no value when you retrieve it.

public Optional<String> getGyunyu(String name) {
  Map<String, String> gyunyu = new HashMap<>();
  gyunyu.put("milk", "コーヒーmilk");
  return Optional.ofNullable(gyunyu.get(name));
}
Optional<String> gyunyu = getGyunyu("Tea");
System.out.println(gyunyu.orElse("Not milk")); // 「Not milk」が出力される

--ofNullable (T value): Generate Optional. If the value is not null, it returns an Optional that describes the value, otherwise it returns an empty Optional. --orElse (T other): Returns a value if it exists, otherwise returns other.

You can also check for the existence of a value by using the ʻisPresent ()` method.

Optional<String> gyunyu = getGyunyu("Tea");
if (!gyunyu.isPresent()) {
  System.out.println("Not milk");
}

In this case, I feel that it is not much different from ʻif (return value == null)`, but if it is null, the value should be retaken. It seems that it can be used when you want to perform additional processing.

Optional class

interface interface can now have an implementation. By describing the default keyword, you can describe the method that has the implementation.

Gyunyu.java


public interface Gyunyu {
  public default void print() {
    System.out.println("milk");
  }
}

Coffee.java


public class Coffee implements Gyunyu {
  //I haven't overridden the print method but I don't get a compile error
}
Gyunyu gyunyu = new Coffee();
gyunyu.print();

Create a Milk interface that inherits the Gyunyu interface and If you implement it in the ʻIchigoclass and call theprint ()` method,

Milk.java


public interface Milk extends Gyunyu {
  public default void print() {
    System.out.println("milk");
  }
}

Ichigo.java


public class Ichigo implements Milk {
}
Gyunyu gyunyu = new Ichigo();
gyunyu.print(); //"Milk" is output

The method closest to the class calling the method (in this case the Milk interfaceprint ()) is executed.

Date and Time API

Get the current date and time

You can get the current date and time by using the now () method.

//No time zone information
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime.getYear());
System.out.println(localDateTime.getMonthValue());
System.out.println(localDateTime.getDayOfMonth());
System.out.println(localDateTime.getHour());
System.out.println(localDateTime.getMinute());
System.out.println(localDateTime.getSecond());
System.out.println(localDateTime.getNano());
		
//With time zone information
ZonedDateTime zoneDatetime = ZonedDateTime.now();
System.out.println(zoneDatetime.getYear());
System.out.println(zoneDatetime.getMonthValue());
System.out.println(zoneDatetime.getDayOfMonth());
System.out.println(zoneDatetime.getHour());
System.out.println(zoneDatetime.getMinute());
System.out.println(zoneDatetime.getSecond());
System.out.println(zoneDatetime.getNano());
System.out.println(zoneDatetime.getZone().getId()); // 「Asia/"Tokyo" is output

Displayed in Japanese calendar

JapaneseDate japaneseDate = JapaneseDate.now();
System.out.println(japaneseDate.getEra()); //"Heisei" is output
System.out.println(japaneseDate.get(ChronoField.YEAR_OF_ERA)); //"29" is displayed

Addition and subtraction

LocalDateTime localDateTime = LocalDateTime.now();

LocalDateTime tomorrow = localDateTime.plusDays(1);
System.out.println(tomorrow.getDayOfMonth()); //"30" is output when executed on November 29th

LocalDateTime yesterday = localDateTime.minusDays(1);
System.out.println(yesterday.getDayOfMonth()); //"28" is output when executed on November 29th

--Use plus Years (long years) if you want to add years --If you want to subtract years, use minus Years (long years) --For other addition / subtraction, LocalDateTime class

Base64 encoding / decoding

It is now supported as standard.

String coffee = "coffee milk";
		
Encoder encoder = Base64.getEncoder();
String encodeCoffee = encoder.encodeToString(coffee.getBytes());
System.out.println(encodeCoffee); //"44Kz44O844OS44O854mb5Lmz" is output
		
Decoder decoder = Base64.getDecoder();
String decodeCoffee = new String(decoder.decode(encodeCoffee));
System.out.println(decodeCoffee); //"Coffee milk" is output

String concatenation

By using String.join (), it is now possible to easily join multiple strings with any string.

String[] gyunyu = {"coffee milk", "Lemon milk", "Strawberry milk"};
System.out.println(String.join("###", gyunyu)); //"coffee milk###Lemon milk###Output with "strawberry milk"
List<String> gyunyu = new ArrayList<>();
gyunyu.add("coffee milk");
gyunyu.add("Lemon milk");
gyunyu.add("Strawberry milk");
System.out.println(String.join("###", gyunyu)); //"coffee milk###Lemon milk###Output with "strawberry milk"

Similar joins can be made with the java.util.StringJoiner class.

StringJoiner gyunyu = new StringJoiner("###");
gyunyu.add("coffee milk");
gyunyu.add("Lemon milk");
gyunyu.add("Strawberry milk");
System.out.println(gyunyu); //"coffee milk###Lemon milk###Output with "strawberry milk"

The StringJoiner class can also be joined by specifying a prefix and suffix.

StringJoiner gyunyu = new StringJoiner(" > ", "[Like]", "[Hate]");
gyunyu.add("coffee milk");
gyunyu.add("Lemon milk");
gyunyu.add("Strawberry milk");
System.out.println(gyunyu); //「[Like]coffee milk>Lemon milk>Strawberry milk[Hate]Is output

Lambda expression

It is written in the following grammar.

(Method arguments) -> {processing}

By using a lambda expression, it seems that the process can be written concisely ... When there is the following interface and processing

public interface Gyunyu {	
  public void output(String v);
}
Gyunyu gyunyu = new Gyunyu() {
  @Override
  public void output(String v) {
    System.out.println(v);
  }
};
gyunyu.output("coffee milk"); //「coffee milk」と出力される

Using a lambda expression, you can write something like the following.

Gyunyu gyunyu = (String v) -> { System.out.println(v); };
gyunyu.output("coffee milk"); //「coffee milk」と出力される

Furthermore, the argument type is inferred and can be omitted. If there is only one argument, the parentheses "()" can also be omitted. If there is only one expression, the curly braces "{}" can be omitted, so the description can be as follows.

Gyunyu gyunyu = v -> System.out.println(v);
gyunyu.output("coffee milk");

Functional interface

An interface that declares only one abstract method. If you make the Gyunyu interface written in the lambda expression a functional interface, The description is as follows.

Gyunyu.java


@FunctionalInterface
public interface Gyunyu {
   public void output(String s);
}

By adding the @FunctionalInterface annotation, it is possible to explicitly indicate that it is a functional interface. It will be possible to issue a compile error if the conditions of the functional interface are not met.

Method reference

Since method references can omit arguments, the amount of description can be reduced compared to lambda expressions. How to execute static method or instance method.

--Notation method

Class name (instance name) :: method name

When referencing an instance method

Gyunyu gyunyu = System.out::println;
gyunyu.output("coffee milk"); //「coffee milk」と出力される

If you describe the above with a lambda expression, you can write it as follows.

Gyunyu gyunyu = v -> System.out.println(v);
gyunyu.output("coffee milk");

If the method you want to refer to is your own class, use this

Main.java


public void println(String v) {
  System.out.println(v);
}

public void execute() {
  List<String> gyunyu = Arrays.asList("coffee milk", "Strawberry milk", "Lemon milk");
  gyunyu.forEach(this::println);
}

public static void main(String[] args) {
  (new Main()).execute();
}

When referencing a static method

Main.java


public static void output(String v) {
  System.out.println(v);
}

public static void main(String[] args) {
  List<String> gyunyu = Arrays.asList("coffee milk", "Strawberry milk", "Lemon milk");
  gyunyu.forEach(Main::output);
}

Stream API API that can aggregate values and process using data. The basic processing flow is

  1. Stream generation
  2. Intermediate processing
  3. Termination become that way.

Stream generation

--For arrays

String[] gyunyu1 = {"coffee milk", "Coffee milk", "Strawberry milk", "Strawberry milk", "Lemon milk", "Lemon milk"};
Stream<String> stream1 = Arrays.stream(gyunyu1);

String[] gyunyu2 = {"Melon milk", "Apple milk", "Melon milk", "Grape milk"};
Stream<String> stream2 = Stream.of(gyunyu2);

--For Collection

List<String> gyunyu3 = Arrays.asList("fruits", "Banana", "Watermelon");
Stream<String> stream3 = gyunyu3.stream();

--For Map

Map<String, String> gyunyu4 = new HashMap<>();
gyunyu4.put("1", "Peach milk");
gyunyu4.put("2", "Loquat");
gyunyu4.put("3", "Fig milk");
Stream<Entry<String, String>> stream4 = gyunyu4.entrySet().stream();

Reference: Stream generation

Intermediate operation

Processing to narrow down and edit Steam and create a new Stream.

--When narrowing down the elements --By using the filter method, you can get the Stream of only the specified element.

//I want to extract only the character string containing milk
String[] gyunyu1 = {"coffee milk", "Coffee milk", "Strawberry milk", "Strawberry milk", "Lemon milk", "Lemon milk"};
Stream<String> stream1 = Arrays.stream(gyunyu1);
stream1.filter(s -> s.contains("milk"));

--If you want to remove duplicate elements --You can get the Stream with duplicate elements removed by using the distinct method.

//I want to remove duplicate milk
String[] gyunyu2 = {"Melon milk", "Apple milk", "Melon milk", "Grape milk"};
Stream<String> stream2 = Stream.of(gyunyu2);
stream2.distinct();

--If you want to convert elements --You can get the Stream with updated elements by using the map method.

//I want to add milk
List<String> gyunyu3 = Arrays.asList("fruits", "Banana", "Watermelon");
Stream<String> stream3 = gyunyu3.stream();
stream3.map(s -> s + "milk");

Reference: Intermediate operation

Termination

When the termination process is executed, the intermediate process is evaluated.

--If you want to get the number of elements --You can get the number of elements by using the count method.

//I want to output the number of cases including milk
Map<String, String> gyunyu4 = new HashMap<>();
gyunyu4.put("1", "Peach milk");
gyunyu4.put("2", "Loquat");
gyunyu4.put("3", "Fig milk");
Stream<Entry<String, String>> stream4 = gyunyu4.entrySet().stream();
System.out.println(stream4.filter(s -> s.getValue().contains("milk")).count()); //"2" is output

--When you want to take out elements one by one and operate them --Side effects can be generated by using the forEach method.

String[] gyunyu1 = {"coffee milk", "Coffee milk", "Strawberry milk", "Strawberry milk", "Lemon milk", "Lemon milk"};
Stream<String> stream1 = Arrays.stream(gyunyu1);
stream1.filter(s -> s.contains("milk")).forEach(s -> System.out.println(s));

Reference: Termination processing

At the end

Since the service I am in charge of in business runs on Java 5 and 6, I do not have the opportunity to use 8 in business, but I would like to create an opportunity to upgrade the version. I'm not familiar with StreamAPI and lambda expressions yet, so I feel I need to learn more about these two features.

Recommended Posts

What I researched about Java 8
What I researched about Java 6
What I researched about Java 9
What I researched about Java 7
What I researched about Java 5
What I researched about Java learning
What I learned about Kotlin
What I learned with Java Gold
What I learned with Java Silver
About Java interface
[Java] About Java 12 features
What is java
[Java] About arrays
Take what you've learned about Java reflection
Something about java
Where about java
About Java features
What is Java <>?
About Java threads
[Java] About interface
What is Java
What i learned
About Java class
About Java arrays
About java inheritance
About interface, java interface
What I learned from Java monetary calculation
About List [Java]
About java var
About Java literals
About Java commands
What I thought about when I started migrating from Java to Kotlin
Summary of what I learned about Spring Boot
[Java] What should I use for writing files?
What I learned in Java (Part 2) What are variables?
What I did when I converted java to Kotlin
About Java log output
About Java functional interface
Java, about 2D arrays
About class division (Java)
About [Java] [StreamAPI] allMatch ()
About Java StringBuilder class
I first touched Java ②
[Java] About Singleton Class
I first touched Java ③
About Java method binding
I first touched Java ④
[Java] About anonymous classes
About method splitting (Java)
What is Java Encapsulation?
[Java Silver] About initialization
What I learned ② ~ Mock ~
About Java Array List
About Java Polymorphism super ()
What I learned ① ~ DJUnit ~
About inheritance (Java Silver)
About Java String class
About Java access modifiers
About Java lambda expressions
What is Java technology?
I first touched Java