Skip to content

Instantly share code, notes, and snippets.

@linead
Created November 27, 2014 23:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save linead/807eda8385d8ce56a6f7 to your computer and use it in GitHub Desktop.
Save linead/807eda8385d8ce56a6f7 to your computer and use it in GitHub Desktop.
Demonstrating the difference in order when iterating a HashSet in java 6/7 to java 8
import java.util.*;
/**
* Results :
*
* /usr/java/jdk1.8.0/bin/java
* First = EEEE last = DDDD
*
* /usr/java/jdk1.7.0_40/bin/java
* First = IIII last = HHHH
*
* /usr/java/jdk1.6.0_45/bin/java
* First = IIII last = HHHH
*
*
*/
public class Test {
public static void main(String[] args) {
HashSet<String> strings = new HashSet<String>();
strings.add("AAAA");
strings.add("BBBB");
strings.add("CCCC");
strings.add("DDDD");
strings.add("EEEE");
strings.add("FFFF");
strings.add("GGGG");
strings.add("HHHH");
strings.add("IIII");
String first = null;
String last = null;
for(String s : strings) {
if(first == null) {
first = s;
}
last = s;
}
System.err.println("First = " + first + " last = " + last);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment