Partial search in HashMap

前端 未结 5 780
后悔当初
后悔当初 2020-11-29 03:55

I need to create phone book kind of thing. It contains name & number. Now when I type letters matching list should be returned. For the example given below, when I type

5条回答
  •  甜味超标
    2020-11-29 04:41

    Put it all in a MultiMap (or just store a List as the value in your HashMap). For "Brown", store:

    "B"->["Brown"]
    "BR"->["Brown"]
    "BRO"->["Brown"]
    

    If you later add "Bradley":

    "B"->["Brown", "Bradley"]
    "BR"->["Brown", "Bradley"]
    "BRO"->["Brown"]
    "BRA"->["Bradley"]
    

    etc...

    then have another map to map "Brown" or "Bradley" to the phone number.

提交回复
热议问题