SORU
22 ŞUBAT 2011, Salı


HashSet vs LinkedHashSet

Aralarındaki fark nedir? Biliyorum

Bir LinkedHashSet HashSet sıralı bir sürüm olduğunu tüm öğeleri arasında iki kat bağlantılı bir Listesini tutar. HashSet yerine bu sınıfı kullanın yineleme düzeni önemsiyorsan. Bir HashSet arasında yineleme zaman sipariş bir LinkedHashSet öğeleri arasında yineleme yapmak sağlar, ancak tahmin edilemez, sırayla takılı kaldılar.

Ama LinkedHashSet bu kaynak kod içinde sadece HashSet kurucusu diyor. -Çift bağlantılı Liste ve ekleme düzeni bu mu?

CEVAP
22 ŞUBAT 2011, Salı


Cevap yatıyorkurucularLinkedHashSet kullandığı temel sınıf oluşturmak için:

public LinkedHashSet(int initialCapacity, float loadFactor) {
    super(initialCapacity, loadFactor, true);      // <-- boolean dummy argument
}

...

public LinkedHashSet(int initialCapacity) {
    super(initialCapacity, .75f, true);            // <-- boolean dummy argument
}

...

public LinkedHashSet() {
    super(16, .75f, true);                         // <-- boolean dummy argument
}

...

public LinkedHashSet(Collection<? extends E> c) {
    super(Math.max(2*c.size(), 11), .75f, true);   // <-- boolean dummy argument
    addAll(c);
}

Ve (örnek) boolean bir değişken alır HashSet yapıcı tarif edilir, ve bu gibi görünüyor:

/**
 * Constructs a new, empty linked hash set.  (This package private
 * constructor is only used by LinkedHashSet.) The backing
 * HashMap instance is a LinkedHashMap with the specified initial
 * capacity and the specified load factor.
 *
 * @param      initialCapacity   the initial capacity of the hash map
 * @param      loadFactor        the load factor of the hash map
 * @param      dummy             ignored (distinguishes this
 *             constructor from other int, float constructor.)
 * @throws     IllegalArgumentException if the initial capacity is less
 *             than zero, or if the load factor is nonpositive
 */
HashSet(int initialCapacity, float loadFactor, boolean dummy) {
    map = new LinkedHashMap<E,Object>(initialCapacity, loadFactor);
}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • akalyne

    akalyne

    13 Mayıs 2009
  • hockeywebcasts

    hockeywebcas

    31 EKİM 2012
  • LG Mobile Global

    LG Mobile Gl

    2 EYLÜL 2010