Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@Viyond Viyond commented Jun 28, 2017

nice~

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