Map without using an array in java

I wrote an article like this the other day.

I want to get rid of the over-nested () https://qiita.com/tkturbo/items/04960f4e3e7226de3b46

I wrote here

// using custom object
const applier = (val)=>{
  const  o={
    value : val,
    apply : (unary)=>{ o.value=unary(o.value); return o; }
  };
  return o;
};
console.log(applier(name).apply(f0).apply(f1).apply(f2).value);

Let's convert this to java today.

Applier.java


public class Applier<T> {
  private T value;
  public Applier(T value) {
    this.value = value;
  }
  // import java.util.function.Function;
  public <R> Applier<R> apply(Function<? super T, ? extends R> appliable){
    return new Applier<R>(appliable.apply(this.value));
  }
  public T get() { return this.value; }
}

That's right if you use the java.util.function.Function interface.

It is this one line that will be the key.

  public <R> Applier<R> apply(Function<? super T, ? extends R> appliable){

Because of this, it is possible to process even if the input and output have different types.

For example

System.out.println(
  new Applier<String>("0001")
    .apply(v->Integer.parseInt(v,10))
    .apply(v->v+1)
    .apply(v->String.format("%04d", v))
    .get()
);

It can be used like this.

personally,

    return new Applier<R>(appliable.apply(this.value));

This one line feels strange, so I want to write it like this.

public class Applier<T> {
  private T value;
  public Applier(T value) {
    this.value = value;
  }
  public <R> Applier<R> apply(Appliable<? super T, ? extends R> appliable){
    return new Applier<R>(appliable.applyTo(this.value));
  }
  public T get() { return this.value; }
  @FunctionalInterface
  public static interface Appliable<T, R> {
    public R applyTo(T value);
  }
}

The public static interface is used to prevent namespace pollution.

Recommended Posts

Map without using an array in java
Arbitrary search method in an array using binary search
Try using RocksDB in Java
Use without preparing an authentication file when using Firebase Admin SDK in Java
I tried using an extended for statement in Java
[Ruby] Count an even number in an array using the even? Method
I want to build Java Applet without using an IDE
[Java8] Sort int type array in descending order using stream
Generate Stream from an array of primitive types in Java
I sent an email in Java
Java learning memo (creating an array)
Encrypt using RSA cryptography in Java
Get Null-safe Map values in Java
Use composite keys in Java Map.
[Java] Declare and initialize an array
Try an If expression in Java
I made an annotation in Java.
HTTPS connection using tls1.2 in Java 6
I tried using JWT in Java
Formatting an enum using formatter-maven-plugin (Java)
Run an external process in Java
[Java] array
Java array
Java array
Note No. 1 "Counting and displaying duplicate values in an array" [Java]
java (array)
Java array
[Java] Array
JAVA (Map)
Java array
Cast an array of Strings to a List of Integers in Java
[Android] Convert Map to JSON using GSON in Kotlin and Java
java array
[Java] Array
[Java] Express Enum type without using Enum (enumeration) type
When seeking multiple in a Java array
Duplicate Map sorted by key in Java
Try using the Stream API in Java
Reverse Key from Value in Java Map
[Java] Send an email using Amazon SES
Study Java Try using Scanner or Map
Using JavaScript from Java in Rhino 2021 version
Organized memo in the head (Java --Array)
ERRORCODE = -4471 occurs in Java application using Db2.
Try using JSON format API in Java
Compare the elements of an array (Java)
Read Felica using RC-S380 (PaSoRi) in Java
[Spring MVC] Implement dynamic parameters included in URL without using Optional (~ Java7)
I want to ForEach an array with a Lambda expression in Java
[Swift] How to set an image in the background without using UIImageView.
[In-house study session] Java basics-execution without using IDE- (2017/07/06)
Try adding text to an image in Scala using the Java standard library
java array variable
[Java] Creating an Excel file using Apache POI
Implement share button in Rails 6 without using Gem
ChatWork4j for using the ChatWork API in Java
I want to send an email in Java.
[Java] API creation using Jerjey (Jax-rs) in eclipse
Send email using Amazon SES SMTP in Java
Send push notifications using Notification Hubs in Java
Changes in Java 11