繁体   English   中英

如何检查存储在哈希图中的对象中是否存在字符串?

[英]How to check if a string exists in a object which is stored in a hashmap?

我用3个参数创建了一个Employee类。

  1. ID
  2. 名称
  3. 年龄

要求:根据名称搜索。 在这种情况下,所有员工都有唯一的名称。 必须添加键为id的对象。 在极少数情况下,需要根据名称进行搜索。

我做了什么 :

在该类中,我将重写hashCode和Equals方法。

我将这些对象的列表添加到哈希图中,其ID为键,值作为Employee对象

但是在添加或从哈希图搜索时,两种方法都不会被调用

那么在hasmap上这些方法的用途是什么?

员工阶层

public class Employee {

    private int id;
    private String name;
    private int age;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int hashCode() {
        return name.hashCode();
    }

    public boolean equals(Employee emp) {
        if (emp == null)
            return false;
        else if (emp.name.equalsIgnoreCase(this.name))
            return true;
        else
            return false;
    }
}

主要方法:

public class HashMapTest {

    public static void main(String[] args) {

        Employee emp1=new Employee();
        emp1.setId(1);
        emp1.setName("Maclean");
        emp1.setAge(24);

        Employee emp2=new Employee();
        emp2.setId(2);
        emp2.setName("Sampath");
        emp2.setAge(25);

        Employee emp3=new Employee();
        emp3.setId(3);
        emp3.setName("Achar");
        emp3.setAge(27);

        Employee emp4=new Employee();
        emp4.setId(4);
        emp4.setName("Sudheer");
        emp4.setAge(25);

        Employee emp5=new Employee();
        emp5.setId(5);
        emp5.setName("Kunder");
        emp5.setAge(25);

        HashMap<Integer, Employee> empmap=new HashMap();
        empmap.put(emp1.getId(), emp1);
        empmap.put(emp2.getId(), emp2);
        empmap.put(emp3.getId(), emp3);
        empmap.put(emp4.getId(), emp4);
        empmap.put(emp5.getId(), emp5);

        Employee emp=new Employee();
        emp.setName("Maclean");
        System.out.println(empmap.containsValue(emp));

        System.exit(1);
    }

}

更新解决方案:

感谢所有的答案。

1.仅当Key是一个对象并且该方法存在于Key类中时,hashCode方法才被调用

2. Equals(Employee emp)导致函数重载而不是覆盖。 我应该使用equals(Object o)

更改代码以解决问题

@Override
public boolean equals(Object o) {
    if (o == null)
        return false;
    if (!(o instanceof Employee))
        return false;

    Employee emp = (Employee) o;
    if (emp.name.equalsIgnoreCase(this.name))
        return true;
    else
        return false;
}

您没有重写需要做的Object.equals(Object o)。 您正在超载。 这就是为什么它没有被调用的原因。

试试这个equals():

public boolean equals(Object o) {
    if (o == null)
        return false;
    if (!(o instanceof Employee))
        return false;

    Employee emp = (Employee) o;
    if (emp.name.equalsIgnoreCase(this.name))
        return true;
    else
        return false;
}

在该类中,我将重写hashCode和Equals方法。 [...]但是在添加哈希表或从哈希表搜索时,这两种方法都不会被调用。那么在hasmap上这些方法的用途是什么?

如果您有Map<Key, Value> ,并且在该地图上调用putget ,那么hashCodeequals会在Key类而不是Value类上调用。

就您而言,这意味着如果您执行empmap.put(emp1.getId(), emp1); 然后它检查emp1.getId()的哈希值以及该哈希值是否已在映射中。 因此,通常不会在Employee类上调用这些方法。

另外,如果id是“唯一”属性,则Employee.hashCode可能应该基于该属性(也equals hashCode ,也要与hashCode保持一致),如另一个答案所述, Employee.equals应该接受任何Object作为参数。

我没有测试,但可以尝试

HashMap<Integer, Employee> empmap=new HashMap<>();

甚至

HashMap<Integer, Employee> empmap=new HashMap<Integer, Employee>();

在Java 8中使用流可以很好地做到这一点。

empmap.values().stream().anyMatch(emp.getName().equals(searchedName)); 

这将获取映射中所有条目的集合,并查看流是否与名称等于searchedName任何条目匹配。

在可比对象中,您还可以使用Stream.filter()获取所有匹配的名称。

实现不同版本的equals / hashcode是棘手的,因为它以多种方式改变了类的行为。

解决您问题的最简单的方法是将此方法添加到Employee类中。 HashMap静默地使用值对象(在本例中为Employee)的equals(Object o)检查该对象的存在。

@Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Employee employee = (Employee) o;

        if (name != null ? !name.equals(employee.name) : employee.name != null) return false;

        return true;
    }

请注意,equals(Object o)的此实现仅对名称起作用,并且不检查其他字段。

Employee emp=new Employee();
emp.setId(10);
emp.setName("Maclean");
emp.setAge(240);

System.out.println(empmap.containsValue(emp));
System.out.println(emp1.equals(emp));

真实真实

暂无
暂无

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

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