开发者

using Map to implement efficient searching process - java

I am creating a phone book API in Java. Every name in my phone book is mapped to a set of phone numbers & address. I want to build a search function which can search an entry in my phone book using a search parameter as a name开发者_如何学JAVA or as the phone number itself.

My approach has been to add all my entries to a:

Map <String, PhoneNumber> book = new HashMap <String,PhoneNumber>();
book.put("Name1",new PhoneNumber(new Integer(12345),new Integer(123456));
book.get("Name1");
// PhoneNumber is my class which can have different types of phone numbers

I want to search by both the name which is the key and also by the value. I cannot do that using a HashMap. Is there a way to do it better to implement an efficient searching process?


Don't use a Map for your phone book. It's too limited. Instead create a PhoneBook class. Within that class have 2 Maps, one for the by-name searching and one for the by-number searching. Create an add() routine that adds Info to both maps.

public class PhoneBook {
   private Map<String,Info> byNumber;
   private Map<String,Info> byName;
}

Where "Info" is a class that tells you everything about the person.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜