

How to create a Map of more than 10 key-value pairs in Java
source link: https://nick.scialli.me/how-to-create-a-map-of-more-than-10-in-java/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to create a Map of more than 10 key-value pairs in Java
Nick Scialli • June 22, 2021 • 🚀 1 minute read
If you’re trying to create a Map
of more than 10 key/value pairs in Java, you may be having some trouble. For example, the following seems to work:
Map<String, String> items = Map.of(
"key-1", "value-1",
"key-2", "value-2",
"key-3", "value-3",
"key-4", "value-4",
"key-5", "value-5",
"key-6", "value-6",
"key-7", "value-7",
"key-8", "value-8",
"key-9", "value-9",
"key-10", "value-10");
But what if we want to add more key/value pairs to our map?
The problem
Adding just one more key/value pair to the Map
results in a compilation error:
Map<String, String> items = Map.of(
"key-1", "value-1",
"key-2", "value-2",
"key-3", "value-3",
"key-4", "value-4",
"key-5", "value-5",
"key-6", "value-6",
"key-7", "value-7",
"key-8", "value-8",
"key-9", "value-9",
"key-10", "value-10",
"key-11", "value-11");
You will get a cryptic error to the effect of:
no suitable method found for of(String, String, String, String, String, String, String, String, String, String, String, String, String, String, String, String, String, String, String, String, String, String)
The solution
For larger Maps, you can use the Map.ofEntries
method rather than Map.of
. You’ll additionally need to specify each key/value pair inside Map.entry
. Here’s an example of the aforementioned 11-pair Map
that will actually compile:
Map<String, String> items = Map.ofEntries(
Map.entry("key-1", "value-1"),
Map.entry("key-2", "value-2"),
Map.entry("key-3", "value-3"),
Map.entry("key-4", "value-4"),
Map.entry("key-5", "value-5"),
Map.entry("key-6", "value-6"),
Map.entry("key-7", "value-7"),
Map.entry("key-8", "value-8"),
Map.entry("key-9", "value-9"),
Map.entry("key-10", "value-10"),
Map.entry("key-11", "value-11"));
And there you have it—our code compiles and we can keep adding key/value pairs to our map!
Recommend
-
48
Those who used curses at least once to draw something in color had to notice that setting colors happens in pairs (foregrou...
-
10
Update key-value pairs – thispointer.comThis article will discuss how to add or append new key-value pairs to a dictionary or update existing keys’ values. Table of Contents We can add/append key-value pairs to...
-
6
SQL - Subquery returned more than one value. How to fix? advertisements -- Using orders, customers, employees, and order details tables, selec...
-
7
A simple thought about key-value pairs Sep 3 ・3 min read
-
5
Memory CardsLearn it by heart, create pairs ‘word - word’ and practice
-
8
Home Chevron icon It indicates an exp...
-
4
2022 Hyundai Kona Review: More Than Just Good Value
-
11
This article will cover what key-value pairs (KVPs) are, how they’re used, and how GdPicture.NET’s KVP extraction engine extracts them from documents that contain unstructured data. What Are Key-Value Pairs?
-
5
-
6
Java Solution of D- Divisible Pairs CF Round 925 Java Solution of D- Divisible Pairs CF Round 925 ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK