How to return multiple values in Java methods (Generics, Record, Triple, Tuple, using standard library or external library)

Overview

--Sample code using standard library or external library for how to return multiple values in Java method (sample code that returns 3 values)

environment

External library you are using

How to avoid using external libraries

Standard library java.lang.Object []

Set a value in an array of Objects and return it. The recipient needs a cast.

public class ArraySample {

  static Object[] getArray() {
    Object[] array = new Object[3];
    array[0] = "foo";
    array[1] = true;
    array[2] = 123456;
    return array;
  }

  public static void main(String[] args) {
    Object[] array = getArray();
    String  foo = (String) array[0];
    boolean bar = (boolean) array[1];
    int     baz = (int) array[2];
    System.out.println(foo);
    System.out.println(bar);
    System.out.println(baz);
  }
}

Standard library java.util.List

Set the value to the element of List and return it. The recipient needs a cast.

import java.util.ArrayList;
import java.util.List;

public class ListSample {

  static List<Object> getList() {
    ArrayList<Object> list = new ArrayList<>();
    list.add("foo");
    list.add(true);
    list.add(123456);
    return list;
  }

  public static void main(String[] args) {
    List<Object> list = getList();
    String  foo = (String) list.get(0);
    boolean bar = (boolean) list.get(1);
    int     baz = (int) list.get(2);
    System.out.println(foo);
    System.out.println(bar);
    System.out.println(baz);
  }
}

Standard library java.util.Map

Set the value to the element of Map and return it. The recipient needs a cast.

import java.util.HashMap;
import java.util.Map;

public class MapSample {

  static Map<String, Object> getMap() {
    HashMap<String, Object> map = new HashMap<>();
    map.put("foo", "foo");
    map.put("bar", true);
    map.put("baz", 123456);
    return map;
  }

  public static void main(String[] args) {
    Map<String, Object> map = getMap();
    String  foo = (String) map.get("foo");
    boolean bar = (boolean) map.get("bar");
    int     baz = (int) map.get("baz");
    System.out.println(foo);
    System.out.println(bar);
    System.out.println(baz);
  }
}

Define a class

Define a class, set a value in the field and return.

public class ClassSample {

  static class MyClass {
    String foo;
    boolean bar;
    int baz;
  }

  static MyClass getMyClass() {
    MyClass cls = new MyClass();
    cls.foo = "foo";
    cls.bar = true;
    cls.baz = 123456;
    return cls;
  }

  public static void main(String[] args) {
    MyClass cls = getMyClass();
    String  foo = cls.foo;
    boolean bar = cls.bar;
    int     baz = cls.baz;
    System.out.println(foo);
    System.out.println(bar);
    System.out.println(baz);
  }
}

Define a genericized class

Define a genericized class and specify the type when using it. Set a value in the field and return it.

public class GenericsSample {

  static class ValuesGenerics<T1, T2, T3> {
    T1 foo;
    T2 bar;
    T3 baz;
  }

  static ValuesGenerics<String, Boolean, Integer> getGenerics() {
    ValuesGenerics<String, Boolean, Integer> generics = new ValuesGenerics<>();
    generics.foo = "foo";
    generics.bar = true;
    generics.baz = 123456;
    return generics;
  }

  public static void main(String[] args) {
    ValuesGenerics<String, Boolean, Integer> generics = getGenerics();
    String  foo = generics.foo;
    boolean bar = generics.bar;
    int     baz = generics.baz;
    System.out.println(foo);
    System.out.println(bar);
    System.out.println(baz);
  }
}

Define a record

Define and use records (scheduled to be officially introduced from Java 16).

public class RecordSample {

  record ValuesRecord(String foo, boolean bar, int baz) {
  }

  static ValuesRecord getRecord() {
    return new ValuesRecord("foo", true, 123456);
  }

  public static void main(String[] args) {
    ValuesRecord record = getRecord();
    String  foo = record.foo;
    boolean bar = record.bar;
    int     baz = record.baz;
    System.out.println(foo);
    System.out.println(bar);
    System.out.println(baz);
  }
}

Reference: JEP 384: Records \ (Second Preview )

How to use an external library

Kotlin Standard Library (kotlin.Triple)

Use Kotlin's standard library from Java. Kotlin's standard library includes Pair, which handles two values, and Triple, which handles three values.

import kotlin.Triple;

public class KotlinSample {

  static Triple<String, Boolean, Integer> getTriple() {
    return new Triple<>("foo", true, 123456);
  }

  public static void main(String[] args) {
    Triple<String, Boolean, Integer> triple = getTriple();
    String  foo = triple.getFirst();
    boolean bar = triple.getSecond();
    int     baz = triple.getThird();
    System.out.println(foo);
    System.out.println(bar);
    System.out.println(baz);
  }
}

Reference: Triple -Kotlin Programming Language

Scala Standard Library (scala.Tuple3)

Use Scala's standard library from Java. Scala's standard library ranges from Tuple1 for one value to Tuple22 for 22 values.

import scala.Tuple3;

public class ScalaSample {

  static Tuple3<String, Boolean, Integer> getTuple3() {
    return new Tuple3<>("foo", true, 123456);
  }

  public static void main(String[] args) {
    Tuple3<String, Boolean, Integer> tuple = getTuple3();
    String  foo = tuple._1();
    boolean bar = tuple._2();
    int     baz = tuple._3();
    System.out.println(foo);
    System.out.println(bar);
    System.out.println(baz);
  }
}

Reference: Scala Standard Library 2 \ .13 \ .3 -scala \ .Tuple3

Apache Commons Lang (org.apache.commons.lang3.tuple.Triple)

Apache Commons Lang has a Pair that handles two values and a Triple that handles three values.

import org.apache.commons.lang3.tuple.Triple;

public class ApacheCommonsLangSample {

  static Triple<String, Boolean, Integer> getTriple() {
    return Triple.of("foo", true, 123456);
  }

  public static void main(String[] args) {
    Triple<String, Boolean, Integer> triple = getTriple();
    String  foo = triple.getLeft();
    boolean bar = triple.getMiddle();
    int     baz = triple.getRight();
    System.out.println(foo);
    System.out.println(bar);
    System.out.println(baz);
  }
}

Reference: [Triple \ (Apache Commons Lang 3 \ .11 API )](https://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/tuple/ Triple.html)

Functional Java (fj.P3)

Functional Java ranges from P1 for one value to P8 for eight values.

import fj.P;
import fj.P3;

public class FunctionalJavaSample {

  static P3<String, Boolean, Integer> getP3() {
    return P.p("foo", true, 123456);
  }

  public static void main(String[] args) {
    P3<String, Boolean, Integer> p = getP3();
    String  foo = p._1();
    boolean bar = p._2();
    int     baz = p._3();
    System.out.println(foo);
    System.out.println(bar);
    System.out.println(baz);
  }
}

Reference: P3 \ (core 4 \ .8 \ .1 API )

javatuples (org.javatuples.Triplet)

javatuples ranges from Units that handle one value to Decades that handle ten values.

import org.javatuples.Triplet;

public class JavatuplesSample {

  static Triplet<String, Boolean, Integer> getTriplet() {
    return Triplet.with("foo", true, 123456);
  }

  public static void main(String[] args) {
    Triplet<String, Boolean, Integer> triplet = getTriplet();
    String  foo = triplet.getValue0();
    boolean bar = triplet.getValue1();
    int     baz = triplet.getValue2();
    System.out.println(foo);
    System.out.println(bar);
    System.out.println(baz);
  }
}

Reference: Triplet \ (javatuples 1 \ .2 API )

jOOλ (org.jooq.lambda.tuple.Tuple3)

jOOλ ranges from Tuple0, which handles 0 values, to Tuple16, which handles 16 values.

import org.jooq.lambda.tuple.Tuple;
import org.jooq.lambda.tuple.Tuple3;

public class JoolSample {

  static Tuple3<String, Boolean, Integer> getTuple3() {
    return Tuple.tuple("foo", true, 123456);
  }

  public static void main(String[] args) {
    Tuple3<String, Boolean, Integer> tuple = getTuple3();
    String  foo = tuple.v1();
    boolean bar = tuple.v2();
    int     baz = tuple.v3();
    System.out.println(foo);
    System.out.println(bar);
    System.out.println(baz);
  }
}

Reference: [Tuple3 \ (jOOL 0 \ .9 \ .14 API )](https://www.jooq.org/products/jOO%ce%bb/javadoc/0.9.14/org/jooq/lambda/tuple /Tuple3.html)

Reactor (reactor.util.function.Tuple3)

Reactors range from Tuple2, which handles two values, to Tuple8, which handles eight values.

import reactor.util.function.Tuple3;
import reactor.util.function.Tuples;

public class ReactorSample {

  static Tuple3<String, Boolean, Integer> getTuple3() {
    return Tuples.of("foo", true, 123456);
  }

  public static void main(String[] args) {
    Tuple3<String, Boolean, Integer> tuple = getTuple3();
    String  foo = tuple.getT1();
    boolean bar = tuple.getT2();
    int     baz = tuple.getT3();
    System.out.println(foo);
    System.out.println(bar);
    System.out.println(baz);
  }
}

Reference: [Tuple3 \ (reactor \ -core 3 \ .3 \ .10 \ .RELEASE )](https://projectreactor.io/docs/core/3.3.10.RELEASE/api/reactor/util/function/ Tuple3.html)

Vavr (io.vavr.Tuple3)

Vavr ranges from Tuple0, which handles 0 values, to Tuple8, which handles 8 values.

import io.vavr.Tuple;
import io.vavr.Tuple3;

public class VavrSample {

  static Tuple3<String, Boolean, Integer> getTuple3() {
    return Tuple.of("foo", true, 123456);
  }

  public static void main(String[] args) {
    Tuple3<String, Boolean, Integer> tuple = getTuple3();
    String  foo = tuple._1();
    boolean bar = tuple._2();
    int     baz = tuple._3();
    System.out.println(foo);
    System.out.println(bar);
    System.out.println(baz);
  }
}

Reference: Tuple3 \ (Vavr 0 \ .10 \ .3 API )

Sample code execution file

build.gradle

plugins {
  id 'java'
  id 'application'
}

repositories {
  mavenCentral()
}

dependencies {
  //Required libraries
  implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.4.10'
  implementation 'org.scala-lang:scala-library:2.13.3'
  implementation 'org.apache.commons:commons-lang3:3.11'
  implementation 'org.functionaljava:functionaljava:4.8.1'
  implementation 'org.javatuples:javatuples:1.2'
  implementation 'org.jooq:jool:0.9.14'
  implementation 'io.projectreactor:reactor-core:3.3.10.RELEASE'
  implementation 'io.vavr:vavr:0.10.3'
}

sourceCompatibility = JavaVersion.VERSION_15
mainClassName = "App"

tasks.withType(JavaCompile) {
  //Use Java 15 preview feature
  options.compilerArgs += ['--enable-preview']
}

application {
  //Use Java 15 preview feature
  applicationDefaultJvmArgs = ['--enable-preview']
}

Sample code batch execution class

public class App {

  public static void main(String[] args) {
    ArraySample.main(args);
    ListSample.main(args);
    MapSample.main(args);
    ClassSample.main(args);
    GenericsSample.main(args);
    RecordSample.main(args);
    KotlinSample.main(args);
    ScalaSample.main(args);
    ApacheCommonsLangSample.main(args);
    FunctionalJavaSample.main(args);
    JavatuplesSample.main(args);
    JoolSample.main(args);
    ReactorSample.main(args);
    VavrSample.main(args);
  }
}

Reference material

-Note when you want Tuple in Java -Qiita

Recommended Posts

How to return multiple values in Java methods (Generics, Record, Triple, Tuple, using standard library or external library)
How to convert A to a and a to A using AND and OR in Java
Try adding text to an image in Scala using the Java standard library
[Java] How to encrypt with AES encryption with standard library
How to try Java preview features (such as Java 15 Record) in IntelliJ
How to solve the unknown error when using slf4j in Java
How to learn JAVA in 7 days
How to insert an external library
[Java] Try to implement using generics
How to use class methods [Java]
How to use classes in Java?
How to name variables in Java
How to concatenate strings in java
How to implement a slideshow using slick in Rails (one by one & multiple by one)
How to test private methods with arrays or variadic arguments in JUnit
[Java] How to receive numerical data separated by spaces in standard input