Have two arrays of equal size

String [] keys = new String [5];
String [] values = new String [5];

keys [0] = "name";
values [0] = "Fred";

String getValue (String key) {

   // loop around keys array to get index of key

   return values [index];
}
Answer from Scary Wombat on Stack Overflow
🌐
Delft Stack
delftstack.com › home › howto › java › java key value pair
How to Implement Key Value Pair in Java | Delft Stack
February 12, 2024 - 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.
🌐
Dev.java
dev.java › learn › api › collections-framework › maps
Using Maps to Store Key Value Pairs - Dev.java
September 14, 2021 - This hierarchy is very simple and looks like the Set hierarchy, with SortedSet and NavigableSet. Indeed, a SortedMap shares the same kind of semantics as the SortedSet: a SortedMap is a map that keeps its key-value pairs sorted by their keys.
🌐
How to do in Java
howtodoinjava.com › home › collections framework › java hashmap › complete guide to java hashmap (with examples)
Complete Guide to Java HashMap (with Examples)
December 5, 2024 - The HashMap, part of the Java Collections framework, is used to store key-value pairs for quick and efficient storage and retrieval operations.
🌐
Dev.java
dev.java › learn › using-maps-to-store-key-value-pairs
Using Maps to Store Key Value Pairs
Copyright © 2026 Oracle and/or its affiliates. All rights reserved. Terms of Use | Privacy | Trademarks
🌐
Baeldung
baeldung.com › home › java › core java › using pairs in java
Using Pairs in Java | Baeldung
March 13, 2024 - For example, we might use a HashMap to store multiple key-value pairs, like associating the key 1 with the value “One,” the key 2 with “Two,” and the key 3 with “Three.” In this case, the HashMap allows us to retrieve any value by ...
🌐
W3Schools
w3schools.com › java › java_hashmap.asp
Java HashMap
A HashMap can store many different combinations, such as: ... import java.util.HashMap; // Import the HashMap class HashMap<String, String> capitalCities = new HashMap<>(); Now you can use methods like put() to add key/value pairs, get() to ...
Find elsewhere
Top answer
1 of 9
11

here is the sample code for saving key-values with hashmap :

HashMap<String, String> data = new HashMap<String, String>();
data.put("key", "value");

or if the order matters to you use linkedHashMap :

HashMap<String, String> data = new LinkedHashMap<>();

the usage is the same;)

2 of 9
7

Data Structure Logic:

Hashmaps are used to access the content via its key.

In this sample, I guess you are scanning 'tuples' one by one and each tuple is different from other, so you won't need to access an old tuple via it's key.

So here, I suggest you to create the model class suitable for the key-value pairs and store them in a list. You can push that list when you are done.

Sample model for tuple:

public class KeyValuePair {
    private String key;
    private String value;
    public KeyValuePair(String key, String value) {
        this.key = key;
        this.value = value;
    }
}

Sample List to store:

List<KeyValuePair> keyValuePairList = new ArrayList<>();
keyValuePairList.add(new KeyValuePair("0", "816444014066"));
keyValuePairList.add(new KeyValuePair("1", "747083010945"));
keyValuePairList.add(new KeyValuePair("2", "816444010969"));

Storing:

If you cannot pass the data between activities, check out SQLite. You can store the data in SQLite and fetch it when needed. It is an offline database works on Android devices. You can delete the data when pushed to upstream.

Edit:

If the keys are the orders, you can simply use a String List like this:

List<String> keyValuePairList = new ArrayList<>();
keyValuePairList.add("816444014066");
keyValuePairList.add("747083010945");
keyValuePairList.add("816444010969");
🌐
MakeUseOf
makeuseof.com › home › programming › learn how to use the java hashmap data structure
Learn How to Use the Java HashMap Data Structure
July 14, 2022 - A HashMap (or a HashTable) is a data structure that allows quick access to data using key-value pairs. The Java HashMap class extends the AbstractMap class and implements the Map interface, which gives it access to a lot of operations. HashMaps have two type parameters—K and V, where K stores the keys and V stores the values in each HashMap...
🌐
TutorialsPoint
tutorialspoint.com › article › java-program-to-create-a-hashmap-and-add-key-value-pairs
Java program to create a HashMap and add key-value pairs
September 5, 2024 - In the program, we first import the java.util.* package to access the HashMap and other utilities. A HashMap named hm is created to store key-value pairs like "Bag" with a value of 1100. The put() method is used to add these pairs to the map.
🌐
Vultr
docs.vultr.com › java › standard library › java › util › hashmap › put()
Java HashMap put() - Insert Key-Value Pair
September 27, 2024 - The put() method in Java's HashMap class plays a crucial role in adding entries to a hash map, allowing you to store key-value pairs efficiently. This method is fundamental for handling associative arrays in Java, where the keys are unique.
🌐
How to do in Java
howtodoinjava.com › home › pairs in java
Java Pair class - key value pair data structure in Java - HowToDoInJava
October 1, 2022 - A pair provide a convenient way of associating a simple key to value. In Java, maps are used to store key-value pairs.
🌐
Tutorjoes
tutorjoes.in › Java_example_programs › create_a_hashmap_to_store_key_value_pair_in_java
Write a Java program to create a HashMap to store Key/Value pair
This program creates a HashMap named "stu" that stores key-value pairs, where the keys are of type Integer and the values are of type String. The program then adds 10 key-value pairs to the HashMap using the put() method. Finally, the program prints the entire HashMap using the println() method, ...
🌐
CodingTechRoom
codingtechroom.com › question › store-key-value-pairs-java
How to Store Key-Value Pairs in Java: A Comprehensive Guide - CodingTechRoom
In Java, storing key-value pairs can be accomplished using various data structures, primarily Maps, such as HashMap, LinkedHashMap, and TreeMap. Each structure offers unique characteristics that suit different use cases, including performance ...