0

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

So, How can get the values ​​one by one through an iterator?

Is iterator changing set into like list which ordered data structure?

4
  • I think iterator is part of Collections of which Set is? Commented Dec 30, 2021 at 5:24
  • What do you mean by set is unordered ? Commented Dec 30, 2021 at 5:25
  • An Iterator does not imply/assume ordering. The point is just to go through all elements. In a list (or other ordered collections, which may include other sets), the order of iteration is predictable. But with a HashSet, it is not. But that has nothing to do with whether it can use an iterator or not. Commented Dec 30, 2021 at 5:30
  • Set interface implements the Iterable interface in java, so an iterator is available. A hash set uses HashMaps for internal storage, there is an iterator available on the keys. Commented Dec 30, 2021 at 5:31

4 Answers 4

3

How can Iterator can using in set?

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 predictable1, and not specified in the 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. 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. 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. This is in part due to the way that Integer.hashCode() is specified.

Sign up to request clarification or add additional context in comments.

1 Comment

The last part is pretty much what I posted. I think some developers read in the Javadoc that the set makes "no guarantees as to the iteration order of the set" and assume this means "unordered". They even fail to realize that SortedSet is a subinterface of Set and those certainly are ordered.
1

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).


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.

Further, it is not changing the Set into a List

Comments

0

Set is unordered in a logical sense. 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.

2 Comments

This is not necessarily true. See my post.
Also, Set is not akin with a "bag". Mathematically speaking, a set is a collection of distinct entities; emphasis in the word "distinct". That's what makes a set different than a list. Albeit, sets can be arranged in no logical order. But that doesn't mean that all sets must comply with this "unordered" rule. People who collect sets of coins or stamps do often arrange them in some order.
0

I understand, set is unordered, In contrast List

This is not necessarily true. SortedSet is a subinterface of 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. Also, the main distinction between Set and List is that List allows for duplicate objects to be contained, whereas Set does not.

Now, if you are talking specifically about HashSet, then you are correct about being unordered.

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. 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. 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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.