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());