nntp2http.com
Posting
Suche
Optionen
Hilfe & Kontakt

HashSet spinnt!

Von: willybrandt (willybrandt@gmx.de) [Profil]
Datum: 03.07.2008 12:37
Message-ID: <e942e5be-be69-49d4-aea1-89cf527975ad@d45g2000hsc.googlegroups.com>
Newsgroup: de.comp.lang.java
Hallo d/c/l/j

In der javadoc von java.util.Set heißt es:
A collection that contains no duplicate elements. More formally, sets
contain no pair of elements e1 and e2 such that e1.equals(e2), and at
most one null element.

Soweit so gut. Jetzt habe ich hier ein kleines Testsnippet, das für
HashSet genau das Gegenteil beweist:
---CODE ON---
public class MyClass {
public static void main(String[] args) {
MyClass a = new MyClass();
MyClass b = new MyClass();

Set<MyClass> myHashSet = new HashSet<MyClass>();
myHashSet.add(a);
myHashSet.add(b);
System.out.println("myHashSet.size()="+myHashSet.size());
}

@Override
public boolean equals(Object obj) {
System.out.println("bin da!");
return true;
}
}
---CODE OFF---

Auf meiner Eclipse 3.3.1.1 Platform mit JRE 1.6.0_04 bekomme ich
myHashSet.size()=2
als Ausgabe. Laut javadoc müßte aber eigentlich
bin da!
myHashSet.size()=1
rauskommen... Was zur Hölle ist da los???

Gruß,
Alfred Schnabel

[ Auf dieses Posting antworten ]

Antworten