[JAVA] Function is very easy to use

Function interface

You can now write a method as a Function in a lambda expression. Let's do it for the time being.

reference Official Reference


package functionPractice;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Arrays;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;

 * To practice function-interface
 * @author me
public class FunctionPractice {

	 * main
	 * @param args
	public static void main(String[] args) {
		try {
		} catch (NumberFormatException e) {
			echo.accept("・ Ω ・ v");

	 * run
	 * @throws NumberFormatException if conversion to numeric number is impossible.
	private static void run() throws NumberFormatException {
		List<Integer> list = generate.get().stream().map(toInteger).collect(Collectors.toList());

	/** Output */
	static Consumer<Object> echo = v -> System.out.println(v);
	/** Generate */
	static Supplier<List<String>> generate = () -> Arrays.asList(null, "1", "2", "3");
	/** Object to String */
	static Function<Object, String> toString = v -> String.valueOf(v);
	/** Object to Integer */
	static Function<Object, Integer> toInteger = v -> Integer
	/** Add "3" */
	static UnaryOperator<String> add3 = v -> v + "3";
	/** Join comma */
	static Function<List<String>, String> join = v -> v.stream().filter(Objects::nonNull).collect(Collectors.joining(","));
	/** Sum integer list */
	static Function<List<Integer>, Integer> sum = v -> v.stream().filter(Objects::nonNull)

Just declare Function <argument, return value> and write whatever process you like.

Well, there are many other things, but they were officially referred to.

What you usually write with methods can be written with Funtion The number of lines will be reduced and it will be easier to see.

The difficulty is that javaDoc is hard to see?

Even so, it will be insanely smart, right?

Recommended Posts

Function is very easy to use
Easy to use array.map (&: method)
Easy to use Cloud Firestore (Android)
[Processing × Java] How to use the function
[Rails] Enum is easier to use! Enumelize!
"Let's write versatile code that is easy to use over time" (nth time)
Write code that is easy to maintain (Part 1)
Add a tag function to Rails. Use acts-as-taggable-on
Super easy way to use enum with JSP
Write code that is easy to maintain (Part 4)
Write code that is easy to maintain (Part 3)
How to use Map
How to use rbenv
How to use letter_opener_web
How to use fields_for
How to use java.util.logging
How to use map
How to use collection_select
PATH to use docker-slim
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
[How to use label]
How to use identity
How to use hashes
14 Corresponds to function expressions
How to use JUnit 5
How to use Dozer.mapper
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
Use stream to verify that SimpleDateFormat is thread unsafe
How to use Map
Easy to maintain FizzBuzz
Delegate is convenient to use when you want to reuse parts
[Java] How to use Map
How to use Chain API
[Java] How to use Map
How to use Priority Queuing
Java to play with Function
How to add ActionText function
[Rails] How to use enum
Easy to create Processing library
How to use java Optional
How to use JUnit (beginner)
How to use Ruby return
Use ransack Search function implementation
[Rails] How to use enum
How to use @Builder (Lombok)
[Swift] How to use UserDefaults
How to use java class
How to use Swift UIScrollView
How to use Big Decimal
[Java] How to use Optional ②
[Java] How to use removeAll ()
How to use String [] args
[Java] How to use string.format
How to use rails join