将整数插入 TreeMap

发布于 2024-11-17 20:13:17 字数 455 浏览 2 评论 0原文

我想存储项目的 ID 及其相应的坐标。为此,我使用 TreeMap,其中坐标是包含 int x 和 int y 的类。现在,为了将数据插入到地图中,我可以写:

treeMapObject.put(5,new BasicRow(30,90));

或者我必须写:

treeMapObject.put(new Integer(5),new BasicRow(30,90));

我猜只有第二个是正确的,因为地图处理对象。但现在的问题是,假设我有以下代码:

treeMapObject.put(new Integer(5),new BasicRow(30,90));
treeMapObject.put(new Integer(5),new BasicRow(45,85));

在这种情况下会发生什么?

I want to store the IDs of items and their corresponding co-ordinates. For this, I'm using a TreeMap where Coordinates is a class containing int x and int y. Now, for inserting data into the map can I write:

treeMapObject.put(5,new BasicRow(30,90));

Or do I have to write:

treeMapObject.put(new Integer(5),new BasicRow(30,90));

I guess only the second one is correct because Maps deal with objects. But now the question is, say I have the following piece of code:

treeMapObject.put(new Integer(5),new BasicRow(30,90));
treeMapObject.put(new Integer(5),new BasicRow(45,85));

In such a case what will happen?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小鸟爱天空丶 2024-11-24 20:13:17

实际上这两个版本都是正确的,因为你的Java编译器将为你执行所谓的“自动装箱”:如果你提供一个int,而需要一个Integer,java会自动为你将该int包装在一个Integer对象中。反之亦然(已在 Java 5 中引入。如果您使用甚至不应该编译的旧 Java 版本)。

对于您的第二个问题:您首先添加的条目将被覆盖。

actually both versions are correct, because your Java compiler will perform what is called "autoboxing" for you: If you supply an int, where an Integer is required, java will automatically wrap that int in an Integer object for you. This works vice-versa as well (has been introduced in Java 5. If you'd use an older Java version that should not even compile.).

For your second question: The entry you added first will be overwritten.

巴黎夜雨 2024-11-24 20:13:17

yankee 是对的,两者都可以工作,因为 Java 将 autobox< /a> 将整数值转换为 Integer 对象。

请注意,如果您想显式执行此操作,最好编写:

Integer a = Integer.valueOf(5);

而不是:

Integer a = new Integer(5);

如果您使用 valueOf,则类 Integer 可以避免创建新的 Integer 对象,它将从其内部缓存中返回一个预先存在的 Integer 对象,这比创建新对象更有效。

yankee is right, both will work, because Java will autobox the integer values into an Integer object.

Note that if you want to do this explicitly, it is better to write:

Integer a = Integer.valueOf(5);

instead of:

Integer a = new Integer(5);

If you use valueOf, then class Integer can avoid creating a new Integer object, it will return a pre-existing Integer object from its internal cache, which is more efficient than creating a new object.

剪不断理还乱 2024-11-24 20:13:17

存在轻微差异,但不会影响地图的行为。在这两种情况下,如果您尝试获取整数 5 的值,映射将返回该基本行。

区别:第二行总是创建一个新的 Integer 实例,而第一行可能会插入一个现有实例。但地图是通过平等进行比较,而不是身份,所以这实际上并不重要。只是第一行对内存消耗有(非常)小的积极影响。

There is a slight difference, which does not affect the behaviour of the map. In both cases, if you try to get the value for integer 5, the map will return that basic row.

The difference: the second line will always create a new instance of Integer while the first one may insert an existing instance. But maps compare by equality, not by identity, so it actually doesn't matter. Just that the first line has a (very) small postive impact on memory consumption.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文