Sanjay Rabari
Sanjay Rabari

Reputation: 2081

Which collections to use?

Suppose I want to store phone numbers of persons. Which kind of collection should I use for key value pairs? And it should be helpful for searching. The name may get repeated, so there may be the same name having different phone numbers.

Upvotes: 0

Views: 133

Answers (4)

davioooh
davioooh

Reputation: 24666

Your problem has different solutions. For example, I'll go with a LIST: List<Person>, where Person is a class like this:

public class Person{

    private String name;
    private List<String> phoneNumbers;

    // ...

}

For collections searching/filtering I suggest Guava Collections2.filter method.

Upvotes: 2

In case you want to use key value pair. Good choice is to use Map instead of collection.

So what should that map store ?

As far it goes for key. First thing you want to assure is that your key is unique to avoid collisions.

class Person {

 long uniqueID; 

 String name; 
 String lastname;

}

So we will use the uniqueID of Person for key.

What about value ?

In this case is harder. As the single Person can have many phone numbers. But for simple task lest assume that a person can have only one phone number. Then what you look is

class PhoneNumberRegistry {

   Map<Long,String> phoneRegistry = new HashMap<>();

}

Where the long is taken from person. When you deal with Maps, you should implement the hashCode and equals methods.

Then your registry could look like

 class PhoneNumberRegistry {

   Map<Person,String> phoneRegistry = new HashMap<>();

}

In case when you want to store more then one number for person, you will need to change the type of value in the map.

You can use Set<String> to store multiple numbers that will not duplicate. But to have full control you should introduce new type that not only store the number but also what king of that number is.

 class PhoneNumberRegistry {

   Map<Person,HashSet<String>> phoneRegistry = new HashMap<>();

}

But then you will have to solve various problems like, what phone number should i return ?

Upvotes: 5

Grim
Grim

Reputation: 1974

You should use this:

Hashtable<String, ArrayList<String>> addressbook = new Hashtable<>();
ArrayList<String> persons =  new ArrayList<String>()
persons.add("Tom Butterfly");
persons.add("Maria Wanderlust");
addressbook.put("+0490301234567", persons);
addressbook.put("+0490301234560", persons);

Hashtable are save to not have empty elements, the ArrayList is fast in collect small elements. Know that multiple persons with different names may have same numbers.

Know that 2 persons can have the same number and the same Name!

String name = "Tom Butterfly";
String[] array = addressbook.keySet().toArray(new String[] {});
int firstElement = Collections.binarySearch(Arrays.asList(array),
        name, new Comparator<String>() {

            @Override
            public int compare(String top, String bottom) {
                if (addressbook.get(top).contains(bottom)) {
                    return 0;
                }
                return -1;
            }
        });
System.out.println("Number is " + array[firstElement]);

Upvotes: 1

ifloop
ifloop

Reputation: 8386

Maybe

List<Pair<String, String> (for one number per person) or
List<Pair<String, String[]> (for multiple numbers per person)

will fit your needs.

Upvotes: 1

Related Questions