. Advertisement .
..3..
. Advertisement .
..4..
Is it possible to add key value pair Java? The answer is Yes; even better, there are many methods to do so. Keep scrolling for our helpful pointers.
How to Add Key Value Pair Java
Method 1. Use Properties Class
Properties classes in Java collections help store your data into different key-value pairs. Also, their getProperty()
command can send back values associated with these keys. The example below will clear things up for you.
import java.util.Properties;
public class MainClass extends Thread{
public static void main(String[] args) {
Properties props = new Properties();
props.setProperty("names", "Johan"); // (key, value)
String value = props.getProperty("name");
System.out.println("{names:"+value+"}");
}
Output
{names:Johan}
Method 2. Use Maps.immutableEntry
In this case, we suggest you use the Map.immutableEntry
to establish a Java key-value pair. Count on getValue()
and getKey()
approaches to receive the value and key 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("names", "Johan");
String key = entry.getKey();
String value = entry.getValue();
System.out.println("{"+key+":"+value+"}");
}
}
The output will still be the same:
{names:Johan}
Method 3. Use AbstractMap.SimpleImmutableEntry
This command is another great method to help you produce immutable sets of key-value Java pairs. Check out the following 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<>("names","Johan");
String key = entry.getKey();
String value = entry.getValue();
System.out.println("{"+key+":"+value+"}");
}
}
Output:
{names:Johan}
Method 4. Use Map.Entry
It’s possible to use Map.Entry
for data storage in value and key pair. Specifically, we adopt the interface Entry – and its getValue()
and getKey()
methods – to obtain value and key separately.
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("names", "Johan");
String key = entry.getKey();
String value = entry.getValue();
System.out.println("{"+key+":"+value+"}");
}
}
Output:
{names:Johan}
Method 5. Use LinkedHashmap
LinkedHashMap entails Hash tables and listed link implementation for Map interfaces. The key-value order depends on how the keys got inserted into your map. Should you decide to reinsert the key, your insertion order will not affect the outcome.
The steps are fairly simple:
- Establish two variables called Value and Key
- Accept user input in Value and in Key
- Adopt
Put()
to insert the pair within LinkedHashmap
Here is the example:
// Java Program to add key-value
// pairs to LinkedHashMap
import java.util.*;
public class Main {
public static void main(String[] args)
{
// create an instance of LinkedHashMap
LinkedHashMap<Integer, Integer> map = new LinkedHashMap<Integer, Integer>();
int num, key, val;
num = 2;
for (int i = 0; i < num; i++) {
// Taking inputs from user
key = i + 1;
val = key * 10;
// Add mappings using put method
map.put(key, val);
}
// Displaying key
System.out.println("Keys: " + map.keySet());
// Displaying value
System.out.println("Values: " + map.values());
// Displaying key-value pair
System.out.println("Key-Value pairs: " + map.entrySet());
}
}
Output:
Keys: [1, 2]
Values: [10, 20]
Key-Value pairs: [1=10, 2=20]
Method 6. Use ArrayList
ArrayList is not a conventional method, as it cannot help you store key-value pairings. Still, it’s possible to create one ArrayList of defined classes, given two variables of instances (key and value).
public class solution
{
static class pair {
Integer keys;
Integer values;
Pair (Integer key, Integer value)
{
this.key = key;
this.value = value;
}
}
public static void main (String [] arg)
{
ArrayList <Pair> a = new ArrayList <Pair> ();
Pair p = new Pair (1, 501);
Pair q = new Pair (2, 602)
a.add(p);
a.add(q);
}
}
Output:
[1 = 501, 2= 602]
Conclusion
This guide has delivered different solutions for adding key value pair Java. Choose one that suits you and your program best! Aside from the methods above, you can also add key-value pairs to a dictionary; check this Q&A section for more guidance.
Leave a comment