Java Tuple-useful methods

Java Tuple-useful methods

Creating and Converting Tuples

with()- another way of instantiating a tuple. For example:

TupleType<Type1,Type2, etc> tupleName = TupleType.with( Type1Value, Type2Value, etc);

so its a simple way to create a tuple.

You can also do something similar with fromIterable(), fromCollection() and fromArray(), So it would be like this:

TupleType<Type1,Type2, etc> tupleName = TupleType.fromIterable(NameOfIterable);

TupleType<Type1,Type2, etc> tupleName = TupleType.fromArray(NameOfArray);

TupleType<Type1,Type2, etc> tupleName = TupleType.fromCollection(NameOfCollection);

or(only iterable has the ability to do this I believe)

TupleType<Type1,Type2, etc> tupleName = TupleType.fromIterable(NameOfIterable, index#);

The number of elements in the array, list or whatever else should be the same as the iterable (includes lists and arrays), unless you are using the second version of the iterable method(the with index#).

There are also conversion methods of a tuple to something else specifically toArray(),toList() and toString().Which work like:

Object[ ] arrayName = tupleName.toArray();

List<Object> listName = tupleName.toList();

String stringName= tupleName.toString();

I don’t believe you have to use object if all the values in the tuple are of the same datatype, but the type does have to match the type of all the elements in the tuple and Object is the only datatype that guarantees that it will.

Getting and setting Values

You can get a value in a tuple with getValue(), and getValueX(). An example of these are:

dataype variableName= TupleType.getValue#();

dataype variableName= TupleType.getValue(#);

These methods return a value so keep that in mind. The # stands for the index number of the element of the tuple you are getting.

You can set a value in a tuple with the setAtX(), which work similarly:

TupleType<Type1,Type2, etc> modTupleName = tupleName.setValue#(value);

Tuples are immutable so using these methods will return a new tuple so you should use it when modifying a tuple. Again # represents the index number and value represents what you are putting at that index number.

Adding and removing elements

So to add a element to a tuple you would use the add() or addAtX(). They are formatted like:

TupleType<Type1,Type2, etc> addTupleName = tupleName.addAt#(value);

TupleType<Type1,Type2, etc> addTupleName = tupleName.add(value);

So, add() will add the element to the end so it would be the last index. While addAtX() would add the element at the index of #. So, the index of what was added would be #. You can add multiple elements at the same time, but you have to make sure the TupleType matches the total amount of elements that will be in the Tuple. If you are adding a tuple to a tuple its the number of elements from tuple1 + number of elements of tuple2. Remember that these methods create a new tuple.

To remove a element use the removeFromX(). Which works like:

TupleType<Type1,Type2, etc> removeTupleName = tupleName.removeFrom#();

So, it basically deletes the element at index #. It has no parameters, and again it returns a tuple and the elements need to match what it will have when that element is removed.

Other

In order to figure out how many elements are in a tuple use getSize(). Which is formatted as :

int nameOfVar= tupleName.getSize();

Which has no parameters and returns a int that is the number of elements in the tuple.

To find the index of something use indexOf() or lastIndexOf(). Which is formatted like:

int nameOfVar= tupleName.indexOf(Value);

int nameOfVar= tupleName.lastIndexOf(Value);

So indexof() gives you the index of the first element that matches the value. lastIndexOf() finds the last element that matches the value.

There are also methods like contains(), containsAll(), equals(), which give a boolean and check to see if a tuple has something or if a tuple equals another tuple. I might make a separate post on these or add them here at a later time.

Java Tuple

Java Tuple

Tuples are a collection of objects of different types. The different types bit is what makes them different from arrays and lists. This would mean that you could have one object(with that tuple being that one object) contain all of someones identifying information, like age(int), gender(string), name(string), etc.

Ex:

Triplet<String, String, Integer> = new Triplet<String, String,Integer>(“Julia”, “female”, 20) ;

or in generalized form:

SpecificTupleType<ObjectType1,ObjectType2,etc> = new SpecificTupleType<ObjectType1,ObjectType2,etc>(Object1,Object2,etc);

That’s how you make a tuple by the way. So there are actually ten kinds of tuples, that vary bu how many objects you can have in a tuple, so for one object it would be a unit, two pair, three triplet, four quartet, etc. So the max number of objects you can have in a Tuple is 10. Also Java doesn’t have the JavaTuple data structure by default so you will need to download the JavaTuple library onto whatever platform you are using to make the program(EX.: Eclipse).

Some advantages to tuples are that they are immutable(which means you can’t change them after you have created them), they are type safe, they are serializable(which means that they can be put in the file system and can therefore be put into other applications) they can be used in iterations and they have things like toString() and equals() built into them.

Usefull Methods(will be updated with the rest of the information soon)

It’s possible I will also do one on the LabelValue and KeyValue classes.(There like another version of the pair tuple from my understanding).

Some sites to go to for more information are the tuple library which is already linked, geekforGeeks, tutorialspoints, and howtodoinjava.