Jiri Mares wrote: >> Javadoc k contains() i remove() se dedi ze Set, > > To neni pravda. Vynatek ze zdrojaku JDK6 k tride HashSet: > Ja tu nic o dedeni javadocu nevidim.
Aha, omlouvam se za nepresnost, nedival jsem se do zdrojaku.
Nicmene javadoc ke contains() u HashSet jsou *stejne* jako u Set
a dokonce u Collection. A podle mne contains u HashSet porusuje
kontrakt od Collection. Zkuste si tohle:
import java.util.*;
import java.util.concurrent.CopyOnWriteArraySet;
public class MutovaniHash {
public static void main(String[] args) {
List<Integer> list;
Set<List<Integer>> set;
list = new ArrayList<Integer>();
set = new HashSet<List<Integer>>();
test(set, list);
list = new ArrayList<Integer>();
set = new CopyOnWriteArraySet<List<Integer>>();
test(set, list);
}
private static void test(Collection<List<Integer>> collection, List<Integer>
list) {
collection.add(list);
list.add(1);
System.out.println("collection = " +
collection.getClass().getSimpleName());
System.out.println("collection.contains(list) = " +
collection.contains(list));
for(List<Integer> prvek : collection) {
if(prvek.equals(list)) {
System.out.println("list je obsazen ");
System.out.println("prvek.hashCode() = " + prvek.hashCode());
System.out.println("list.hashCode() = " + list.hashCode());
}
}
}
}
Dostanete:
collection = HashSet
collection.contains(list) = false
list je obsazen
prvek.hashCode() = 32
list.hashCode() = 32
collection = CopyOnWriteArraySet
collection.contains(list) = true
list je obsazen
prvek.hashCode() = 32
list.hashCode() = 32
Cili HashSet ten zmutovany list *obsahuje* (contains),
protoze ho muzu najit iteratorem, pri porovnani equals()
dostanu true a hashCode() vraci to same, cili plni kontrakt.
Ale zaroven jeji metoda contains() vraci false, ackoliv jine
implementace Set se chovaji korektne.
IMHO tedy je javadoc ke contains() u HashSet spatne.
Makub
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Supercomputing Center Brno Martin Kuba
Institute of Computer Science email: [EMAIL PROTECTED]
Masaryk University http://www.ics.muni.cz/~makub/
Botanicka 68a, 60200 Brno, CZ mobil: +420-603-533775
--------------------------------------------------------------
smime.p7s
Description: S/MIME Cryptographic Signature
