JavaTuples – Remove Elements

JavaTuples - Remove Elements

This topic is about JavaTuples – Remove Elements.

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.Adglob;
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/Adglob/TupleTester.java

Now run the TupleTester to see the result −

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

Output

Verify the Output

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

In this topic we learned about JavaTuples – Remove Elements. To know more, Click Here.

This Post Has One Comment

Leave a Reply