Java 如何通过key-object从Map获取值
Java how to get value from Map by key-object
我正在 java 中进行图形实现。我有以下问题:
我无法通过键从 HashMap 获取值,它是一个模板化的 class 对象,
更准确地说,我需要得到一个值,其二进制表示形式的键等于指定的键对象,但不是(它们在内存中有不同的地址)。按我的理解,get(..)
方法returns一个键地址等于指定one.I的值不傻搜就不知道怎么弄了。顺便说一句,我是用C++写的,没看懂Java水平还不错
这是有问题的代码:
public void addEdge(V v1, V v2, E edg) {
nodes.get(new Vertex<V>(v1)).put(new Vertex<V>(v2), new Edge<E>(edg));
nodes.get(new Vertex<V>(v2)).put(new Vertex<V>(v1), new Edge<E>(edg));
}
完整代码如下:
import java.util.*;
class Vertex<Node> {
Node node;
int id;
Vertex(Node val) {
this.node = val;
}
Vertex(Node val, int id) {
this.node = val;
this.id = id;
}
}
class Edge<T> {
T value;
Edge(T val) {
this.value = val;
}
}
public class Graph<V, E> {
private Map<Vertex<V>, HashMap<Vertex<V>, Edge<E>>> nodes;
Graph() {
nodes = new HashMap<Vertex<V>, HashMap<Vertex<V>, Edge<E>>>();
}
public void addVertex(V ver, int id) {
nodes.put(new Vertex<V>(ver, id), new HashMap<Vertex<V>, Edge<E>>());
}
public void addEdge(V v1, V v2, E edg) {
nodes.get(new Vertex<V>(v1)).put(new Vertex<V>(v2), new Edge<E>(edg));
nodes.get(new Vertex<V>(v2)).put(new Vertex<V>(v1), new Edge<E>(edg));
}
public V getNode(int id) {
for(Vertex<V> el: nodes.keySet())
if(el.id == id)
return el.node;
return null;
}
}
来自https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html#get-java.lang.Object-
public V get(Object key)
Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
More formally, if this map contains a mapping from a key k to a value v such that >(key==null ? k==null : key.equals(k)), then this method returns v; otherwise it >returns null. (There can be at most one such mapping.)
您需要在顶点中实现 equals(),否则它只会查看必须是原始对象的默认 equals()。
类似
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Vertex that = (Vertex) o;
if (node != null ? !node.equals(that.node) : that.node != null) return false;
return this.id == that.id;
}
重要提示:
此外,您需要实现 implement hashcode() 以使其在 HashMap 中工作,或实现 compareTo() 以实现树图。
等教程
我正在 java 中进行图形实现。我有以下问题:
我无法通过键从 HashMap 获取值,它是一个模板化的 class 对象,
更准确地说,我需要得到一个值,其二进制表示形式的键等于指定的键对象,但不是(它们在内存中有不同的地址)。按我的理解,get(..)
方法returns一个键地址等于指定one.I的值不傻搜就不知道怎么弄了。顺便说一句,我是用C++写的,没看懂Java水平还不错
这是有问题的代码:
public void addEdge(V v1, V v2, E edg) {
nodes.get(new Vertex<V>(v1)).put(new Vertex<V>(v2), new Edge<E>(edg));
nodes.get(new Vertex<V>(v2)).put(new Vertex<V>(v1), new Edge<E>(edg));
}
完整代码如下:
import java.util.*;
class Vertex<Node> {
Node node;
int id;
Vertex(Node val) {
this.node = val;
}
Vertex(Node val, int id) {
this.node = val;
this.id = id;
}
}
class Edge<T> {
T value;
Edge(T val) {
this.value = val;
}
}
public class Graph<V, E> {
private Map<Vertex<V>, HashMap<Vertex<V>, Edge<E>>> nodes;
Graph() {
nodes = new HashMap<Vertex<V>, HashMap<Vertex<V>, Edge<E>>>();
}
public void addVertex(V ver, int id) {
nodes.put(new Vertex<V>(ver, id), new HashMap<Vertex<V>, Edge<E>>());
}
public void addEdge(V v1, V v2, E edg) {
nodes.get(new Vertex<V>(v1)).put(new Vertex<V>(v2), new Edge<E>(edg));
nodes.get(new Vertex<V>(v2)).put(new Vertex<V>(v1), new Edge<E>(edg));
}
public V getNode(int id) {
for(Vertex<V> el: nodes.keySet())
if(el.id == id)
return el.node;
return null;
}
}
来自https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html#get-java.lang.Object-
public V get(Object key)
Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
More formally, if this map contains a mapping from a key k to a value v such that >(key==null ? k==null : key.equals(k)), then this method returns v; otherwise it >returns null. (There can be at most one such mapping.)
您需要在顶点中实现 equals(),否则它只会查看必须是原始对象的默认 equals()。
类似
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Vertex that = (Vertex) o;
if (node != null ? !node.equals(that.node) : that.node != null) return false;
return this.id == that.id;
}
重要提示: 此外,您需要实现 implement hashcode() 以使其在 HashMap 中工作,或实现 compareTo() 以实现树图。
等教程