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.
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.
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).
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
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;
}
So, if you want Tuple in Java,
It seems good to think that.