Create a multi-key map with the standard library

If you need a map with multiple keys, you can use a third-party library, or you can nest the standard library Map. Here, an example is a map with three keys. The first is get (). Returns null if no value with the specified key exists.

public static <A, B, C, D> D get(Map<A, Map<B, Map<C, D>>> map, A a, B b, C c) {
    return Optional.ofNullable(map.get(a))
        .map(m -> m.get(b))
        .map(m -> m.get(c))
        .orElse(null);
}

Next is put ().

public static <A, B, C, D> void put(Map<A, Map<B, Map<C, D>>> map, A a, B b, C c, D d) {
    map.computeIfAbsent(a, t -> new HashMap<>())
        .computeIfAbsent(b, t -> new HashMap<>())
        .put(c, d);
}

If you know the pattern, you can easily make a map with 4 or more keys. Below is the test code.

@Test
public void testMultiKeyMap() {
    Map<Integer, Map<Integer, Map<Integer, Integer>>> map = new HashMap<>();
    for (int a = 0, value = 0; a < 2; ++a)
        for (int b = 0; b < 2; ++b)
            for (int c = 0; c < 2; ++c)
                put(map, a, b, c, value++);
    Map<Integer, Map<Integer, Map<Integer, Integer>>> expected =
        Map.of(0, Map.of(0, Map.of(0, 0,
                                   1, 1),
                         1, Map.of(0, 2,
                                   1, 3)),
               1, Map.of(0, Map.of(0, 4,
                                   1, 5),
                         1, Map.of(0, 6,
                                   1, 7)));
    assertEquals(expected, map);
    for (int a = 0, value = 0; a < 2; ++a)
        for (int b = 0; b < 2; ++b)
            for (int c = 0; c < 2; ++c)
                assertEquals(value++, get(map, a, b, c));
    assertEquals(null, get(map, 2, 2, 2));
}

map.toString () looks like this:

{0={0={0=0, 1=1}, 1={0=2, 1=3}}, 1={0={0=4, 1=5}, 1={0=6, 1=7}}}

Recommended Posts

Create a multi-key map with the standard library
Create a simple web server with the Java standard library com.sun.net.httpserver
Create a jar file with the command
Create a playground with Xcode 12
Create a Docker image with the Oracle JDK installed (yum
About the behavior when doing a file map with java
Create a Vue3 environment with Docker!
Make a list map with LazyMap
Create exceptions with a fluid interface
Create a Maven project with a command
[Rails6] Create a new app with Rails [Beginner]
Create a Spring Boot app development project with the cURL + tar command
Create a simple web application with Dropwizard
Create a Tokyo subway map from the CSV file of station data.jp
[Rails withdrawal] Create a simple withdrawal function with rails
[Java] Create a jar file with both compressed and uncompressed with the jar command
[Java] Use cryptography in the standard library
Create a MySQL environment with Docker from 0-> 1
Run a DMN with the Camunda DMN Engine
[Rails 5] Create a new app with Rails [Beginner]
(Note) Get a set of dependent library jars with the help of Gradle
Let's make a calculator application with Java ~ Create a display area in the window
[Memo] Create a CentOS 8 environment easily with Docker
Create a simple search app with Spring Boot
Create a CSR with extended information in Java
Create a simple bulletin board with Java + MySQL
[Rails] rails new to create a database with PostgreSQL
[Windows] [IntelliJ] [Java] [Tomcat] Create a Tomcat9 environment with IntelliJ
Let's create a timed process with Java Timer! !!
Come out with a suffix on the method
[Java] Create a collection with only one element
Create a team chat with Rails Action Cable
Create a SandBox account with fastlane spaces ip
A story packed with Java's standard input Scanner
Create a web api server with spring boot
Create a Spring Boot development environment with docker
Introducing the library
Create a 3D model (PLY format) from the Hyogo prefecture numerical topographic map DSM
3. Create a database to access from the web module
Create an EC site with Rails 5 ⑨ ~ Create a cart function ~
Try implementing the Eratosthenes sieve using the Java standard library
Create a widget template for iOS14 with Intent Configuration.
[Java] How to encrypt with AES encryption with standard library
The story of making a reverse proxy with ProxyServlet
Create a Chat app with WebSocket (Tyrus) + libGDX + Kotlin
`Failed to create the host-only adapter` with docker-machine start`
Create a user with an empty password on CentOS7
[Note] Create a java environment from scratch with docker
Create a Service with an empty model Liferay 7.0 / DXP
Create a simple demo site with Spring Security with Spring Boot 2.1
Addicted to the webpacker that comes standard with Rails 6
Create a login authentication screen using the session function