ホームページ >Java >&#&チュートリアル >Javaのハッシュテーブルと同期マップの違い
とHashtable
の重要な区別を調査します。 A SynchronizedMap
は、インデックス計算を介してアクセスされる一連のスロットを使用して値にキーを効率的にマップし、高速データ検索を提供します。逆に、a Hashtable
はマップを同期してスレッドの安全性を確保し、同時アクセスの問題を防ぎます。 重要なことに、SynchronizedMap
はnull値を許可しません。SynchronizedMap
<code>[ARB, RDD, KOL, DHKA]</code>出力の例
<code>Hashtable Insertion Order: [ARB, RDD, KOL, DHKA] SynchronizedMap Insertion Order: [ARB, RDD, DHKA, KOL]</code>方法論
アルゴリズムの手順:
Hashtable
SynchronizedMap
結果を出力します。TreeSet
vs.IdentityHashMap
比較
<code class="language-java">TreeSet<integer> stTree = new TreeSet<>(); stTree.add(4); stTree.add(5); stTree.add(6); stTree.add(8); stTree.add(4); IdentityHashMap<integer string> ihmap = new IdentityHashMap<>(); ihmap.put(10, "ARB"); ihmap.put(20, "RDD"); ihmap.put(30, "ARBRDD"); ihmap.put(40, "KOLDHKA"); ihmap.put(50, "You"); System.out.println("IdentityHashMap size: " + ihmap.size()); System.out.println("Initial IdentityHashMap: " + ihmap); Hashtable<integer string> table = new Hashtable<>(); table.put(1, "X"); table.put(2, "Y"); table.put(3, "Z"); table.put(4, "A"); for (Map.Entry<integer string> m : table.entrySet()) { Iterator<map.entry string>> itr = ihmap.entrySet().iterator(); while (itr.hasNext()) { IdentityHashMap.Entry<integer string> entry = itr.next(); TreeMap<integer integer> maptree = new TreeMap<>(); maptree.put(2, 5); maptree.put(3, 6); maptree.put(4, 6); maptree.put(2, 3); } }</integer></integer></map.entry></integer></integer></integer></integer></code>
collections.synchronizedMap()
Hashtable
は、組み込みのスレッドの安全性を提供しますが、パフォーマンスの罰則に苦しむ可能性があります。 SynchronizedMap
は、より柔軟なアプローチを提供し、マップの実装を同期させることができますが、パフォーマンスオーバーヘッドを導入します。 Hashtable
は一般にレガシークラスと見なされ、SynchronizedMap
または同時コレクション(ConcurrentHashMap
など)が最新のJava開発に優先されます。
キーワードを使用して、1つのスレッドのみが特定のオブジェクトのメソッドに一度にアクセスできるようにします。
オブジェクトレベルの同期を使用した例synchronized
例
HashMap
結論Collections.synchronizedMap()
<code>[ARB, RDD, KOL, DHKA]</code>と
は一般に新しいコードに対して落胆します。Hashtable
以上がJavaのハッシュテーブルと同期マップの違いの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。