【问题标题】:Looking up a map using a key method, manages to find the key in the map. How?使用 key 方法查找地图,设法在地图中找到关键。如何?
【发布时间】:2023-03-31 01:35:01
【问题描述】:

我有一个 TreeMap 包含 StockItem 值的映射:

private final Map<StockItem, Integer> list;

我正在使用一种方法来查找此映射中的值,方法是生成一个返回 StockItem 类型的键。

方法如下:

public static StockItem makeKey(String name, double price, int quantityStock){
    return new StockItem(name,price,quantityStock);
}

代码运行良好,查找运行良好,我的问题是这到底是怎么可能发生的? makeKey 方法返回一个全新的对象,其中包含可能包含在列表中的完全相同的数据。它是否会经过每次迭代然后调用.equals 来比较每个对象?

这是StockItem 类:

public class StockItem implements Comparable<StockItem> {
    private final String name;
    private int quantity;
    private double price;
    private int reserveItems = 0;

    public StockItem(String name, double price) {
        this.name = name;
        this.price = price;
        quantity = 0;
    }

    public StockItem(String name, double price, int quantityStock) {
        this.name = name;
        this.price = price;
        quantity = quantityStock;

    }

    public void reserveItem(int amountReserved){
        this.reserveItems = amountReserved + this.reserveItems;
    }

    public void unReserveItem(int unreserve){
        reserveItems = reserveItems - unreserve;
    }

    public int getReservedAmount(){
        return reserveItems;
    }

    public String getName() {
        return name;
    }

    public int quantityInStock() {
        return quantity;
    }

    public double getPrice() {
        return price;
    }

    public void adjustStock(int quantity) {
        this.quantity = this.quantity + quantity - this.reserveItems;
    }

    public final void setPrice(double price) {
        if (price > 0.0)
        this.price = price;
    }

    public static StockItem MakeKey(String name, double price, int quantityStock){
        return new StockItem(name,price,quantityStock);
    }
    @Override
    public int compareTo(StockItem o) {
        if (this == o){
            return 0;
        }

        if (o != null){
            return this.name.compareTo(o.getName());
        }

        throw new NullPointerException();
    }


    public String toString(){
        return "Item Name: " + this.name + " Item Price: " + this.price;
    }
}

【问题讨论】:

  • 请发布 StockItem 代码。
  • 它仅在如果实际地图是TreeMap(代码中未显示)时有效,在这种情况下,查找是通过StockItem 的名称值,由Comparable 接口的compareTo() 方法实现,这是TreeMap 使用的。如果映射是HashMap,则查找将失败,因为StockItem 类没有实现equals()hashCode()。请参阅 TreeMapHashMap 的 javadoc,了解它们的工作原理。
  • @4castle TreeMap
  • @Andreas 好的,所以在 HashMap 中,如果我做了同样的事情,但使用 equals 和 hashcode 也可以吗?
  • @JordanDixon 是的。

标签: java maps equality treemap


【解决方案1】:

没有ComparatorTreeMap (例如您的示例)会根据其对象的compareTo 方法是否认为这些项目相等来进行查找:

有序映射使用其compareTo(或compare)方法执行所有键比较,因此从有序映射的角度来看,此方法认为相等的两个键是相等的。

由于StockItemcompareToname 值相等时返回true,并且由于“键”项包含name 值,因此按键查找映射将相应项作为“键”返回" 对象被地图视为等于整个对象。

关于它如何工作以及是否遍历每个对象的问题的答案可以从TreeMapJavadocs 确定:

基于红黑树的 NavigableMap 实现。 [...] 此实现为containsKeygetputremove 操作提供有保证的 log(n) 时间成本。算法改编自 Cormen、Leiserson 和 Rivest 的Introduction to Algorithms中的算法。

如上所述,查找是使用red–black tree 执行的。它是如何工作的细节超出了这个答案的合理范围。然而,get 方法被定义为 log(n) time cost 的事实告诉我们,它没有迭代列表中的所有元素,因为迭代所有元素会花费更多的时间成本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-27
    • 2018-09-21
    • 1970-01-01
    • 2014-12-18
    • 2021-12-17
    • 1970-01-01
    相关资源
    最近更新 更多