为什么这是通过参考更新的hashmap值?

发布于 2025-01-26 16:52:45 字数 1783 浏览 1 评论 0原文

我试图围绕引用更新时不更新与哈希图关联的价值的原因。由于java是通过value-by-Reference不应与valuebin1关联的,只需指向现在' curr指向?

class Solution {
    static class Card{
        private final String bin;
        private final String cardType;
        private final String cardName;
        private int trustScore;

        public Card(String bin, String cardType, String cardName, int trustScore){
            this.bin = bin;
            this.cardType = cardType;
            this.cardName = cardName;
            this.trustScore = trustScore;
        }

        public String toString(){
            return this.bin + " " + this.cardName + " "+ this.cardType + " " + this.trustScore;
        }
    }
    static class CardProcessor{
        private Map<String, Card> map;

        CardProcessor(){
            this.map = new HashMap<>();
        }
        public void store(String bin, String cardType, String cardName, int trustScore){
            if(!map.containsKey(bin))
                map.put(bin, new Card(bin, cardType, cardName, trustScore));
            else {
                Card curr = map.get(bin);
                if(curr.trustScore < trustScore) {
                    curr = new Card(bin, cardType, cardName, trustScore);
                    map.put(bin, curr); // Why is this line necessary to point BIN1 to the new value of card? Since Curr is a reference to Card shouldn't curr simply point to the new value supplied?
                }
            }
        }
    }
    public static void main(String[] args) {
        CardProcessor cp = new CardProcessor();
        cp.store("BIN1", "VISA", "BoA", 1);
        cp.store("BIN1", "VIEX", "BACU", 5);
        System.out.println(cp.map.entrySet());
    }
}

I'm trying to wrap my head around why the value associated with a hashMap isnt updated when the reference is updated. Since Java is pass-by-value-by-reference shouldn't the value associated with BIN1 simply point to the new object that now 'curr points to?

class Solution {
    static class Card{
        private final String bin;
        private final String cardType;
        private final String cardName;
        private int trustScore;

        public Card(String bin, String cardType, String cardName, int trustScore){
            this.bin = bin;
            this.cardType = cardType;
            this.cardName = cardName;
            this.trustScore = trustScore;
        }

        public String toString(){
            return this.bin + " " + this.cardName + " "+ this.cardType + " " + this.trustScore;
        }
    }
    static class CardProcessor{
        private Map<String, Card> map;

        CardProcessor(){
            this.map = new HashMap<>();
        }
        public void store(String bin, String cardType, String cardName, int trustScore){
            if(!map.containsKey(bin))
                map.put(bin, new Card(bin, cardType, cardName, trustScore));
            else {
                Card curr = map.get(bin);
                if(curr.trustScore < trustScore) {
                    curr = new Card(bin, cardType, cardName, trustScore);
                    map.put(bin, curr); // Why is this line necessary to point BIN1 to the new value of card? Since Curr is a reference to Card shouldn't curr simply point to the new value supplied?
                }
            }
        }
    }
    public static void main(String[] args) {
        CardProcessor cp = new CardProcessor();
        cp.store("BIN1", "VISA", "BoA", 1);
        cp.store("BIN1", "VIEX", "BACU", 5);
        System.out.println(cp.map.entrySet());
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰火雁神 2025-02-02 16:52:45

首先,您将curr指向map.get.get(bin)的结果:

Card curr = map.get(bin); 

之后,您将curr指向新对象:

curr = new Card(bin, cardType, cardName, trustScore);

其参考的对象由map.get(bin)不更改返回

map.get(bin).cardType = "VIEX"

将更改该对象中的内容。

First you pointed curr to the result of map.get(bin):

Card curr = map.get(bin); 

After that you pointed curr to a new object:

curr = new Card(bin, cardType, cardName, trustScore);

The object whose reference was returned by map.get(bin) did not change

map.get(bin).cardType = "VIEX"

would have changed the content within that object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文