classlib: fix AbstractSet.hashCode

This commit is contained in:
Alexey Andreev 2022-08-12 16:47:59 +02:00
parent 51603695f4
commit 10415b356e
2 changed files with 21 additions and 3 deletions

View File

@ -15,8 +15,6 @@
*/
package org.teavm.classlib.java.util;
import java.util.Arrays;
public abstract class TAbstractSet<E> extends TAbstractCollection<E> implements TSet<E> {
public TAbstractSet() {
super();
@ -67,6 +65,13 @@ public abstract class TAbstractSet<E> extends TAbstractCollection<E> implements
@Override
public int hashCode() {
return Arrays.hashCode(toArray());
int result = 0;
for (TIterator<E> iter = iterator(); iter.hasNext();) {
E e = iter.next();
if (e != null) {
result += e.hashCode();
}
}
return result;
}
}

View File

@ -20,6 +20,7 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.LinkedHashSet;
import java.util.Set;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -121,4 +122,16 @@ public class SetTest {
assertNull("Iterator did not return all of expected elements", e);
}
}
@Test
public void hashCodeTest() {
Set<String> a = new LinkedHashSet<>();
a.add("foo");
a.add("bar");
Set<String> b = new LinkedHashSet<>();
b.add("bar");
b.add("foo");
assertEquals(a.hashCode(), b.hashCode());
}
}