Implement Key Value Pair in Java
-
Implement Key Value Pair Using
HashMap
in Java -
Implement Key Value Pair Using the
Map.Entry
in Java -
Implement Key Value Pair Using the
AbstractMap.SimpleEntry
Class in Java -
Implement Key Value Pair Using
Map.entry
in Java -
Implement Key Value Pair Using
AbstractMap.SimpleImmutableEntry
in Java -
Implement Key Value Pair Using
Maps.immutableEntry
in Java -
Implement Key Value Pair Using
Properties
Class in Java

This tutorial introduces how to implement key-value pairs in Java.
In Java, to deal with the key-value
pair, the Map
interface and its implementation classes are used. We can use classes such as HashMap
and TreeMap
to store data into the key-value
pair. Apart from these built-in classes, we can create our own class that can hold the key-value
pair.
Here, we will use HashMap
, user-defined class, AbstractMap
, Map.entry()
, AbstractMap.SimpleImmutableEntry()
and Properties
, etc. Let’s take a close look at the examples.
Implement Key Value Pair Using HashMap
in Java
Java’s Map interface in the Collection
framework can be used to store data into the key-value
pair. Here, we use the HashMap
class to store string type key-value pairs. See the example below.
import java.util.HashMap;
import java.util.Map;
public class SimpleTesting extends Thread{
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put("name", "Rohan");
map.put("sname", "Kumar");
System.out.println(map);
}
}
Output:
{sname=Kumar, name=Rohan}
Implement Key Value Pair Using the Map.Entry
in Java
Here, we use the Map.Entry
interface to create a custom class that will hold data in key-value pairs. We create a class, Student
, with two instance variables to hold key and value pair. We also created getters and setters methods to set values for each instance of this class. See the example below.
import java.util.Map;
class Student<K, V> implements Map.Entry<K, V> {
private final K key;
private V value;
public Student(K key, V value) {
this.key = key;
this.value = value;
}
@Override
public K getKey() {
return key;
}
@Override
public V getValue() {
return value;
}
@Override
public V setValue(V value) {
V old = this.value;
this.value = value;
return old;
}
}
public class SimpleTesting extends Thread{
public static void main(String[] args) {
Student<String, String> student = new Student<>("name","Rohan");
String key = student.getKey();
String value = student.getValue();
System.out.println("{"+key+":"+value+"}");
}
}
Output:
{name:Rohan}
Implement Key Value Pair Using the AbstractMap.SimpleEntry
Class in Java
Here, we use the AbstractMap
class to implement key-value pair. The getKey()
and getValue()
methods are used to get key and value respectively. See the below example.
import java.util.AbstractMap;
import java.util.Map;
public class SimpleTesting extends Thread{
public static void main(String[] args) {
Map.Entry<String,String> entry =
new AbstractMap.SimpleEntry<String, String>("name", "Rohan");
String key = entry.getKey();
String value = entry.getValue();
System.out.println("{"+key+":"+value+"}");
}
}
Output:
{name:Rohan}
Implement Key Value Pair Using Map.entry
in Java
We can use Map.entry
to store data into key and value pair. Here, we use the Entry
interface and its methods getKey()
and getValue()
to get key and value respectively. See the below example.
import java.util.Map;
import java.util.Map.Entry;
public class SimpleTesting extends Thread{
public static void main(String[] args) {
Entry<String, String> entry = Map.entry("name", "Rohan");
String key = entry.getKey();
String value = entry.getValue();
System.out.println("{"+key+":"+value+"}");
}
}
Output:
{name:Rohan}
Implement Key Value Pair Using AbstractMap.SimpleImmutableEntry
in Java
We can use SimpleImmutableEntry
to create an immutable set of key-value pairs. See the below example.
import java.util.AbstractMap;
import java.util.Map.Entry;
public class SimpleTesting extends Thread{
public static void main(String[] args) {
Entry<String, String> entry = new AbstractMap.SimpleImmutableEntry<>("name","Rohan");
String key = entry.getKey();
String value = entry.getValue();
System.out.println("{"+key+":"+value+"}");
}
}
Output:
{name:Rohan}
Implement Key Value Pair Using Maps.immutableEntry
in Java
Here, we use Map.immutableEntry
to create key-value pair in Java. We use getKey()
and getValue()
methods to get key and value respectively.
import java.util.AbstractMap;
import java.util.Map;
import java.util.Map.Entry;
import com.google.common.collect.Maps;
public class MainClass extends Thread{
public static void main(String[] args) {
Map.Entry<String, String> entry = Maps.immutableEntry("name", "Rohan");
String key = entry.getKey();
String value = entry.getValue();
System.out.println("{"+key+":"+value+"}");
}
}
Output:
{name:Rohan}
Implement Key Value Pair Using Properties
Class in Java
The Properties
class of Java collections can be used to store data into key-value pairs. The getProperty()
method of the Properties
class returns the value associated with the key. See the below example.
import java.util.Properties;
public class MainClass extends Thread{
public static void main(String[] args) {
Properties props = new Properties();
props.setProperty("name", "Rohan"); // (key, value)
String value = props.getProperty("name");
System.out.println("{name:"+value+"}");
}
}
Output:
{name:Rohan}
Related Article - Java Collection
- Thread-Safe Collections in Java
- Convert Collection to List in Java
- Iterate Over Each Element of Map in Java