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 - Set Values
JavaTuples - Set Values
A tuple has setAtX() methods to set value at particular index. For example Triplet class has following methods.
setAt0() − set value at index 0.
setAt1() − set value at index 1.
setAt2() − set value at index 2.
Feature
Tuples are immutable. Each setAtX() returns a new tuple which is to be used to see the updated value.
Type of a position of a tuple can be changed using setAtX() method.
Example
Let s see JavaTuples in action. Here we ll see how to set values in a tuple using various ways.
Create a java class file named TupleTester in C:>JavaTuples.
File: TupleTester.java
package com.tutorialspoint; import org.javatuples.Pair; pubpc class TupleTester { pubpc static void main(String args[]){ //Create using with() method Pair<String, Integer> pair = Pair.with("Test", Integer.valueOf(5)); Pair<String, Integer> pair1 = pair.setAt0("Updated Value"); System.out.println("Original Pair: " + pair); System.out.println("Updated Pair:" + pair1); Pair<String, String> pair2 = pair.setAt1("Changed Type"); System.out.println("Original Pair: " + pair); System.out.println("Changed Pair:" + pair2); } }
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
Original Pair: [Test, 5] Updated Pair:[Updated Value, 5] Original Pair: [Test, 5] Changed Pair:[Test, Changed Type]Advertisements