为什么这是通过参考更新的hashmap值?
我试图围绕引用更新时不更新与哈希图关联的价值的原因。由于java是通过value-by-Reference
不应与value
与bin1
关联的,只需指向现在' 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您将
curr
指向map.get.get(bin)
的结果:之后,您将
curr
指向新对象:其参考的对象由
map.get(bin)
不更改返回将更改该对象中的内容。
First you pointed
curr
to the result ofmap.get(bin)
:After that you pointed
curr
to a new object:The object whose reference was returned by
map.get(bin)
did not changewould have changed the content within that object.