Open In App

KeyValue Class in JavaTuples

Improve
Improve
Like Article
Like
Save
Share
Report

A KeyValue is a Tuple from JavaTuples library that deals with only 2 elements – a key and a value. Since this KeyValue is a generic class, it can hold any type of value in it.

Since KeyValue is a Tuple, hence it also has all the characteristics of JavaTuples:  

  • They are Typesafe
  • They are Immutable
  • They are Iterable
  • They are Serializable
  • They are Comparable (implements Comparable<Tuple>)
  • They implement equals() and hashCode()
  • They also implement toString()

Class Declaration 

public final class KeyValue<A, B> extends Tuple 
           implements IValueKey<A>, IValueValue<B>

Class hierarchy 

Object
  ↳ org.javatuples.Tuple
      ↳ org.javatuples.KeyValue<A, B>

Creating KeyValue Tuple 

  • From Constructor:
    Syntax
KeyValue<A, B> kv = new KeyValue<A, B>(value1, value2);

Example

Java




// Below is a Java program to create
// a KeyValue tuple from Constructor
 
import java.util.*;
import org.javatuples.KeyValue;
 
class GfG {
    public static void main(String[] args)
    {
        KeyValue<Integer, String> kv
            = new KeyValue<Integer, String>(Integer.valueOf(1), "GeeksforGeeks");
 
        System.out.println(kv);
    }
}


Output: 

[1, GeeksforGeeks]
  • Using with() method: The with() method is a function provided by the JavaTuples library, to instantiate the object with such values.
    Syntax
KeyValue<type1, type2> kv = KeyValue.with(value1, value2);

Example

Java




// Below is a Java program to create
// a KeyValue tuple from with() method
 
import java.util.*;
import org.javatuples.KeyValue;
 
class GfG {
    public static void main(String[] args)
    {
        KeyValue<Integer, String> kv
            = KeyValue.with(Integer.valueOf(1), "GeeksforGeeks");
 
        System.out.println(kv);
    }
}


Output: 

[1, GeeksforGeeks]
  • From other collections: The fromCollection() method is used to create a Tuple from a collection, and fromArray() method is used to create from an array. The collection/array must have the same type as of the Tuple and the number of values in the collection/array must match the Tuple class.
    Syntax
KeyValue<type1, type2> kv = KeyValue.fromCollection(collectionWith_2_value);
KeyValue<type1, type2> kv = KeyValue.fromArray(arrayWith_2_value);

Example

Java




// Below is a Java program to create
// a KeyValue tuple from Collection
 
import java.util.*;
import org.javatuples.KeyValue;
 
class GfG {
    public static void main(String[] args)
    {
        // Creating KeyValue from List
        List<String> list = new ArrayList<String>();
        list.add("GeeksforGeeks");
        list.add("A computer portal");
        KeyValue<String, String> kv
            = KeyValue.fromCollection(list);
 
        // Creating KeyValue from Array
        String[] arr = { "GeeksforGeeks", "A computer portal" };
        KeyValue<String, String> otherKeyValue
            = KeyValue.fromArray(arr);
 
        System.out.println(kv);
        System.out.println(otherKeyValue);
    }
}


Output: 

[GeeksforGeeks, A computer portal]
[GeeksforGeeks, A computer portal]

Getting Value

The getValue() and getKey() methods can be used to fetch the value and key respectively in a KeyValue Tuple. 

  • getKey():
    Syntax
KeyValue<type1, type2> kv = 
    new KeyValue<type1, type2>(value1, value2);

type2 val1 = kv.getKey();

Example

Java




// Below is a Java program to get
// a KeyValue value
 
import java.util.*;
import org.javatuples.KeyValue;
 
class GfG {
    public static void main(String[] args)
    {
        KeyValue<Integer, String> kv
            = KeyValue.with(Integer.valueOf(1), "GeeksforGeeks");
 
        System.out.println(kv.getKey());
    }
}


Output: 

1
  • getValue():
    Syntax
KeyValue<type1, type2> kv = 
    new KeyValue<type1, type2>(value1, value2);

type2 val1 = kv.getValue();

Example

Java




// Below is a Java program to get
// a KeyValue value
 
import java.util.*;
import org.javatuples.KeyValue;
 
class GfG {
    public static void main(String[] args)
    {
        KeyValue<Integer, String> kv
            = KeyValue.with(Integer.valueOf(1), "GeeksforGeeks");
 
        System.out.println(kv.getValue());
    }
}


Output: 

GeeksforGeeks

Setting KeyValue Value

Since the Tuples are immutable, it means that modifying a value at an index is not possible. Hence, JavaTuples offer setKey(value) and setValue(value) which creates a copy of the KeyValue with a new value according to method used, and returns a new KeyValue object.

  • setKey():
    Syntax
KeyValue<type1, type2> kv = 
    new KeyValue<type1, type2>(value1, value2);

KeyValue<type1, type2> kvNew = kv.setKey(valueNew);

Example

Java




// Below is a Java program to set
// a KeyValue Key
 
import java.util.*;
import org.javatuples.KeyValue;
 
class GfG {
    public static void main(String[] args)
    {
        KeyValue<Integer, String> kv
            = KeyValue.with(Integer.valueOf(1), "GeeksforGeeks");
 
        KeyValue<Integer, String> otherKeyValue
            = kv.setKey(10);
 
        System.out.println(otherKeyValue);
    }
}


Output: 

[10, GeeksforGeeks]
  • setValue():
    Syntax
KeyValue<type1, type2> kv = 
    new KeyValue<type1, type2>(value1, value2);

KeyValue<type1, type2> kvNew = kv.setValue(valueNew);

Example

Java




// Below is a Java program to set
// a KeyValue Value
 
import java.util.*;
import org.javatuples.KeyValue;
 
class GfG {
    public static void main(String[] args)
    {
        KeyValue<Integer, String> kv
            = KeyValue.with(Integer.valueOf(1), "GeeksforGeeks");
 
        KeyValue<Integer, String> otherKeyValue
            = kv.setValue("A computer science portal");
 
        System.out.println(otherKeyValue);
    }
}


Output: 

[1, A computer science portal]

Searching in KeyValue

An element can be searched in a tuple with the pre-defined method contains(). It returns a boolean value whether the value is present or not.

Syntax:

KeyValue<type1, type2> kv = 
    new KeyValue<type1, type2>(value1, value2);

boolean res = kv.contains(value2);

Example:

Java




// Below is a Java program to search
// a value
 
import java.util.*;
import org.javatuples.KeyValue;
 
class GfG {
    public static void main(String[] args)
    {
        KeyValue<Integer, String> kv
            = KeyValue.with(Integer.valueOf(1), "GeeksforGeeks");
 
        // Using contains for True result
        boolean exist = kv.contains("GeeksforGeeks");
 
        // Using contains for False result
        boolean exist1 = kv.contains(4);
 
        System.out.println(exist);
        System.out.println(exist1);
    }
}


Output:

true
false

Iterating through KeyValue

Since KeyValue implement the Iterable<Object> interface. It means that they can be iterated in the same way as collections or arrays.

Syntax:

KeyValue<type1, type2> kv = 
    new KeyValue<type1, type2>(value1, value2);

for (Object item : kv) {
        ...
}

Example:

Java




// Below is a Java program to iterate
// a KeyValue
 
import java.util.*;
import org.javatuples.KeyValue;
 
class GfG {
    public static void main(String[] args)
    {
        KeyValue<Integer, String> kv
            = KeyValue.with(Integer.valueOf(1), "GeeksforGeeks");
 
        for (Object item : kv)
            System.out.println(item);
    }
}


Output:

1
GeeksforGeeks

 



Last Updated : 05 Aug, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads