Skip to content

Instantly share code, notes, and snippets.

@rednaxelafx
Last active July 2, 2018 06:42
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save rednaxelafx/930f8979473185cfc0a0 to your computer and use it in GitHub Desktop.
Save rednaxelafx/930f8979473185cfc0a0 to your computer and use it in GitHub Desktop.
JDK8: StackOverflowError of HashMap due to self reference
$ ~/sdk/jdk1.8.0/Contents/Home/bin/java HashMapStackOverflow
Exception in thread "main" java.lang.StackOverflowError
at java.util.AbstractMap.hashCode(AbstractMap.java:505)
at java.util.Objects.hashCode(Objects.java:98)
at java.util.HashMap$Node.hashCode(HashMap.java:296)
at java.util.AbstractMap.hashCode(AbstractMap.java:507)
at java.util.Objects.hashCode(Objects.java:98)
at java.util.HashMap$Node.hashCode(HashMap.java:296)
at java.util.AbstractMap.hashCode(AbstractMap.java:507)
at java.util.Objects.hashCode(Objects.java:98)
at java.util.HashMap$Node.hashCode(HashMap.java:296)
at java.util.AbstractMap.hashCode(AbstractMap.java:507)
at java.util.Objects.hashCode(Objects.java:98)
at java.util.HashMap$Node.hashCode(HashMap.java:296)
at java.util.AbstractMap.hashCode(AbstractMap.java:507)
at java.util.Objects.hashCode(Objects.java:98)
at java.util.HashMap$Node.hashCode(HashMap.java:296)
at java.util.AbstractMap.hashCode(AbstractMap.java:507)
...
import java.util.*;
public class HashMapStackOverflow {
public static void main(String[] args) throws Exception {
HashMap<String, Object> map = new HashMap<>();
map.put("self", map);
System.out.println(map.hashCode());
}
}
@Viyond
Copy link

Viyond commented Jun 28, 2017

nice~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment