diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/TTemplateCollections.java b/classlib/src/main/java/org/teavm/classlib/java/util/TTemplateCollections.java index 8f6dfdb3e..ed7ad959e 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/util/TTemplateCollections.java +++ b/classlib/src/main/java/org/teavm/classlib/java/util/TTemplateCollections.java @@ -201,7 +201,7 @@ class TTemplateCollections { for (T element : data) { Objects.requireNonNull(element); - int suggestedIndex = element.hashCode() % data.length; + int suggestedIndex = Math.abs(element.hashCode()) % data.length; int index = suggestedIndex; boolean found = false; while (index < data.length) { @@ -268,7 +268,7 @@ class TTemplateCollections { return false; } - int suggestedIndex = o.hashCode() % data.length; + int suggestedIndex = Math.abs(o.hashCode()) % data.length; for (int i = suggestedIndex; i < data.length; ++i) { if (data[i].equals(o)) { return true; @@ -413,16 +413,17 @@ class TTemplateCollections { private Entry[] data; private AbstractImmutableSet> entrySet; + @SuppressWarnings("unchecked") @SafeVarargs NEtriesMap(Entry... data) { - Entry[] table = data.clone(); + Entry[] table = new Entry[data.length]; Arrays.fill(table, null); for (Entry entry : data) { Objects.requireNonNull(entry.getKey()); Objects.requireNonNull(entry.getValue()); - int suggestedIndex = entry == null ? 0 : entry.getKey().hashCode() % data.length; + int suggestedIndex = Math.abs(entry.getKey().hashCode()) % data.length; int index = suggestedIndex; boolean found = false; while (index < data.length) { @@ -481,7 +482,7 @@ class TTemplateCollections { if (key == null) { return false; } - int suggestedIndex = key.hashCode() % data.length; + int suggestedIndex = Math.abs(key.hashCode()) % data.length; for (int i = suggestedIndex; i < data.length; ++i) { if (data[i].getKey().equals(key)) { return true; @@ -500,7 +501,7 @@ class TTemplateCollections { if (key == null) { return null; } - int suggestedIndex = key.hashCode() % data.length; + int suggestedIndex = Math.abs(key.hashCode()) % data.length; for (int i = suggestedIndex; i < data.length; ++i) { Entry entry = data[i]; if (entry.getKey().equals(key)) { @@ -561,7 +562,7 @@ class TTemplateCollections { if (key == null) { return false; } - int suggestedIndex = key.hashCode() % data.length; + int suggestedIndex = Math.abs(key.hashCode()) % data.length; for (int i = suggestedIndex; i < data.length; ++i) { Entry entry = data[i]; if (entry.getKey().equals(key)) {