A note when you want Tuple in Java

Java also incorporates relatively modern features such as lambda expressions and Stream APIs, and there are increasing cases where you want to return the processing result as two or more values. One way to do this is with Tuple, which is often available in other languages. However, Java does not have Tuple as a standard API, so it is a memo when I investigated a little.

What is Tuple

Tuple

A tuple is a set of ordered elements. Originally a mathematical concept, some programming languages provide a data type called tuples.
Tuple type specifications differ depending on the language, but it can store multiple different types of data and objects, and a serial number (subscript) is assigned to each element like an array, which represents a data structure that identifies the element. Often. In functional languages such as LISP, a data type that has a binary tree structure is called a tuple.

This is a quote from e-Words, and although this depends on the language and implementation, Tuple here is a data structure for grouping any number of elements and types.

Why there is no Tuple in the standard API

Does Java SE 8 have Pairs or Tuples? The answers from Oracle's Java and OpenJDK developers will be helpful. (Partial excerpt and super summary)

Java SE suggested having a Pair (Tuple) class and was rejected at least once. However, I understand the need for multiple implementations in other libraries and applications. However, if you provide a structure like Pair as a standard API, you will want to represent complex data structures with Pairs and collections without using abstractions (classes). (Pair <List <String>, List <Pair <String, List <Boolean >>>>, Map <Double, List <Pair <QueryTuple, Map <StatType, Number >>>>)
Also, naming convention issues and environments like the Stream API require Pairs in combination with primitives as well as Pairs for objects. (ʻObjIntPair, ʻObjLongPair, ʻObjDoublePair, ʻIntObjPair, ʻIntIntPair` ...)

Although there is a possibility of implementation in the future, it seems that the current situation is that it has not been decided to add it as a standard API.

So, although other paradigms are included, when you want Tuple in Java, an object-oriented language, it seems that the current direction is to make an appropriate abstraction (class).

Use the library

Even though it's Java's policy, there are times when you still want it. You can also use javafx.util.Pair, but the package is javafx or only Pair is prepared. I think it is also a good idea to use an external library because it is not available.

In many cases, it is provided by a library with a relatively strong function orientation.

It's important to note that some libraries also have tuples that are mutable and immutable. In particular, it should be noted that immutable properties cannot be used with APIs that assume mutable properties such as the Accumulator of Collector. Also, depending on the library, classes other than Tuple will be added, so if you introduce it easily, management may become complicated. * 1

Implement

While adding with a library is easy, there is also a problem * 1 so if it is a simple Tuple that does not require functionality, there is also a method to easily implement it with Lombok etc.

@Data(staticConstructor = "of")
public class Pair<A, B> {
    private final A left;
    private final B right;
}

Summary

So, if you want Tuple in Java,

  1. Consider whether it can be expressed in class etc.
  2. If you want the minimum functionality, use your own Tuple instead.
  3. Consider the library for full-scale use

It seems good to think that.

Recommended Posts

A note when you want Tuple in Java
When you want to dynamically replace Annotation in Java8
When seeking multiple in a Java array
When you want to bind InputStream in JDBI3
[Swift] Use nonzeroBitCount when you want popcnt in Swift
A note for Initializing Fields in the Java tutorial
What I learned when building a server in Java
A note about Java GC
Find a subset in Java
A note when examining Javalin
You don't have to write for twice when you make a right triangle in Java
Code to use when you want to process Json with only standard library in Java
Resolve CreateProcess error = 206 when running Java in a Windows environment
Things you often use when doing web development in Java
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
Use JLine when you want to handle keystrokes on the console character by character in Java
When you want to implement Java library testing in Spock with multi-module in Gradle in Android Studio 3
3 Implement a simple interpreter in Java
I created a PDF in Java.
<java> When "EXCEPTION_ACCESS_VIOLATION" appears in awt
A simple sample callback in Java
Get stuck in a Java primer
Do you use Stream in Java?
In Java 10, when you do gradle eclipse and JavaSE-1.10 comes out, ...
ProxyFactory is convenient when you want to test AOP in Spring!
[Note] What I learned in half a year from inexperienced (Java) (1)
[Note] What I learned in half a year from inexperienced (Java) (3)
[Swift] When you want to know if the number of characters in a String matches a certain number ...
Object-oriented design that can be used when you want to return a response in form format
A note on the differences between interfaces and abstract classes in Java
When you want Rails to disable a session for a specific controller only
Pass parameters when debugging vscode java. [Note]
[Creating] A memorandum about coding in Java
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (PowerMockito edition)
Bad habits pointed out in code reviews when Java was a beginner
[JAVA] Project Euler, I got stuck in Q8, so make a note
Java creates a table in a Word document
(Limited to Java 7 or later) I want you to compare objects in Objects.equals
Java creates a pie chart in Excel
What is a class in Java language (1 /?)
What is a class in Java language (2 /?)
Create a TODO app in Java 7 Create Header
The first thing to do when you want to be happy with Heroku on GitHub with Eclipse in Java
Try making a calculator app in Java
I want to get the IP address when connecting to Wi-Fi in Java
Implement something like a stack in Java
Split a string with ". (Dot)" in Java
How to pass a proxy when throwing REST over SSL in Java
Creating a matrix class in Java Part 1
I want to ForEach an array with a Lambda expression in Java
[Java] Eclipse shortcuts that make sense when you go back and forth between source code in a project
What to do when you think you can't do Groovy-> Java in IntelliJ IDEA CE
When you want to notify an error somewhere when using graphql-spring-boot in Spring Boot
A note when I'm addicted to using Docker Hub Vault in server mode
Cause of is not visible when calling a method of another class in java
When using a list in Java, java.awt.List comes out and an error occurs
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (black magic edition)
I made a primality test program in Java
GetInstance () from a @Singleton class in Groovy from Java
Add .gitignore when creating a project in Xcode
Escape processing when creating a URL in Ruby