gpt4 book ai didi

java - Java中HashMap定义的有效性?

转载 作者:行者123 更新时间:2023-12-01 20:03:06 25 4
gpt4 key购买 nike

class Node {
long data;
Node node;
int rank;
}
  1. private Map<Long, Node> map = new HashMap<>();

  2. private Map<Long, Node> map = new HashMap<Long,Node>();

我正在实现 Node 类的 hashmap

我的问题是上面提到的两个Hashmaps的有效性。(两者是否都是coreect?)。如果它们有效,那么两个初始化有什么区别?

最佳答案

根据文档:https://docs.oracle.com/javase/tutorial/java/generics/types.html

In Java SE 7 and later, you can replace the type arguments required to invoke the constructor of a generic class with an empty set of type arguments (<>) as long as the compiler can determine, or infer, the type arguments from the context. This pair of angle brackets, <>, is informally called the diamond.

从 Java 7 开始,两者都是正确的。

关于java - Java中HashMap定义的有效性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47796727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com