简体   繁体   English

确定性是什么意思?

[英]What does deterministic mean?

I am reading the Java Hashmap documentation but I don't understand this sentence. 我正在阅读Java Hashmap文档,但我不明白这句话。

Note that the iteration order for HashMap is non-deterministic. 请注意,HashMap的迭代顺序是非确定性的。 If you want deterministic iteration, use LinkedHashMap. 如果您想要确定性迭代,请使用LinkedHashMap。

What does deterministic mean? 确定性是什么意思?

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. 上面,它表示迭代完全相同的HashMap可能会在不同的时间给出不同的结果,即使你没有改变任何东西。 Usually that doesn't matter, but if it does, you should use a LinkedHashMap. 通常这无关紧要,但如果确实如此,则应使用LinkedHashMap。

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. 加扰位置不能提前确定 - 它们是不可确定的 - 您不知道产生的顺序。

In simpler terms: When you call keys(), values() or entrySet() you get back a collection, over which you can iterate. 简单来说:当你调用keys(),values()或entrySet()时,你会得到一个可以迭代的集合。 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. 如果希望迭代器按插入顺序工作,请使用LinkedHashMap。 If you want to iterate by key value, use a TreeMap. 如果要按键值进行迭代,请使用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. 请注意,这两者的性能都比普通的HashMap略差,因为它们都需要做额外的工作来跟踪顺序。

Strictly speaking, HashMap iteration order is almost certainly not non-deterministic. 严格地说,HashMap迭代顺序几乎肯定不是非确定性的。 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. 在大多数情况下,在任何单独的平台上,使用最常见的线程模型 - 单线程应用程序 - 如果你总是在序列X中插入和删除一组特定的东西,你将始终按顺序Y将它们输出。这只是Y将完全依赖于X,并且在平台上,即使考虑它将会是什么也没有意义。

Basically, even though it isn't random, it might just as well be. 基本上,即使它不是随机的,它也可能是。

deterministic : can be determined 确定性:可以确定
non-deterministic : can't be determined 非确定性:无法确定

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. 

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

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. 算术表达式,如1 + 2或log e,是确定性的。 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. 这可能并不完全正确,因为您可以查看底层库和JVM实现的源代码,并且可能有某种方法可以确定将导致的排序。 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. 在这种情况下,相关的是您不能依赖订购。

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. 这是HashMap的属性,其中元素的迭代次数与插入的顺序不同,因为HashMap不按顺序插入元素。 Therefore the line in documentation 因此在文档中的行

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. 在HashMap的情况下,根据您插入元素的方式,您可能具有迭代的一个或其他顺序。

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. HashMap不维护您添加的顺序,如果您希望输出是您添加的顺序,则应使用LinkedHashMap,因此deterministic意味着按顺序输出您添加的内容。

Here is example: 1.non-deterministic 这是一个例子: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.deterministic 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}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM