无法从Hashmap获取对象的值,即使它返回相同的哈希码

Rag*_*S S 3 java oop equals hashmap java-8

在此输入图像描述//覆盖哈希码后无法获取hashmap中对象的值//这是我根据项名称生成hascode的item类

 public class Item {
                    private String name;
                    private Long id;
                    private double price;
                //Constructor
                    public Item(String name, Long id, double price) {
                        this.name = name;
                        this.id = id;
                        this.price = price;
                    }

                    public String getName() {
                        return name;
                    }

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

                    public Long getId() {
                        return id;
                    }

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

                    public double getPrice() {
                        return price;
                    }

                    public void setPrice(double price) {
                        this.price = price;
                    }
                //Generating hashcode based on name comparing if item id are //same        
                    @Override
                    public int hashCode() {
                        return name.hashCode();
                    }
                    @Override
                    public boolean equals(Object obj) {
                        return ((Item) obj).id ==(id);
                    }

                    @Override
                    public String toString() {
                        return "Item {" +
                                " name='" + name + '\'' +
                                ", id=" + id +
                                ", price=" + price +
                                '}';
                    }
        //Here there are items but when i pass the exact same item with exact //credentials i get null while checking how many items are there using Hashmap.
                public class Warehouse {
                    Map<Item, Integer> itemList = new HashMap<>();
                    List<Drone> drones = new ArrayList<>();
                    private Drone drone;
                    private String sourceAddress;
                    private Address address;

                    public Warehouse(Address address) {
                        this.address = address;
                        Item item = new Item("PlayStation 4 pro", (long) 100, 42000);
                        Item item1 = new Item("X box one S", (long) 200, 40000);
                        Item item2 = new Item("Apple Macbook Pro", (long) 500, 82000);
                        Item item3 = new Item("Dell Xps Laptop", (long) 1000, 92000);
                        Item item4 = new Item("iPhone 7 plus", (long) 2000, 72000);

                        itemList.put(item, 10);
                        itemList.put(item1, 20);
                        itemList.put(item2, 40);
                        itemList.put(item3, 50);
                        itemList.put(item4, 20);

                    }

                    public Drone getDrone() {
                        return new Drone();
                    }

                    public void setDrone(Drone drone) {
                        this.drone = drone;
                        System.out.println("Drone # " + drone.getDroneID() + " has arrived at the warehouse " + address);
                    }

                    public Address getAddress() {
                        return address;
                    }


                    public ArrayList<Item> getItemList() {
                        return (ArrayList<Item>) itemList;
                    }
                //Setting the item
                    public void setItem(Item item) {
                        Integer num = itemList.get(item);
                        if (num == null) {
                            num = 0;
                        }
                        this.itemList.put(item, ++num);

                    }
Run Code Online (Sandbox Code Playgroud)

//这是我面临的问题,如果我查询同一项的hashmap它返回null,Item甚至返回相同的hashcode

                    public Item removeItem(Item item) {
                        Integer num = itemList.get(item);
                        //## Issue is i get null in num 
                        if(null!= num||num!=0  ){
                            itemList.put(item,num-1);
                        }
                        System.out.println(item);
                        return item;
                    }

            }
Run Code Online (Sandbox Code Playgroud)

Era*_*ran 5

您的对象hashCode使用了该name属性,但您equals使用了该id属性.这违反了合同.equals返回true的对象必须具有相同的值hashCode.

HashMap使用两者hashCode并equals找到一个键.首先,它在HashMap基于的位置内找到一个bin hashCode.然后它会遍历bin中的所有条目,equals以找到您要查找的密钥.当hashCode不匹配时equals,您认为相等的两个对象可能会映射到不同的bin,因此使用map.contains(key)查找存储在其中的密钥Map将失败.

我认为id用作平等标准更有意义,所以我写道:

@Override
public int hashCode() {
    return id.hashCode();
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (!(obj instanceof Item))
        return false;
    return ((Item) obj).id.equals(id);
}
Run Code Online (Sandbox Code Playgroud)

请注意,我用来equals比较ids.比较对象==(Long在你的情况下为s)通常是错误的.

此外,您可能希望equals通过obj在将其转换为之前检查其类型来使您的方法更安全,Item如果类型不匹配则返回false.

PS基于你的新代码,你有另一个问题:

这个条件:

if(null!= num||num!=0  )
Run Code Online (Sandbox Code Playgroud)

将是真(if num != null)或抛出NullPointerException(如果num是null).

因此,只能将item中Map,如果它已经在Map.目前尚不清楚所需的逻辑是什么,但它看起来并不正确.