Is there any way to declare 'Hybrid' HashMap of java like in PHP

Viewed 101

I want to know that there is a way to declare and initialize the 'Hybrid' HashMap in Java as I used to do in PHP.

Something like this, (in PHP)

Array
(
    [key_0] => Array (
            [name] => category1
            [sub] => Array (
            )
    ),

    [key_1] => Array (
            [name] => category2
            [sub] => Array (
                  sub_category1,
                  sub_category2,
                  sub_category3,
            )
    ),

    [key_2] => Array (
            [name] => category2
            [sub] => Array (
                  [sub_sub1] => Array(
                           sub_sub_cate1,
                           sub_sub_cate2,
                  ),
                  [sub_sub2] => Array(
                           sub_sub_cate3,
                           sub_sub_cate4,
                  ),
            )
    ),
    ...
)

I've googled for several hours, but there was no satisfactory answer.

Can anyone help me?

1 Answers

It seems that a Map<String, Object> is needed and may be created but it won't be convenient to use because the values are raw objects:

Map<String, Object> map = Map.of(
    "key_0", Map.of("name", "category0", "sub", Collections.emptyList()),
    "key_1", Map.of("name", "category1", "sub", List.of("sub_1", "sub_2", "sub_3")),
    "key_2", Map.of("name", "category2", "sub", Map.of(
            "sub_1", List.of("sub_1_1", "sub_1_2"),
            "sub_2", List.of("sub_2_1", "sub_2_2")
    ))
);
Related