I am translating a program from C# to Java. In the C# code, the developer uses Tuple. I need to translate this C# code into Java code. Therefore, does Java have an equivalent variable type to C#'s Tuple?
Due to type erasure, there is no way in Java to have exact mirrors of the various Tuple
classes in .NET. However, here is a BSD-licensed implementation of Tuple2
and Tuple3
for Java, which mirror the Tuple<T1, T2>
and Tuple<T1, T2, T3>
types from .NET.
- Tuple.java (static methods to construct tuples with type inference)
- Tuple2.java
- Tuple3.java
One cool thing you can do in Java but not C# is this:
class Bar extends Foo { }
...
Tuple2<? extends Foo, ? extends Foo> tuple = Tuple.create(new Bar(), new Bar());
In C#, you would have to use casts instead:
Tuple<Foo, Foo> tuple = Tuple.Create((Foo)new Bar(), (Foo)new Bar());
Tuple.Create(new Bar(), new Bar())
will return an instance of Tuple<Bar, Bar>
- Tuple<Foo, Foo> tuple = Tuple.Create(new Bar(), new Bar())
will not compile, as Tuple doesn't support covariance. To make it work in c#, you'd need to use the constructor instead - Tuple<Foo, Foo> tuple = new Tuple<Foo, Foo>(new Bar(), new Bar())
–
Eda Probably a bit off topic, but have you considered Scala in a JVM environment? It has tuple support + a few other nice features that you might miss from C#.
There seems to be a pretty decent looking Tuple library at https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/tuple/package-summary.html. Three elements is the largest it can be, but if you need a Tuple larger than three, you should probably give up and define a class.
it's quite old topic, but i will leave this what i recently used during converting C# tuples(Tuple) in Java project - you can simply use org.javatuples lib - https://www.javatuples.org/, which will gives the same behavior to storing data.
Simple example below for people which will lookin for this later:
C#
var tupleCs = new Tuple<string, string, string>("value1","value2","value3");
Console.Writeline(tupleCs.Item1); => value1
Console.Writeline(tupleCs.Item2); => value2
Console.Writeline(tupleCs.Item3); => value3
the same in Java
Triplet tupleJava = new Triplet("value1","value2","value3");
System.out.println(tupleJava.getValue0()); => value1
System.out.println(tupleJava.getValue1()); => value2
System.out.println(tupleJava.getValue2()); => value3
I was in need of Tuple equivalent in my android project as well, but didn't found any native solution in Java. But surprisingly there is Pair.class
in android's util which is exact what I was searching for.
So Android developers, who came across this question, use Pair.class
in android.util
package.
P.S. I'm sad to accept that there are a lot of things that Java is far behind from other languages already.
There might be implementations, but not in the JDK Library as far as I know. See here for an example of your own tuple: Using Pairs or 2-tuples in Java
When you're using Spring Boot with WebFlux it comes with Project Reactor and that provides Tuples
classes. The advantage of the Project Reactor implementation is they have a clean way of generating the Tuples using the .of
operator
e.g.
Tuples.of("A", 1, myObject);
I usually use it for date ranges as shown in this example to find the starting date of a range given a processing date.
return schedules.stream()
.map(s -> Tuples.of(s.getStartDate(), s.getStopDate().orElse(LocalDate.MAX)))
.filter(range -> (!processingDate.isBefore(range.getT1())) &&
processingDate.isBefore(range.getT2()))
.findAny()
.map(Tuple2::getT1);
There's no need to memorize types like Pair, Triple, Decade, etc.
This is a rather old question, but here is what I ended up developing to be able to use tuples in the post Java 8 world with all of its functional tools and the nicer fluent language: https://github.com/mmnaseri/tuples4j/
It is already available in Maven Central, too, so it is very easy to get a hold of.
At least a 2-tupel exists in Java, right from the beginning: java.util.Map.Entry<K,V>
! Ok, it is an interface only …
But since Java 9, there is the static method java.util.Map.entry()
that allows the creation of instances of that interface on the fly (in fact, instances of a hidden class implementing that interface, but who cares about that little detail?).
I would not like to use that in the definition of a public API that is not dealing with Collections, but for an internal utility class … why not? The class already exists in the JRE, no additional external dependency, and I can live with the fact that the elements are named "key" and "value" instead of "item1", "item2", …
I am not a C# developer but given the documentation I just looked at about Tuple in C# I can't imagine what you would/could/SHOULD use something like this.
var population = new Tuple<string, int, int, int, int, int, int>(
"New York", 7891957, 7781984,
7894862, 7071639, 7322564, 8008278);
Looking at this code I have no earthly clue what those values represent and can't imagine why you wouldn't use a class.
As I see it, this just represents poor encapsulation.
However, if you want a Tuple in Java, the only one I know of is
Map.Entry<K,V>
And it's really only appropriate for use in Maps
ComboBox
. You could have one item represent the object itself, another for display value (DisplayMember
) and another for SelectedValue
(there are times when you want this different than SelectedItem
). But wait, there anonymous types in C#. So for a simple CSV reader Tuple
would've been perfect, if I know the type of each data column. Because Java sucks balls, I have to create a class for each scenario. The items borrowed from C# also suck balls in Java... –
Kristopherkristos © 2022 - 2024 — McMap. All rights reserved.