简体   繁体   English

迭代器如何在 set(java) 中使用?

[英]How can Iterator can using in set(java)?

my question was why does iterator work on set?我的问题是为什么迭代器在集合上工作?

Here is my example code,这是我的示例代码,

public class Staticex {


    public static void main(String[] args) {
        HashSet set = new HashSet();

        set.add(1);
        set.add(2);
        set.add(3);
        set.add(4);
        set.add(5);

        Iterator iter = set.iterator();
        while (iter.hasNext()) {
            System.out.println(iter.next());
        }
    }
}

I understand, set is unordered, In contrast List我明白了,set 是无序的,相比之下 List

So, How can get the values one by one through an iterator?那么,如何通过迭代器一一获取值呢?

Is iterator changing set into like list which ordered data structure?迭代器是否将集合更改为有序数据结构的列表?

How can Iterator can using in set? Iterator如何在集合中使用?

Like you are using it.就像你正在使用它一样。

How can get the values one by one through an iterator?如何通过迭代器一一获取值?

Your code is doing that.您的代码正在这样做。

Is iterator changing set into like list which ordered data structure?迭代器是否将集合更改为有序数据结构的列表?

No.不。

The thing that you are missing is what "unordered" means.您缺少的是“无序”的含义。 It means that the order in which the (set's) elements are returned is not predictable 1 , and not specified in the javadocs.这意味着返回(集合)元素的顺序是不可预测的1 ,并且没有在 javadocs 中指定。 However each element will be returned once and (since the elements of a set are unique!) only once for the iteration.然而,每个元素被返回一次并且(因为集合的元素是唯一的!)迭代只返回一次


1 - Actually, this is not strictly true. 1 - 实际上,这并不完全正确。 If you have enough information about the element class, the element values, how they were created and how / when they were added to the HashSet , AND you analyze the specific HashSet implementation... it is possible that you CAN predict what the iteration order is going to be.如果您有足够的关于元素 class 的信息、元素值、它们是如何创建的以及它们是如何/何时添加到HashSet的,并且您分析了具体的HashSet实现......您可以预测迭代顺序将是。 For example if you create a HashSet<Integer> and add 1, 2, 3, 4, ... to it, you will see a clear (and repeatable) pattern when you iterate the elements.例如,如果您创建一个HashSet<Integer>并向其中添加 1, 2, 3, 4, ...,您将在迭代元素时看到一个清晰(且可重复)的模式。 This is in part due to the way that Integer.hashCode() is specified.这部分是由于Integer.hashCode()的指定方式。

Referring to the documentation , we see that:参考文档,我们看到:

Iterator<E> iterator()

Returns an iterator over the elements in this collection.返回此集合中元素的迭代器。 There are no guarantees concerning the order in which the elements are returned (unless this collection is an instance of some class that provides a guarantee).不保证返回元素的顺序(除非此集合是提供保证的某些 class 的实例)。


Since there are no guarantees concerning the order in which the elements are returned for iterator, it is not a problem for iterator to apply to Set, which is unordered.由于没有保证迭代器返回元素的顺序,因此迭代器应用于无序的 Set 是没有问题的。

Further, it is not changing the Set into a List此外,它不会将 Set 更改为 List

Set is unordered in a logical sense. Set在逻辑意义上是无序的。 When you have a bag of things, there isn't a sense of order when they are inside the bag.当你有一袋东西时,当它们在袋子里时,它们就没有秩序感。 But when you take each thing out of the bag, one at a time, you end up with some order.但是,当你从袋子里拿出每一件东西时,一次一件,你最终会得到一些订单。 And like the other answer has mentioned, you cannot rely on that order since it is purely accidental.就像另一个答案提到的那样,您不能依赖该顺序,因为它纯粹是偶然的。

I understand, set is unordered, In contrast List我明白了,set 是无序的,相比之下 List

This is not necessarily true.这不一定是真的。 SortedSet is a subinterface of Set. SortedSet是 Set 的子接口。 As the name implies, instances of this interface are ordered in some fashion.顾名思义,此接口的实例以某种方式排序。 For example, TreeSets are ordered using their natural ordering, or by a Comparator provided at set creation time, depending on which constructor is used.例如, TreeSet使用它们的自然顺序进行排序,或者通过在集合创建时提供的 Comparator 进行排序,具体取决于使用的构造函数。

Now, if you are talking specifically about HashSet, then you are correct about being unordered.现在,如果您专门谈论 HashSet,那么您对无序的看法是正确的。

I think your confusion is because you are asking yourself "why is the print out showing the numbers in numeric (insertion) order?"我认为您的困惑是因为您在问自己“为什么打印输出以数字(插入)顺序显示数字?” This is sort of a complicated answer for someone of your familiarization level, but the order in which they are printed out is because you are inserting integers and their hash code are basically their numeric values.对于您熟悉程度的人来说,这是一个复杂的答案,但它们被打印出来的顺序是因为您正在插入整数,它们的 hash 代码基本上是它们的数值。 And, although there is no guarantee as to the order in which the elements of the hash set are returned when iterating, the implementation of HashSet is backed by a hash table.而且,虽然不能保证 hash 集合的元素在迭代时返回的顺序,但 HashSet 的实现由 hash 表支持。 In fact, if you change the insertion order of those same values, most likely the numbers will be printed out in the same numeric order.事实上,如果您更改这些相同值的插入顺序,很可能这些数字将以相同的数字顺序打印出来。 Now, remember that with all that, the order is not guaranteed.现在,请记住,尽管如此,订单并不能得到保证。 This may not be true, for instance, if you change the set elements to be String objects.这可能不是真的,例如,如果您将 set 元素更改为 String 对象。

@Hanseo @Hanseo

When we do当我们这样做

set.iterator()

It will return iterator of keyset.它将返回键集的迭代器。

Here keyset is fetched from map这里的键集是从 map 获取的

map is object of LinkedHashMap which keeps track of insertion order. map 是 LinkedHashMap 的 object,它跟踪插入顺序。

You can dig more into by looking into HashSet implementation您可以通过查看 HashSet 实现来深入了解

在此处输入图像描述

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

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