Create a Pair class, that holds both the Integer and the String and then add multiple Pair objects to a List, which will be shuffled.

public class Pair {
  private Integer integer;

  private String string;

  //accessors
}

Then:

List<Pair> list = new ArrayList<Pair>();
//...add some Pair objects to the list
Collections.shuffle(list);
Answer from Konstantin Yovkov on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java collections › java map › get values and keys as arraylist from a hashmap
Get Values and Keys as ArrayList From a HashMap | Baeldung
March 7, 2025 - Map: k1 -> v1 k2 -> v2 k3 -> v3 index : 0, 1, 2 KeyList : k1, k2, k3 ValueList: v1, v2, v3 · The second scenario is relatively straightforward. Here, we aim to extract a key list and a value list from the provided map without retaining concern for the initial key-value pair associations.
Discussions

java - Create a list of key value objects - Stack Overflow
The answer on this thread: Java HashMap associative multi dimensional array can not create or add elements ... Frankly, I'm not sure how your answer pertains to the question. (I never down voted btw) 2013-08-24T03:59:46.837Z+00:00 ... It pertains to the question because it contains an example of how to solve this problem. The example shows how to create a HashMap and put multiple key-value ... More on stackoverflow.com
🌐 stackoverflow.com
data structures - What will store a Key-Value list in Java or an alternate of C# IDictionary in Java? - Stack Overflow
I am new to java development, I am from C# .net, developing android application. I am looking for Key-Value list to use in Java same as IDictionary in C#. Thanks More on stackoverflow.com
🌐 stackoverflow.com
January 5, 2015
Java convert List of key/value strings to Map - Stack Overflow
I have a List of strings which are actually keys and values: "key1", "value1", "key2", "value2", ... (every odd item is a key, every even — value). How can I convert it to Map like this "key1" -> " More on stackoverflow.com
🌐 stackoverflow.com
March 18, 2016
java - What's the right data structure to combine iteration of key-value pairs, and iteration of values - Software Engineering Stack Exchange
I'm looking for a pre-existing or easy-to-build data structure available in Java, which can do these 2 things efficiently: Fetch all the values stored in the collection quickly. Iterate over the data structure to fetch each key and its value. Right now, I'm using a Pair. More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
November 2, 2022
🌐
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...
🌐
W3Schools
w3schools.com › java › java_hashmap.asp
Java HashMap
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 its associated value.
🌐
Dev.java
dev.java › learn › using-maps-to-store-key-value-pairs
Using Maps to Store Key Value Pairs
Watch & Listen · FAQ · Oracle University · Your first Java code · Using IDEs · Java Platform Evolution · Using JDK Preview Features · Language Basics · Object Oriented Programming · Generics, Lambda, and Pattern Matching · Annotations and Exceptions ·
🌐
Stack Abuse
stackabuse.com › java-how-to-get-keys-and-values-from-a-map
Java: How to Get Keys and Values from a Map
November 24, 2020 - Key (name) is: Billy, Value (age) is: 21 Key (name) is: David, Value (age) is: 24 Key (name) is: John, Value (age) is: 35 Key (name) is: Jane, Value (age) is: 19 · Java 8 introduces us to the forEach() method for collections. It accepts a BiConsumer<? super K, ?
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-convert-all-linkedhashmap-key-value-pairs-to-list-in-java
How to Convert all LinkedHashMap Key-Value pairs to List in Java? - GeeksforGeeks
July 23, 2025 - // Java program to Convert all LinkedHashMap // Key-Value pairs to List import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; public class Sias { public static void main(String[] args) { // create LinkedHashMap LinkedHashMap<Integer, String> lhmap = new LinkedHashMap<Integer, String>(); // add elements in LinkedHashMap lhmap.put(1, "One"); lhmap.put(2, "Two"); lhmap.put(3, "Three"); lhmap.put(4, "Four"); lhmap.put(5, "Five"); // Create List 1 that store keys List<Integer> list1 = new ArrayList<Integer>(lhmap.keySet()); // display List 1 System.out.println("List 1 - " + list1); // Create List 2 that store values List<String> list2 = new ArrayList<String>(lhmap.values()); // display List 1 System.out.println("List 2 - " + list2); } } Output ·
🌐
GeeksforGeeks
geeksforgeeks.org › java › keyvalue-class-in-java-tuples
KeyValue Class in JavaTuples - GeeksforGeeks
July 11, 2025 - KeyValue<type1, type2> kv = KeyValue.fromCollection(collectionWith_2_value); KeyValue<type1, type2> kv = KeyValue.fromArray(arrayWith_2_value); ... // 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); } }
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › hashmap-in-java-with-example
HashMap in Java With Examples
September 19, 2022 - HashMap is a Map based collection class that is used for storing Key & value pairs, it is denoted as HashMap<Key, Value> or HashMap<K, V>. HashMap in java, is similar to the Hashtable class except that it is unsynchronized and permits nulls(null values and null key).
🌐
Java Made Easy!
java-made-easy.com › java-collections.html
Java Collections - ArrayList and HashMap
One key corresponds to exactly one value, just like one username corresponds to one password.
🌐
Coderanch
coderanch.com › t › 568613 › java › Adding-key-pairs-HashMap-ArrayList
Adding key/value pairs from a HashMap in to an ArrayList (Beginning Java forum at Coderanch)
February 26, 2012 - For the desired output we can assume that the productAttribute parameter value is "Computer". Current output of the addProduct ArrayList: Desired output of the addProduct ArrayList: I can't seem to figure out what I'm doing wrong. Any help would be greatly appreciated. ... So you have a map which maps a String to a list of Strings. And if I understand your example right, you want to mutate that into a list of lists of Strings, where each of the inner lists comprises a key of the map followed by the list it's mapped to?
🌐
Mrhaki
blog.mrhaki.com › 2021 › 01 › java-joy-transform-stream-of-strings-to.html
Java Joy: Transform Stream Of Strings To List Of Key Value Pairs Or Map - Messages from mrhaki
January 27, 2021 - */ @Override public Function<List<Map<String, String>>, List<Map<String, String>>> finisher() { return Function.identity(); } /** * @return Collector characteristic to indicate finisher method is identity function. */ @Override public Set<Characteristics> characteristics() { return Set.of(Characteristics.IDENTITY_FINISH); } } } Another solution could be to turn the Stream with values into a single Map. Each pair of values is a key/value pair in the resulting Map: package mrhaki.streams; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import ja
🌐
Delft Stack
delftstack.com › home › howto › java › java key value pair
How to Implement Key Value Pair in Java | Delft Stack
February 12, 2024 - Initially, by importing the java.util package and specifically the Hashtable and Map classes. We declare and instantiate a Hashtable named keyValueTable, with explicit specifications that it will have String keys and Integer values.
🌐
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.
🌐
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.
🌐
Reddit
reddit.com › r/learnjava › how to fetch a list which is a "value" in map?
r/learnjava on Reddit: How to fetch a List which is a "value" in Map?
October 21, 2022 -

Hi folks, I hope you're doing awesome in life. I got stuck in a situation which is -

I've got a map which is of type -

Map<Integer,List<Integer>> m = new HashMap<>();

Now the thing is I want to create a (key,value) in map like -

(Key->Value)

(1->[1,2,3]),

(2 ->[2,3,4]),

(-1->[5,6,7]),

(0->[4])

And I want to add these key value pair in the Map by using the given below statement -

If(m.conatinskey(x)){ m.Put(x,m.get(x).add(any value)); ***** line ****** } else{ List<Integer> temp = new ArrayList<>(); temp.add(any integer value); m.add(temp); }

Now when I'm trying to execute the line statement I'm getting an error stating that my error has been simplified and I need verbose for better explanation but when I'm writing the statement as -

m.get(x).add(any value);

I am not getting any error . Can someone please let me know what's the issue as in my line statement I'm first trying to get the key and at thay key index I'm trying to add the element in the list ?