Java

Java

Made by DeepSource

Adding elements of an entry set may fail due to reuse of Entry objects JS0425

Bug risk
Major

The entrySet() method is allowed to return a view of the underlying Map in which a single Entry object is reused and returned during the iteration.

As of Java 1.6, commonly used map structures may return the same entry object while iterating over their entrySet. When iterating through such a Map, the Entry value is only valid until you advance to the next iteration.

If, for example, you try to pass such an entrySet to an addAll method, things will go badly wrong.

Use a for loop and manually add all elements of the map instead.