简体   繁体   English

Hashmap 使用哪个键和值来搜索姓名和电话号码

[英]Hashmap using which key and value to search name and phonenumber

I have requirement to put " Name " and " phonenumber " in map.我需要在地图中放置“ Name ”和“ phonenumber ”。 I dont understand which thing I put as key and value in hashmap .我不明白我在hashmap放置了哪些作为keyvalue东西。 my requirement is we can and name with phone number and search with name.我的要求是我们可以用电话号码命名并用姓名搜索。

like Name:" sanjay " phoneNumber:" 111 ";喜欢姓名:“ sanjaysanjay :“ 111 ”;

Name:" Krish " phoneNumber:" 222 ";姓名:“ Krish ” 电话号码:“ 222 ”;

later search it by name if I search 'sanjay' it provide me sanjay's phonenumber.稍后如果我搜索“sanjay”,则按名称搜索它,它会为我提供 sanjay 的电话号码。

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>> .如果您有Person类,请制作如下地图: Map<Person, Collection<String>>

Then you can find phone numbers by doing map.get(somePerson) , which returns null if the person doesn't exist.然后您可以通过执行map.get(somePerson)找到电话号码,如果此人不存在,则返回null

You could also consider making a PhoneNumber class, which contains the string value of a validated phone number: Map<Person, Collection<PhoneNumber>> .您还可以考虑创建一个PhoneNumber类,其中包含经过验证的电话号码的字符串值: 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.请记住,如果您只比较equals(Object)的名称,则会回到第一个。 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.哈希映射的强大功能是能够以 O(1) 的效率找到值。 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.这里 Student 通过覆盖 hashCode() 和 equals() 方法作为键。

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 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();

}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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