Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplist way to create a tuple in java? [closed]

Tags:

java

tuples

Is there a simple way to create a 2 element tuple in java? I'm thinking of making a class and declaring the variables as final. Would this work?

like image 786
omega Avatar asked Jan 29 '13 03:01

omega


2 Answers

This is as simple as it gets:

public class Pair<S, T> {
    public final S x;
    public final T y;

    public Pair(S x, T y) { 
        this.x = x;
        this.y = y;
    }
}
like image 83
duffymo Avatar answered Sep 21 '22 12:09

duffymo


Yes. Best practices would be to make the fields private and provide getters for them.

For many people (including [most of?] the language designers), the idea of a tuple runs counter to the strong typing philosophy of Java. Rather than just a tuple, they would prefer a use-case-specific class, and if that class only has two getters and no other methods, so be it.

like image 27
yshavit Avatar answered Sep 18 '22 12:09

yshavit