Created
November 27, 2014 23:40
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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