JavaTuples Tutorial
Selected Reading
- JavaTuples - Discussion
- JavaTuples - Useful Resources
- JavaTuples - Quick Guide
- JavaTuples - Decade using Ennead
- JavaTuples - Ennead using Octet
- JavaTuples - Octet using Septet
- JavaTuples - Septet using Sextet
- JavaTuples - Sextet using Quintet
- JavaTuples - Quintet using Quartet
- JavaTuples - Quartet using Triplet
- JavaTuples - Triplet using Pair
- JavaTuples - Pair using Unit
- JavaTuples - KeyValue Class
- JavaTuples - LabelValues Class
- JavaTuples - Decade Class
- JavaTuples - Ennead Class
- JavaTuples - Octet Class
- JavaTuples - Septet Class
- JavaTuples - Sextet Class
- JavaTuples - Quintet Class
- JavaTuples - Quartet Class
- JavaTuples - Triplet Class
- JavaTuples - Pair Class
- JavaTuples - Unit Class
- JavaTuples - Checking Elements
- JavaTuples - Iteration
- JavaTuples - Conversion
- JavaTuples - Remove Elements
- JavaTuples - Add Elements
- JavaTuples - Set Values
- JavaTuples - Get Values
- JavaTuples - Create Tuples
- JavaTuples - Environment Setup
- JavaTuples - Overview
- JavaTuples - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
JavaTuples - Checking Elements
JavaTuples - Checking Elements
Each tuple provides utipty methods to check their elements in similar fashion as collection.
contains(element) − checks if element is present or not.
containsAll(collection) − checks if elements are present or not.
indexOf(element) − returns the index of first element if present otherwise -1.
lastIndexOf(element) − returns the index of last element if present otherwise -1.
Pair<String, Integer> pair = Pair.with("Test", Integer.valueOf(5)); boolean isPresent = pair.contains("Test");
Example
Let s see JavaTuples in action. Here we ll see how to check elements in a tuple.
Create a java class file named TupleTester in C:>JavaTuples.
File: TupleTester.java
package com.tutorialspoint; import java.util.List; import org.javatuples.Quartet; pubpc class TupleTester { pubpc static void main(String args[]){ Quartet<String, Integer, String, String> quartet = Quartet.with( "Test1", Integer.valueOf(5), "Test3", "Test3" ); System.out.println(quartet); boolean isPresent = quartet.contains(5); System.out.println("5 is present: " + isPresent); isPresent = quartet.containsAll(List.of("Test1", "Test3")); System.out.println("Test1, Test3 are present: " + isPresent); int indexOfTest3 = quartet.indexOf("Test3"); System.out.println("First Test3 is present at: " + indexOfTest3); int lastIndexOfTest3 = quartet.lastIndexOf("Test3"); System.out.println("Last Test3 is present at: " + lastIndexOfTest3); } }
Verify the result
Compile the classes using javac compiler as follows −
C:JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java
Now run the TupleTester to see the result −
C:JavaTuples>java -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester
Output
Verify the Output
[Test1, 5, Test3, Test3] 5 is present: true Test1, Test3 are present: true First Test3 is present at: 2 Last Test3 is present at: 3Advertisements