javalangIllegalStateException Duplicate key
The exception java.lang.IllegalStateException: Duplicate key is thrown when you try to insert a duplicate key into a data structure that does not allow duplicates, such as a Set or a Map with unique keys.
Here are a few possible reasons for this exception:
-
Inserting a duplicate key into a
Set: If you are using aSetimplementation likeHashSetorTreeSet, they do not allow duplicate elements. So, if you try to add an element that already exists in the set, it will throw this exception. -
Inserting a duplicate key into a
Map: If you are using aMapimplementation likeHashMaporTreeMap, they do not allow duplicate keys. So, if you try to put a key-value pair with a key that already exists in the map, it will throw this exception. -
Using a custom class as a key without overriding
hashCodeandequalsmethods: If you are using a custom class as a key in aSetorMap, you need to make sure that you have properly implemented thehashCodeandequalsmethods. If these methods are not implemented correctly, the data structure may not be able to detect duplicate keys, leading to this exception.
To fix this exception, you should review your code and make sure that you are not inserting duplicate keys into a data structure that does not allow duplicates. If you are using a custom class as a key, make sure to override the hashCode and equals methods properly
原文地址: http://www.cveoy.top/t/topic/hNZ2 著作权归作者所有。请勿转载和采集!