确定性是什么意思?

发布于 2024-09-15 16:20:09 字数 151 浏览 7 评论 0原文

我正在阅读Java Hashmap文档,但我不明白这句话。

请注意,迭代顺序 HashMap 是不确定的。如果你 想要确定性迭代,请使用 LinkedHashMap。

确定性是什么意思?

I am reading the Java Hashmap documentation but I don't understand this sentence.

Note that the iteration order for
HashMap is non-deterministic. If you
want deterministic iteration, use
LinkedHashMap.

What does deterministic mean?

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

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

发布评论

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

评论(11

时光无声 2024-09-22 16:20:10

最简单的定义:

给定相同的输入,您总是得到相同的输出。

上面说的是,即使您没有更改任何内容,迭代完全相同的 HashMap 也可能在不同时间给出不同的结果。通常这并不重要,但如果确实如此,您应该使用 LinkedHashMap。

The simplest definition:

Given the same inputs, you always get the same outputs.

Above, it's saying that iterating through the exact same HashMap may give different results at different times, even when you haven't changed anything. Usually that doesn't matter, but if it does, you should use a LinkedHashMap.

本宫微胖 2024-09-22 16:20:10

按照可以提前“确定”的顺序。

由于散列的工作方式,映射中的元素被“扰乱”到任意位置。加扰位置无法轻易提前确定——它们是不可确定的——你不知道最终的顺序。

In an order which can be "determined" in advance.

Because of the way hashing works, the elements in the map are "scrambled" into arbitrary locations. The scrambling positions cannot easily be determined in advance -- they aren't determinable -- you don't know the resulting order.

仅此而已 2024-09-22 16:20:10

简单来说:当您调用keys()、values() 或entrySet() 时,您将返回一个集合,您可以在该集合上进行迭代。该行表示您不能期望迭代器返回对象的顺序是任何特定的顺序。特别是,它可以不同于插入顺序和按键值的自然排序。

如果您希望迭代器按插入顺序工作,请使用 LinkedHashMap。如果要按键值进行迭代,请使用 TreeMap。请注意,这两种方法的性能都比普通 HashMap 稍差,因为它们都必须做额外的工作来跟踪顺序。

In simpler terms: When you call keys(), values() or entrySet() you get back a collection, over which you can iterate. That line is saying you can't expect the order in which the iterator returns objects will be any particular order. Especially, it can be different from both the insertion order and the natural ordering by key values.

If you want the iterator to work in insertion order, use a LinkedHashMap. If you want to iterate by key value, use a TreeMap. Be aware that both of these have slightly worse performance than a plain HashMap, as they both have to do extra work to keep track of the order.

丢了幸福的猪 2024-09-22 16:20:10

严格来说,HashMap 迭代顺序几乎可以肯定是不是不确定的。与绝大多数计算过程一样,如果您以完全相同的方式进行计算,结果将完全相同。真正的非确定性系统会包含一些外部随机元素,但这里的情况极不可能出现。至少在大多数情况下是这样。

我认为,它们的真正含义是,仅仅因为地图包含一组特定的元素,您就不应该期望当您迭代它们时它们会以特定的顺序出现。这并不意味着迭代的顺序是随机的,它只是意味着作为开发人员,您不应该指望知道它是什么。

在大多数情况下,其原因是对某些实现细节的依赖可能因平台和/或访问顺序而异。而后者又可能由线程调度和事件计时决定,而这些本质上是不可预测的。

在大多数情况下,在任何单独的平台上,使用最常见的线程模型(单线程应用程序),如果您总是在序列 X 中插入和删除一组特定的内容,那么您总是会在序列 Y 中取出它们。 Y 将完全依赖于 X 和平台,以至于没有必要考虑它会是什么。

基本上,即使它不是随机的,但也可能是随机的。

Strictly speaking, HashMap iteration order is almost certainly not non-deterministic. Like the vast majority of computational processes, if you go through it exactly the same way, the results will be exactly the same. A truly non-deterministic system would incorporate some external random element, which is highly unlikely to be the case here. At least in most cases.

What they really mean, I think, is that just because the map contains a particular set of elements, you shouldn't expect that when you iterate over them they will come out in a particular order. That doesn't mean the order of iteration is random, it just means that as a developer you shouldn't bank on knowing what it is.

In most cases, the reason for this is that there will be some dependency on some implementation details that can vary from platform to platform, and/or on order of access. And the latter may in turn be determined by thread scheduling and event timing, which are innately unpredictable.

In most cases, on any individual platform and with the most common threading model -- a single threaded application -- if you always insert and delete a particular set of things in sequence X you will always get them out in sequence Y. It's just that Y will be so exactly dependent on X, and on the platform, that there's no point even thinking about what it's going to be.

Basically, even though it isn't random, it might just as well be.

波浪屿的海角声 2024-09-22 16:20:10

确定性:可以确定
不确定性:无法确定

deterministic : can be determined
non-deterministic : can't be determined

坏尐絯℡ 2024-09-22 16:20:10

这是一种算法,当给定特定输入时将产生相同的输出。

我发现的一个很好的例子:

考虑一个购物清单:
要购买的物品。

可以有两种解释:

* 以任意顺序购买所有这些物品的说明。 
   这是一种非确定性算法。
* 按照给出的顺序购买所有这些物品的说明。这是一个 
   确定性算法。

It's an algorithm that when given a particular input will produce the same output.

A good example I found:

Consider a shopping list: a list of
items to buy.

It can be interpreted in two ways:

* The instruction to buy all of those items, in any order. 
   This is a nondeterministic algorithm.
* The instruction to buy all of those items, in the order given. This is a 
   deterministic algorithm.
甜尕妞 2024-09-22 16:20:10

确定性意味着结果是可预测的/可预见的。

Deterministic means the result is predictable / forseeable.

陌若浮生 2024-09-22 16:20:10

非确定性意味着没有一个结果是您可以事先计算出来的。算术表达式(如 1 + 2 或 log e)是确定性的。只有一个正确答案,您可以预先算出。将一把沙子扔到空中,每粒沙子掉落的位置对于任何主要精度来说都是有效不确定的。

这可能并不完全正确,因为您可以查看底层库和 JVM 实现的源代码,可能有某种方法可以确定所产生的顺序。对他们来说,“不保证特定的顺序”或诸如此类的说法可能更正确。

在这种情况下,重要的是您不能依赖顺序。

Non-deterministic means that there isn't one single result that you can figure it out beforehand. An arithmetical expression, like 1 + 2 or log e, is deterministic. There's exactly one correct answer and you can figure it out upfront. Throw a handful of sand in the air, and where each grain will fall is effectively non-deterministic for any major degree of accuracy.

This probably isn't precisely correct, as you could look at the source code of the underlying library and JVM implementation and there would probably be some way that you could determine the ordering that would result. It might be more correct for them to say,"No particular order is guaranteed," or something of that sort.

What's relevant in this case is that you can't rely on the ordering.

几味少女 2024-09-22 16:20:10

这是 HashMap 的属性,其中元素不会按照插入顺序进行迭代,因为 HashMap 不会按顺序插入元素。因此文档中的行

This is the property of HashMap where elements are not iterated in the same order in which they were inserted as HashMap does not insert elements in order. Therefore the line in documentation

初熏 2024-09-22 16:20:10

非确定性意味着没有明确定义的行为。

对于 HashMap,根据您插入元素的方式,您可能会有一种或另一种迭代顺序。

Non deterministic means there is no well defined behaviour.

In the case of the HashMap depending on how you inserted elements you might have the one or other order of iteration.

九厘米的零° 2024-09-22 16:20:10

HashMap 不维护添加内容的顺序,如果您希望输出按照添加内容的顺序,则应该使用 LinkedHashMap,因此 确定性 意味着按顺序输出您添加的内容。

下面是示例:
1.非确定性

    HashMap<String, Integer> map = new HashMap<String,Integer>();
    map.put("a",5);
    map.put("b",16);
    map.put("c",46);
    System.out.println(map); //ouptput:{a=5, c=46, b=16}

2.确定性

HashMap<String, Integer> map = new LinkedHashMap<String,Integer>();
            map.put("a",5);
            map.put("b",16);
            map.put("c",46);
            System.out.println(map); //output:{a=5, b=16, c=46}

HashMap doesn't maintain order what you add, if you want your output be the order what you add, you should use LinkedHashMap, so deterministic means output orderdly what you add in.

Here is example:
1.non-deterministic

    HashMap<String, Integer> map = new HashMap<String,Integer>();
    map.put("a",5);
    map.put("b",16);
    map.put("c",46);
    System.out.println(map); //ouptput:{a=5, c=46, b=16}

2.deterministic

HashMap<String, Integer> map = new LinkedHashMap<String,Integer>();
            map.put("a",5);
            map.put("b",16);
            map.put("c",46);
            System.out.println(map); //output:{a=5, b=16, c=46}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文