你真的了解Java的HashMap吗?
程序员文章站
2022-03-16 08:52:43
...
认识了它,女朋友都夸你厉害
import java.util.ArrayList;
import java.util.List;
//Node对象
public class Node {
public int val;
public List<Node> neighbors;
public Node() {
val = 0;
neighbors = new ArrayList<Node>();
}
public Node(int _val) {
val = _val;
neighbors = new ArrayList<Node>();
}
public Node(int _val, ArrayList<Node> _neighbors) {
val = _val;
neighbors = _neighbors;
}
}
public class test {
private static Map<Integer, Node> map = new HashMap<>();
public static void main(String[] args) {
Node node = new Node();//创建一个Node对象
map.put(1, node);//把它放进我们的哈希表中
node.val = 100;//然后赋值给我们new 出来的node对象
System.out.println(node);
System.out.println(map.get(1));
System.out.println(node == map.get(1));
System.out.println(map.get(1).val);
}
}