简体   繁体   中英

Hashmap using which key and value to search name and phonenumber

I have requirement to put " Name " and " phonenumber " in map. I dont understand which thing I put as key and value in hashmap . my requirement is we can and name with phone number and search with name.

like Name:" sanjay " phoneNumber:" 111 ";

Name:" Krish " phoneNumber:" 222 ";

later search it by name if I search 'sanjay' it provide me sanjay's phonenumber.

and, there is more then one user with same name and one user may have more then one phonenumber.

Thanks.

If you have a Person class, make a map like: Map<Person, Collection<String>> .

Then you can find phone numbers by doing map.get(somePerson) , which returns null if the person doesn't exist.

You could also consider making a PhoneNumber class, which contains the string value of a validated phone number: Map<Person, Collection<PhoneNumber>> .

Use a class wrapper:

public class Person {

    private List<String> phoneNumbers;
    private String fullName;

    //getters, setters, constructors for field values

    @Override
    public boolean equals(Object o) {
        if (!(o instanceof Person) {
            return false;
        }
        Person p = (Person) o;
        return this.fullName.equals(p.fullName); //and other qualifying things
    }

    @Override
    public int hashcode() {
        //account for fields that you use in #equals(Object)
    }

 }

Then you can index based on whatever you want:

 /* Full name => People */
 Map<String, List<Person>> people = new HashMap<>();
 /* Number => Person */
 Map<String, Person> people = new HashMap<>();

Keep in mind, if you only compare the name in equals(Object) , you're back to square one. Add more things to compare to be consistent with the uniqueness.

Hash maps great power is the ability to find the values in O(1) efficiency. For this to work, the key must be the object you search by.

For example, if you want to search by name than your key should be the name. And since a person can have several phone numbers, the value should be a List of phone numbers.

if you want to find the person name according to the phone number you should handle this the other way around - the key would be the phone number and the value would be the person name.

Perhaps you want both...

There are good answers above, may be this will also helps

Here Student made as key by overriding hashCode() and equals() method.

public class Student {

        public String studentId;
        public String studentName;
        public Student(String studentId, String studentName) {
            this.studentId=studentId;
            this.studentName =studentName;

        }
        @Override
        public int hashCode() {
            return 1234;
        }
        @Override
        public boolean equals(Object o) {
            if (o instanceof Student) {
                Student student=(Student)o;
                if (this.studentId.equalsIgnoreCase(student.studentId)) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }
    }

Phone Number class :

public class PhoneNumber {
        public String phoneNumber;
        public PhoneNumber(String phoneNumber) {
            this.phoneNumber =phoneNumber;
        }
}

Person Class :

import java.util.HashMap;
import java.util.Set;
import java.util.List;
import com.google.common.collect.Lists;

public class Person {
        public static void main(String[] args) {
            Student e1=new Student("e001","studentOne");
            Student e2=new Student("e002","studentTwo");


            PhoneNumber d1 = new PhoneNumber("9999999998");
            PhoneNumber d2 = new PhoneNumber("9999999999");
            List listOfPhoneNumbersOfStudentOne = Lists.newArrayList(d1,d2);

            PhoneNumber d3 = new PhoneNumber("9999999997");
            PhoneNumber d4 = new PhoneNumber("9999999996");
            List listOfPhoneNumbersOfStudentTwo = Lists.newArrayList(d3,d4);

/* Here Student made as key by overriding hashCode() and equals() method.*/

            HashMap<Student, List<PhoneNumber>> map=new HashMap<Student, List<PhoneNumber>>();
            map.put(e1, listOfPhoneNumbersOfStudentOne);
            map.put(e2, listOfPhoneNumbersOfStudentTwo);

            Set<Student> key=map.keySet();
            for (Student student : key) {
                System.out.println(student.studentId+" "+student.studentName +" ");


            }
        }

    }

public class Assignment4 { HashMap map = new HashMap<>();

public void addContact(String name, Integer number) {
    map.put(name, number);

}

public void getphoneNumber(String name) {
    if (map.containsKey(name)) {
        Integer a = map.get(name);
        System.out.println("Contact of " +name+" is " + a);
    }
}

public static void main(String[] args) {

    Assignment4 a4 = new Assignment4();
    a4.addContact("vishal", 10345);
    a4.addContact("sachin", 30456);
    a4.addContact("sai", 30458);
    Scanner s=new Scanner(System.in);
    System.out.println("Enter name to get contact details");
    a4.getphoneNumber(s.next());
    s.close();

}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM