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.
One thought on “Java Tuple-useful methods”
Comments are closed.