Java集合フレームワーク学習(四)LinkedHashSet詳細

1594 ワード

LinkedHashSetの紹介
先にHashSetとTreeSetを紹介しました.
LinkedHashSetもSetインタフェースの実装クラスであり、HashSet public class LinkedHashSetも継承されています.
extends HashSet
implements Set, Cloneable, Serializable
HashSet、TreeSetと似ています.以下が特徴です.
1.LinkedHashSetは、反復順序が予知可能なSetインタフェースを有するハッシュテーブルおよびリンクリスト実装である.
この実装は、HashSetとは異なり、後者はすべてのエントリで実行される二重リンクリストを維持している.
このリンク・リストは、挿入順序またはアクセス順序の反復順序を定義します.
2.LinkedHashSetの下位層はLinkedHashMapを使用してすべての要素を保存します.これはHashSetと継承され、そのすべての方法はHashSetと同じです.
3.LinkedHashSetも非スレッドで安全です.
4.null要素を許可します.
LinkedHashSet例
package com.dylan.collection;

import java.util.LinkedHashSet;

/**
 * @author xusucheng
 * @create 2018-01-27
 **/
public class LinkedHashSetExample {
    public static void main(String args[]) {
        // LinkedHashSet of String Type
        LinkedHashSet lhset = new LinkedHashSet();

        // Adding elements to the LinkedHashSet
        lhset.add("Z");
        lhset.add("PQ");
        lhset.add("N");
        lhset.add("O");
        lhset.add("KK");
        lhset.add("FGH");
        System.out.println(lhset);

        // LinkedHashSet of Integer Type
        LinkedHashSet lhset2 = new LinkedHashSet();

        // Adding elements
        lhset2.add(99);
        lhset2.add(7);
        lhset2.add(0);
        lhset2.add(67);
        lhset2.add(89);
        lhset2.add(66);
        System.out.println(lhset2);
    }
}

出力:
[Z, PQ, N, O, KK, FGH] [99, 7, 0, 67, 89, 66]