- vừa được xem lúc

Function programming in Java 17

0 0 13

Người đăng: Do Nhu Vy

Theo Viblo Asia

Functional interfaces:

Interface Method Return type
Supplier<T> get() T
Consumer<T> accept(T) void
<BiConsumer<T, U> accept(T, U) T
Predicate<T> test(T) boolean
BiPreidcate<T, U> test(T, U) boolean
Function<T, R> apply(T) R
BiFunction<T, U, R> apply(T, U) R
UnaryOperator<T> apply(T) T
BinaryOperator<T> apply(T, T) T

For setting development for Java 17, see https://stackoverflow.com/questions/70083274/java-17-java-invalid-source-release-7-with-enable-preview-preview-language/70083285#70083285

Use Supplier

package org.example; import java.time.LocalDate;
import java.util.function.Supplier; public class App { public static void main(String[] args) { Supplier<LocalDate> localDateSupplier = LocalDate::now; Supplier<LocalDate> localDateSupplier1 = () -> LocalDate.now(); LocalDate localDate = localDateSupplier.get(); LocalDate localDate1 = localDateSupplier1.get(); System.out.print("localDate = "); System.out.println(localDate); System.out.print("localDate1 = "); System.out.println(localDate1); } }

result

localDate = 2021-11-23
localDate1 = 2021-11-23

More complete object

 ArrayList<String> stringArrayList = new ArrayList<>(); stringArrayList.add("Do Nhu Vy"); stringArrayList.add("Nguyen Bich Van"); stringArrayList.add("Nguyen Hong Do"); stringArrayList.add("Nguyen Tien Nam"); Supplier<ArrayList<String>> arrayListSupplier2 = () -> { return stringArrayList; }; ArrayList<String> strings2 = arrayListSupplier2.get(); System.out.print("strings2 = "); System.out.println(strings2);

or other way

 Supplier<ArrayList<String>> arrayListSupplier2 = () -> { ArrayList<String> stringArrayList = new ArrayList<>(); stringArrayList.add("Do Nhu Vy"); stringArrayList.add("Nguyen Bich Van"); stringArrayList.add("Nguyen Hong Do"); stringArrayList.add("Nguyen Tien Nam"); return stringArrayList; }; ArrayList<String> strings2 = arrayListSupplier2.get(); System.out.print("strings2 = "); System.out.println(strings2);

result

strings = []
strings2 = [Do Nhu Vy, Nguyen Bich Van, Nguyen Hong Do, Nguyen Tien Nam]

How Java virtual machine manage object what is instance of Supplier interface? Try an experiment:

 System.out.print("arrayListSupplier2 = "); System.out.println(arrayListSupplier2);

result

arrayListSupplier2 = org.example.App$$Lambda$19/_@.com

Implicity way, this is the result of arrayListSupplier2.toString() . Symbol $$ means object exist on memory only, not in hard disk. Exactly, arrayListSupplier2 initialized by a lambda expression as you seen in the below.

Implement Consumer interface

package org.example; import java.util.HashMap;
import java.util.function.BiConsumer;
import java.util.function.Consumer; public class SampleConsumer { public static void main(String[] args) { Consumer<String> stringConsumer = System.out::println; Consumer<String> stringConsumer1 = vy -> System.out.println(vy); stringConsumer.accept("Nguyen Bich Van"); stringConsumer1.accept("Tran Phuong Ly"); var map = new HashMap<String, Integer>(); BiConsumer<String, Integer> stringIntegerBiConsumer = map::put; BiConsumer<String, Integer> stringIntegerBiConsumer1 = (ke, va) -> map.put(ke, va); stringIntegerBiConsumer.accept("pigeon", 2); stringIntegerBiConsumer1.accept("pig", 4); System.out.print("map = "); System.out.println(map); } }

Result

Nguyen Bich Van
Tran Phuong Ly
map = {pigeon=2, pig=4}

Bình luận

Bài viết tương tự

- vừa được xem lúc

Tổng hợp các bài hướng dẫn về Design Pattern - 23 mẫu cơ bản của GoF

Link bài viết gốc: https://gpcoder.com/4164-gioi-thieu-design-patterns/. Design Patterns là gì. Design Patterns không phải là ngôn ngữ cụ thể nào cả.

0 0 277

- vừa được xem lúc

Học Spring Boot bắt đầu từ đâu?

1. Giới thiệu Spring Boot. 1.1.

0 0 257

- vừa được xem lúc

Cần chuẩn bị gì để bắt đầu học Java

Cần chuẩn bị những gì để bắt đầu lập trình Java. 1.1. Cài JDK hay JRE.

0 0 37

- vừa được xem lúc

Sử dụng ModelMapper trong Spring Boot

Bài hôm nay sẽ là cách sử dụng thư viện ModelMapper để mapping qua lại giữa các object trong Spring nhé. Trang chủ của ModelMapper đây http://modelmapper.org/, đọc rất dễ hiểu dành cho các bạn muốn tìm hiểu sâu hơn. 1.

0 0 180

- vừa được xem lúc

[Java] 1 vài tip nhỏ khi sử dụng String hoặc Collection part 1

. Hello các bạn, hôm nay mình sẽ chia sẻ về mẹo check String null hay full space một cách tiện lợi. Mình sẽ sử dụng thư viện Lớp StringUtils download file jar để import vào thư viện tại (link).

0 0 55

- vừa được xem lúc

Deep Learning với Java - Tại sao không?

Muốn tìm hiểu về Machine Learning / Deep Learning nhưng với background là Java thì sẽ như thế nào và bắt đầu từ đâu? Để tìm được câu trả lời, hãy đọc bài viết này - có thể kỹ năng Java vốn có sẽ giúp bạn có những chuyến phiêu lưu thú vị. DJL là tên viết tắt của Deep Java Library - một thư viện mã ng

0 0 124