Java:如何检查 HashMap 键是否包含对象?

标签 java hashmap

基本上,我尝试使用以下代码读取文本文件,以便使用邻接列表表示构建图形。但是,我遇到了两个问题。

第一个也是主要的问题是:我不明白当我检查graph.contains(v_l)时,它总是返回false。我多年来一直被这个问题困扰。这里确实需要一些帮助。

第二个问题:我不明白为什么在 if 语句中,我无法执行以下操作:

if(graph.containsKey(v_l) == false){
                // this always fail               
                graph.put(v_l, edges_of_this_vertex.add(v_r));
                // the following works though 
                ArrayList<Vertex> edges_of_this_vertex = new ArrayList<Vertex>();
                edges_of_this_vertex.add(v_r);
                graph.put(v_l, edges_of_this_vertex);
 }

我不知道为什么会发生这种情况?

 class Vertex{
            int node;
            ....
            public Vertex(int node){
                 this.node = node;
            }

            public String toString(){
                 return Integer.toString(node);
            }
    }
class dgraph{
 // constructor ...

 // instance method
 public HashMap<Vertex, ArrayList<Vertex>> read_file_and_populate(String file_loc, boolean reverse) throws IOException{

        HashMap<Vertex, ArrayList<Vertex>> graph = new HashMap<Vertex, ArrayList<Vertex>>();
        int l = 0;
        int r = 0;
        if(reverse == false){
            r = 1;
        } else{
            l = 1;
        }

        FileInputStream fil = new FileInputStream(file_loc);
        BufferedReader br = new BufferedReader( new InputStreamReader(fil));
        String element = null;

        while( (element = br.readLine()) != null){
            String[] line = element.split("\\s");
            Vertex v_l = new Vertex( Integer.parseInt(line[l]) );
            Vertex v_r = new Vertex( Integer.parseInt(line[r]) );
            System.out.println("l = " + l + " r = " + r );
            if(graph.containsKey(v_l) == false){
                ArrayList<Vertex> edges_of_this_vertex = new ArrayList<Vertex>();
                edges_of_this_vertex.add(v_r);
                graph.put(v_l, edges_of_this_vertex);
                //graph.put(v_l, edges_of_this_vertex.add(v_r));
            } else{
                graph.get(v_l).add(v_r);

            }
        }
        return graph;
    }
}

下面是一些示例数据:

1 1 
1 2 
1 5 
1 6 
1 7 
1 3 
1 8 
1 4 
2 47646 
2 47647 
2 13019 
2 47648 
2 47649 
2 47650 
2 7700 
2 47651 
2 47652 
3 511596 
5 1 
5 9 

最佳答案

您的值类(Vertex)需要实现 hashCode 和 equals(),否则所有操作都将通过检查其是否相同的实例来完成(在这种情况下永远不会)。假设 int 是 Vertex 中的唯一状态,则 hashCode 和 equals 函数应基于这些函数,例如

public int hashCode() {
   return node *31;
}

public boolean equals(Object o) {
    if (o == this) return true;
    if (o == null || getClass() != o.getClass()) return false;
    Vertex r = (Vertex)o;
    return node == r.node;
}

关于Java:如何检查 HashMap 键是否包含对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27497311/

相关文章:

java - @AttributeOverride 不适用于 Hibernate 5

java - 如何在java中禁用某些用户菜单中的功能?

java - 在 Java 中使用 Scala map

java - 使用常量迭代两个类并将它们的字段加载到 HashMap 中

java - HashMap<String[], List<int[]>> 未检测到重复值

java - 具有多个值键的 HashMap

java - 如何在 Spring 中从 json 创建一个模型,其中外键被引用为长属性?

java - 如何使用工厂模式来获取数据库客户端的实例?

java - 将 hashmap 转换为小写

java - 获取 HashMap<Integer, ArrayList<String>> 的所有子级