我的愚蠢错误。我传入的字符串键有括号,我没有意识到这一点。我在键上添加了一个.replace(),现在一切都很好。感谢您的回复。
所以我有一个类读取包含nfl玩家姓名,职位,薪水,积分和团队的csv文件。 DKdata类读取文件,getPlayers方法返回映射。我遇到的问题是,每当我尝试使用get(key)时,它只返回null。我在网上读到了关于equals方法覆盖的一些内容,但我不知道如何为此实现它。如果有人能帮助我或引导我朝着正确的方向前进,我将不胜感激。下面的代码与输出。
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
public class Test {
public static void main(String[]args) throws FileNotFoundException {
Map<String, Player> players = new HashMap<String, Player>();
DKdata d = new DKdata();
//players.putAll(d.getPlayers());
Player p = new Player("WR","Kev", 1000, 99, "Pit");
String name = p.name;
players.put(p.name, p);
System.out.println(players.get(name).salary);
players.putAll(d.getPlayers());
System.out.println(players.get("Zach Ertz").salary);
}
}
public class DKdata {
private Map<String, Player> players;
private Scanner scanner = new Scanner(new File("/Users/kevinrhea/Documents/DraftKing/DKsalaries.csv"));
public DKdata() throws FileNotFoundException {
try {
players = new HashMap<String, Player>();
scanner.useDelimiter(",");
scanner.nextLine();
while(scanner.hasNext()){
String[] data = scanner.nextLine().split(",");
Player player = new Player(data[0], data[1], Integer.parseInt(data[2]), Double.parseDouble(data[4]), data[5]);
players.put(data[1], player);
}
} catch(Exception e) {
e.printStackTrace();
}
}
public Map<String, Player> getPlayers(){
return players;
}
}
输出:
{&#34; Zach Ertz&#34; = Player @ 2503dbd3,&#34; Jacoby Brissett&#34; = Player @ 4b67cf4d,&#34; Brandon Bolden&#34; = Player @ 7ea987ac,... < / p>
空
答案 0 :(得分:1)
Map.get()
在两种情况下返回null
:
null
如果要区分这两种状态,可以使用map.contains()
方法,该方法返回true / false,具体取决于键是否存在于地图中。
我在网上看到有关equals方法覆盖的内容,但我不知道如何为此实现它。
最简单的选择是让IDE为您生成equals / hashcode。在IntelliJ Idea中,你可以在课堂内的某个地方点击Alt + Inster,然后选择equals() and hashcode()
。如果你想自己写,那么你必须遵循这些方法的合同:
等于(来自javadoc):
It is reflexive: for any non-null reference value x, x.equals(x) should return true.
It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
For any non-null reference value x, x.equals(null) should return false.
Hashcode(来自javadoc):
Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
重要的是要注意地图中的键必须是有效的不可变的。这意味着一旦您将一个给定的密钥放在一个映射中,您就不能修改它,否则您将无法get()
关联的值,因为它的哈希码可能会改变。当然,因为字符串是不可变的,所以“用你的代码嗤之以鼻。”