Is there a Java equivalent of Python's defaultdict?

后端 未结 8 1533
失恋的感觉
失恋的感觉 2021-02-05 00:05

In Python, the defaultdict class provides a convenient way to create a mapping from key -> [list of values], in the following example,



        
8条回答
  •  太阳男子
    2021-02-05 00:24

    Using just the Java runtime library you could use a HashMap and add an ArrayList to hold your values when the key does not exist yet or add the value to the list when the key does exist.

提交回复
热议问题