Java Map返回null get方法



我犯了一个愚蠢的错误。我传递的字符串键有括号,我没有意识到这一点。我在密钥中添加了.replaces((,现在一切都很好。感谢您的回复。


所以我有一个类,它读取一个csv文件,其中包含nfl球员的姓名、职位、薪水、积分和球队。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;
    }   
}

输出:

{"Zach Ertz"=Player@2503dbd3,";Jacoby Brissett"=Player@4b67cf4d,";Brandon Bolden"=Player@7ea987ac。。。

Map.get()在两种情况下返回null

  1. 映射不包含与提供的键关联的值
  2. 与密钥关联的值为null

如果要区分这两种状态,可以使用map.contains()方法,该方法根据映射中是否存在键来返回true/false。

我在网上读到一些关于equals方法重写的内容,但我不确定如何实现它。

最简单的选择是让IDE为您生成equals/hashcode。在IntelliJ Idea中,您只需在类内的某个位置点击Alt+Inster,然后选择equals() and hashcode()。如果你想自己写,那么你必须遵循这些方法的合同:

Equals(来自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. 

哈希代码(来自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()关联的值,因为它的哈希代码可能会更改。当然,你的代码不是这样的,因为字符串是不可变的。

if ((request.getMobileNo()>0)) {
    savedOtp = otps.get(String.valueOf(request.getMobileNo()));
}

这就是如何使用空点异常句柄。

最新更新