Question

I am using HashSet collection since I am interested in unique (no duplicate) elements. I noticed that I can get all elements of a HashSet using an iterator. My (simple) question is whether I can retrieve the same element twice (or more times). To be more specific in the following code

HashSet<String> vectors = new HashSet<String>();
Iterator iterator = vectors.iterator(); 
String temp;
while (iterator.hasNext()){
    temp = (String) iterator.next();
    System.out.println("Next element inside HashSet is " + temp);
    out.write(temp + "\n");
}

I am using temp to store the value of HashSet and to use it twice (one time to display it and one time to write it to a stream. Is there a way to stay in the same element? I think if I use a syntax like:

System.out.println("Next element inside HashSet is " + iterator.next());
out.write(iterator.next() + "\n");

I will get only have the elements (the other half will be displayed only). Isn't that the case?

Était-ce utile?

La solution

Since you are only reading the elements of the Set, I would recommend a for-each loop, which is basically the same as your code (the first snippet) but has been designed for this kind of situations (and is more readable IMO).

HashSet<String> vectors = new HashSet<String>();
for(String temp : vectors){
    System.out.println("Next element inside HashSet is " + temp);
    out.write(temp + "\n");
}

Autres conseils

In HashSet.iterator there is no way to retrieve an element without moving on to the next one.

If you want something like this consider using a stack with pop, push and top methods.

although, I see no reason to do what you have done with temp. Its normal, elegant and straight-forward.

@Eypros, When you call iterator.next() method, It fetches element and move next that's why you will get half elements.

itt.next() call return current element from itr,move itr pointer by one. and if you call it again it will return you next element not same.

The Iterator from HashSet will never return the same String twice. However you can use the returned value as many times as needed after assigning it to the variable, how you have already done.

If you call the next() on the iterator twice, it returns the two subsequent elements (or throws NoSuchElementException if there are not enough elements remaining).

Your mistake is probably you assume Iterator.hasNext() changes the state of iterator, advancing to the next position. hasNext() does not change the iterator state and can be called any number of times. For instance, to print a comma separated list without comma at the end:

while (iterator.hasNext()){
    System.out.print(iterator.next());
    if (iterator.hasNext()) {
       System.out.print(iterator.next());
    }
}
System.out.println("Next element inside HashSet is " + iterator.next());
out.write(iterator.next() + "\n");

This syntex will give two different elements. If you want same element then either you have to store in "Temp" like variable or use for-each loop.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top