为什么不通过引用更新此 HashMap 值?
Why isn't this HashMap value being updated via reference?
我想弄清楚为什么当引用更新时与 hashMap 关联的值没有更新。由于 Java 是 pass-by-value-by-reference
,与 BIN1
关联的 value
不应该简单地指向现在 '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());
}
}
首先您将 curr
指向 map.get(bin)
的结果:
Card curr = map.get(bin);
之后您将 curr
指向一个新对象:
curr = new Card(bin, cardType, cardName, trustScore);
map.get(bin)
返回其引用的对象没有改变
map.get(bin).cardType = "VIEX"
会更改该对象中的内容。
我想弄清楚为什么当引用更新时与 hashMap 关联的值没有更新。由于 Java 是 pass-by-value-by-reference
,与 BIN1
关联的 value
不应该简单地指向现在 '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());
}
}
首先您将 curr
指向 map.get(bin)
的结果:
Card curr = map.get(bin);
之后您将 curr
指向一个新对象:
curr = new Card(bin, cardType, cardName, trustScore);
map.get(bin)
返回其引用的对象没有改变
map.get(bin).cardType = "VIEX"
会更改该对象中的内容。