0

How would you store the contacts in an android phone if the “contact searching while typing” has to be fast and a contact can have multiple phone numbers.

2 Answers2

0

I think Trie Tree will be best. At the end of the character of the name, you can store an object with its elements like an array of the phone number, email variable, and some extra variable that you want.

when searching a name you can go through the character in Trie Tree and show at least n name in sorted order. I think Trie tree will be faster than any other data structure.

0

You can use a standard TreeMap<String, Contact> to implement this feature (where Contact would be a class storing the phone numbers etc). One option is to use TreeMap.tailMap() to get a sub-map of the entries starting from what the user has typed so far (backed by the original map, so this should be fast). You can use a custom comparator to adjust the ordering to your needs (e.g. case-insensitive)

p.s.: That doesn't address persistence, so you may want to use a database from the start, e.g. Room. Prefix search tyically can be implemented by searching for the range (prefix, prefix + "\uffff"). With databases, one would typically add a limit.

Stefan Haustein
  • 18,427
  • 3
  • 36
  • 51