Map implementation with duplicate keys

后端 未结 18 1946
暗喜
暗喜 2020-11-22 15:23

I want to have a map with duplicate keys.

I know there are many map implementations (Eclipse shows me about 50), so I bet there must be one that allows this. I know

18条回答
  •  感动是毒
    2020-11-22 15:48

    If there are duplicate keys then a key may correspond to more than one value. The obvious solution is to map the key to a list of these values.

    For example in Python:

    map = dict()
    map["driver"] = list()
    map["driver"].append("john")
    map["driver"].append("mike")
    print map["driver"]          # It shows john and mike
    print map["driver"][0]       # It shows john
    print map["driver"][1]       # It shows mike
    

提交回复
热议问题