You seem to need some Maps rather than Lists. Check the Javadoc for Map implementations; the most common is HashMap, but there are sorted, concurrent, deterministically iterable implementations etc. available too.

ListObject: every key corresponds to a String value; key is a string itself

Map<String, String>

ListObject: every key corresponds to an array of Strings; key is a string itself

Map<String, String[]>

(or preferably Map<String, List<String>>)

ListObject: same deal but with two value fields per key.

Map<String, UserDefinedClassWithTwoFields>
Answer from Péter Török on Stack Overflow
🌐
EDUCBA
educba.com › home › software development › software development tutorials › java tutorial › associative array in java
Associative Array in Java | Know How to Create Associative Array in Java?
June 23, 2023 - In simple words, an associative array in java stores the set of elements in a key; the value pair form an associative array is a collection of unique keys and collections of values where each key is associated with one value.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Baeldung
baeldung.com › home › java › core java › using pairs in java
Using Pairs in Java | Baeldung
March 13, 2024 - HashMap, however, is more appropriate for cases that involve storing multiple key-value pairs and require efficient data retrieval and modification capabilities. We can find the Pair class in the javafx.util package. The constructor of this class takes two arguments, a key and its corresponding value:
🌐
CodeSpeedy
codespeedy.com › home › implementation of associative array in java
Implementation of Associative Array in Java - CodeSpeedy
February 16, 2021 - import java.io.*; import java.util.*; public class Fruits{ public static void main(String args[]){ HashMap<String, String> hashmap = new HashMap<String, String>(); hashmap.put("Apple", "Red"); hashmap.put("Orange", "Orange"); hashmap.put("Mango", "Yellow"); Set<Map.Entry<String ,String> > s = hashmap.entrySet(); List<Map.Entry<String ,String>> array = new ArrayList<>(s); for (int i=0; i<array.size(); i++) { System.out.println(array.get(i).getKey() + " is " + array.get(i).getValue()); } } }
🌐
GeeksforGeeks
geeksforgeeks.org › java › implementing-associate-array-in-java
Implementing Associate Array in Java - GeeksforGeeks
July 23, 2025 - An associate array is an abstract datatype like a map that is composed of a (key, value) pair, such that each key-value appears at most once in the collection. Basically, an array with named indexes is known as an associative array or hashes.
🌐
Delft Stack
delftstack.com › home › howto › java › associative array in java
Associative Array in Java | Delft Stack
February 12, 2024 - An associative array is a type of array that stores the set of elements in key and value pairs. It is a collection of keys and values where the key is unique and is associated with one value.
🌐
University of Toronto
individual.utoronto.ca › ssuepaul › API › applicationLogic › utility › KeyValuePair.html
applicationLogic.utility Class KeyValuePair
A List containing arrays, where each array contains the values of this KeyValuePair object. public java.lang.String[] getValuesByKeyName(java.lang.String keyName)
Find elsewhere
🌐
W3Schools
w3schools.com › java › java_hashmap.asp
Java HashMap
A HashMap stores items in key/value pairs, where each key maps to a specific value. It is part of the java.util package and implements the Map interface. Instead of accessing elements by an index (like with ArrayList), you use a key to retrieve ...
🌐
Java Made Easy!
java-made-easy.com › java-collections.html
Java Collections - ArrayList and HashMap
You can have any combination of objects, of course. You use the key to obtain its value. One key corresponds to exactly one value, just like one username corresponds to one password. ... This is fairly tricky. The keySet method returns a Set. Then, that set is turned into an array of Strings by creating a new String that has a length that's the size of the HashMap.
🌐
Quora
quora.com › How-do-I-add-a-key-value-pair-in-ArrayList-in-Java
How to add a key value pair in ArrayList in Java - Quora
Answer (1 of 4): We can’t directly store key value pair. But we can achieve it through creating an ArrayList of user defined class, having two instance variables say key, value. Please refer the below example for understanding better - Otherwise, Hashmap, LinkedHashMap, TreeMap are the the data...
🌐
LabEx
labex.io › tutorials › java-how-to-create-a-map-of-key-value-pairs-in-java-413982
How to create a Map of key-value pairs in Java | LabEx
It is a part of the Java Collections Framework and is widely used in various programming scenarios. A Map is an object that stores a collection of key-value pairs. Each key in the Map must be unique, and it is used to access the corresponding value.
🌐
CodingTechRoom
codingtechroom.com › question › -iterate-array-key-value
How to Iterate Through an Array of Key-Value Pairs to Retrieve a Specific Key’s Value? - CodingTechRoom
For a broader Java reference, Java: The Complete Reference, Twelfth Edition is a useful companion. ... const arrayPairs = [['a', 1], ['b', 2], ['c', 3]]; const getValueByKey = (key) => { const pair = arrayPairs.find(pair => pair[0] === key); return pair ?
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-convert-two-arrays-containing-keys-and-values-to-hashmap-in-java
How to Convert Two Arrays Containing Keys and Values to HashMap in Java? - GeeksforGeeks
July 23, 2025 - These values of the HashMap can be accessed by using their respective keys or the key-value pairs can be accessed using their indexes (of Integer type). HashMap is similar to Hashtable in java.
🌐
CodeGym
codegym.cc › java blog › java objects › pairs in java
Pairs in Java
December 18, 2024 - The Pair in Java is a part of the javafx.util package and is used to represent a pair of values, where one value is associated with a key (key to value relationship). Although it is not a built-in class in the standard Java library, it provides ...
🌐
freeCodeCamp
forum.freecodecamp.org › curriculum help
Create an array of key-value pair arrays from a given object - Curriculum Help - The freeCodeCamp Forum
November 18, 2022 - Trying to understand the code [k, obj[k], here object.keys is taking keys of an object and map them so ‘k’ refers to ‘a’ and ‘b’ which are keys. obj[k] seems we are putting mapped keys ‘k’ in an object ‘obj’ to make a…
Top answer
1 of 2
10

At the simplest level, a key-value pair is just two values, one of which you have designated to be a "key" and the other you have designated to be the "value".

However, it is more common to talk about key-value pairs in the context of a mapping, i.e. a (mathematical) function which maps from a key to the corresponding value or values. Depending on the properties of this mapping, you may constrain the set of key-value pairs. For example, for a 1-to-1 mapping, you need the keys in the set to be unique.


Follow-up questions:

Is this the same as an array?

Well ... an array could be considered as a mapping from a set of indexes (integers) to values. But a mapping is more general. And in Java, arrays have other properties that distinguish them from Maps ... and they have a much simpler, faster, and less memory-hungry implementation.

(Note that in some languages, there is no "array" data type per-se. Instead, the primitive is a "hash" or an "associative array" ... which is a more general map.)

And is a key always a string?

No. A key can be any type. (It is generally a bad idea to use a mutable type as a key, especially if your mapping is implemented using one of the standard Map types in Java. However, even that can work in some circumstances.)

Say a variable of type String points to an object or int variable points to int value. Isn't that variable a "key", and the object a "value"?

No. Or at least, not in a static language like Java. The thing that distinguishes a key-value pair from a variable binding is that the "key" object is data, and hence can take different values. By contrast, a variable's name is hard-wired in the source code of your program: you can't change it at runtime.

(In some dynamic languages, you can create new variables dynamically (at runtime), and for such languages you could argue that variables are key-value pairs in a mapping that represents the scope ... at some point in the program's execution. But Java isn't like that ...)

2 of 2
5

A key-value pair is two values usually connected in such a way that the value is accessed using the key. They are commonly used in various data-structures to provide fast access to values. Check out hashtable data structure for instance (in Java, you can take a look at HashMap or Hashtable)

For example, let's say we have a structure CartoonCharacter holding the mentioned pairs like:

This key-value relationship would look something like:

CartoonCharacter[lastName] = "Bunny";

So if you want to get the value Bunny you would access it through the key lastName.

key = "lastName"
value = CartoonCharacter.get(key)
print (value) // this would print "Bunny"
🌐
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.