mercredi 11 mars 2015

Java Tuple Without Creating Multiple Type Parameters



Is there any way to create a Tuple in Java, without having to create multiple classes?


For example, it is possible to make a different class for every different type of Tuple, each with a different number of Type Parameters :



public class SingleTuple<T>{}
public class DoubleTuple<T1, T2>{}
public class TripleTuple<T1, T2, T3>{}
public class QuadraTuple<T1, T2, T3, T4>{}
public class PentaTuple<T1, T2, T3, T4, T5>{}


And it's also possible to create a Tuple object without any Type Parameters by just doing this:



public class Tuple{

private Object[] objects;

public Tuple(Object... objects){
this.objects = objects;
}

public Object get(int index){
return this.objects[index];
}
}


Except if this is used, all of the objects would have to be casted to their correct Subclass once they are taken out of the Tuple, which makes the above class like an ArrayList<Object>, but with less features.


Is there any way to just create one singular class, and have multiple Type Parameters without defining all of them, like this?



public class Tuple<T...>{

}



Aucun commentaire:

Enregistrer un commentaire