JavaTuples - Remove Elements


Advertisements

A tuple has removeAtX() methods to remove value at particular index. For example Triplet class has following methods.

  • removeAt0() − remove value at index 0 and return the resulted tuple.

  • removeAt1() − remove value at index 1 and return the resulted tuple.

  • removeAt2() − remove value at index 2 and return the resulted tuple.

Removing an element returns a new tuple.

Example

Let's see JavaTuples in action. Here we'll see how to remove value in a tuple.

Create a java class file named TupleTester in C:\>JavaTuples.

File: TupleTester.java

package com.howcodex;
import org.javatuples.Pair;
import org.javatuples.Triplet;
public class TupleTester {
   public static void main(String args[]){
      Triplet<String, Integer, String> triplet = Triplet.with(
         "Test1", Integer.valueOf(5), "Test2"
      );
      Pair<String, Integer> pair = triplet.removeFrom2();
      System.out.println("Triplet:" + triplet);
      System.out.println("Pair: " + pair);  
   }
}

Verify the result

Compile the classes using javac compiler as follows −

C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/howcodex/TupleTester.java

Now run the TupleTester to see the result −

C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.howcodex.TupleTester

Output

Verify the Output

Triplet:[Test1, 5, Test2]
Pair: [Test1, 5]
Advertisements