黄色网址大全免费-黄色网址你懂得-黄色网址你懂的-黄色网址有那些-免费超爽视频-免费大片黄国产在线观看

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節(jié)點LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 學(xué)習(xí)攻略 Java學(xué)習(xí) Java哈希表的詳細(xì)介紹

Java哈希表的詳細(xì)介紹

更新時間:2022-10-27 10:03:53 來源:動力節(jié)點 瀏覽1328次

Hashtable類實現(xiàn)了一個哈希表,它將鍵映射到值。任何非空對象都可以用作鍵或值。要成功地從哈希表中存儲和檢索對象,用作鍵的對象必須實現(xiàn) hashCode 方法和 equals 方法。

哈希表的特點

它類似于 HashMap,但是是同步的。

Hashtable 將鍵/值對存儲在哈希表中。

在 Hashtable 中,我們指定一個用作鍵的對象,以及我們要與該鍵關(guān)聯(lián)的值。然后對鍵進行哈希處理,生成的哈希碼用作值存儲在表中的索引。

Hashtable 類的初始默認(rèn)容量為 11,而 loadFactor 為 0.75。

HashMap 不提供任何枚舉,而 Hashtable 不提供快速失敗的枚舉。

宣言:

公共類 Hashtable<K,V> 擴展 Dictionary<K,V> 實現(xiàn) Map<K,V>、Cloneable、Serializable

類型參數(shù):

K - 此映射維護的鍵的類型

V – 映射值的類型

哈希表的層次結(jié)構(gòu)

Hashtable 實現(xiàn)了 Serializable、Cloneable、Map<K,V>接口并擴展了Dictionary<K,V>。直接子類是Properties,UIDefaults。

構(gòu)造函數(shù):

為了創(chuàng)建一個 Hashtable,我們需要從java.util.Hashtable導(dǎo)入它。我們可以通過多種方式創(chuàng)建 Hashtable。

1. Hashtable():這將創(chuàng)建一個空的哈希表,默認(rèn)加載因子為 0.75,初始容量為 11。

Hashtable<K, V> ht = new Hashtable<K, V>();
// Java program to demonstrate
// adding elements to Hashtable
import java.io.*;
import java.util.*;
class AddElementsToHashtable {
	public static void main(String args[])
	{
		// No need to mention the
		// Generic type twice
		Hashtable<Integer, String> ht1 = new Hashtable<>();
		// Initialization of a Hashtable
		// using Generics
		Hashtable<Integer, String> ht2
			= new Hashtable<Integer, String>();
		// Inserting the Elements
		// using put() method
		ht1.put(1, "one");
		ht1.put(2, "two");
		ht1.put(3, "three");
		ht2.put(4, "four");
		ht2.put(5, "five");
		ht2.put(6, "six");
		// Print mappings to the console
		System.out.println("Mappings of ht1 : " + ht1);
		System.out.println("Mappings of ht2 : " + ht2);
	}
}

輸出

ht1 的映射:{3=三,2=二,1=一}
ht2 的映射:{6=6, 5=5, 4=4}

2. Hashtable(int initialCapacity):這將創(chuàng)建一個哈希表,其初始大小由 initialCapacity 指定,默認(rèn)加載因子為 0.75。

Hashtable<K, V> ht = new Hashtable<K, V>(int initialCapacity);
// Java program to demonstrate
// adding elements to Hashtable
import java.io.*;
import java.util.*;
class AddElementsToHashtable {
	public static void main(String args[])
	{
		// No need to mention the
		// Generic type twice
		Hashtable<Integer, String> ht1 = new Hashtable<>(4);
		// Initialization of a Hashtable
		// using Generics
		Hashtable<Integer, String> ht2
			= new Hashtable<Integer, String>(2);
		// Inserting the Elements
		// using put() method
		ht1.put(1, "one");
		ht1.put(2, "two");
		ht1.put(3, "three");
		ht2.put(4, "four");
		ht2.put(5, "five");
		ht2.put(6, "six");
		// Print mappings to the console
		System.out.println("Mappings of ht1 : " + ht1);
		System.out.println("Mappings of ht2 : " + ht2);
	}
}

輸出

ht1 的映射:{3=三,2=二,1=一}
ht2 的映射:{4=4, 6=6, 5=5}

3. Hashtable(int size, float fillRatio):這個版本創(chuàng)建一個哈希表,其初始大小由size指定,填充率由fillRatio指定。填充率:基本上,它決定了哈希表在向上調(diào)整大小之前可以有多滿,其值介于 0.0 到 1.0 之間。

Hashtable<K, V> ht = new Hashtable<K, V>(int size, float fillRatio);
// Java program to demonstrate
// adding elements to Hashtable
import java.io.*;
import java.util.*;
class AddElementsToHashtable {
	public static void main(String args[])
	{
		// No need to mention the
		// Generic type twice
		Hashtable<Integer, String> ht1
			= new Hashtable<>(4, 0.75f);
		// Initialization of a Hashtable
		// using Generics
		Hashtable<Integer, String> ht2
			= new Hashtable<Integer, String>(3, 0.5f);
		// Inserting the Elements
		// using put() method
		ht1.put(1, "one");
		ht1.put(2, "two");
		ht1.put(3, "three");
		ht2.put(4, "four");
		ht2.put(5, "five");
		ht2.put(6, "six");
		// Print mappings to the console
		System.out.println("Mappings of ht1 : " + ht1);
		System.out.println("Mappings of ht2 : " + ht2);
	}
}

輸出

ht1 的映射:{3=三,2=二,1=一}
ht2 的映射:{6=6, 5=5, 4=4}

4. Hashtable(Map m):這將創(chuàng)建一個用 m 中的元素初始化的哈希表。

Hashtable<K, V> ht = new Hashtable<K, V>(Map m);
// Java program to demonstrate
// adding elements to Hashtable
import java.io.*;
import java.util.*;
class AddElementsToHashtable {
	public static void main(String args[])
	{
		// No need to mention the
		// Generic type twice
		Map<Integer, String> hm = new HashMap<>();
		// Inserting the Elements
		// using put() method
		hm.put(1, "one");
		hm.put(2, "two");
		hm.put(3, "three");
		// Initialization of a Hashtable
		// using Generics
		Hashtable<Integer, String> ht2
			= new Hashtable<Integer, String>(hm);
		// Print mappings to the console
		System.out.println("Mappings of ht2 : " + ht2);
	}
}

輸出

ht2 的映射:{3=3,2=2,1=1}

例子:

// Java program to illustrate
// Java.util.Hashtable
import java.util.*;
public class GFG {
	public static void main(String[] args)
	{
		// Create an empty Hashtable
		Hashtable<String, Integer> ht = new Hashtable<>();
		// Add elements to the hashtable
		ht.put("vishal", 10);
		ht.put("sachin", 30);
		ht.put("vaibhav", 20);
		// Print size and content
		System.out.println("Size of map is:- " + ht.size());
		System.out.println(ht);
		// Check if a key is present and if
		// present, print value
		if (ht.containsKey("vishal")) {
			Integer a = ht.get("vishal");
			System.out.println("value for key"
							+ " \"vishal\" is:- " + a);
		}
	}
}

輸出

地圖大小為:- 3
{vaibhav=20,vishal=10,sachin=30}
鍵“vishal”的值是:- 10

 

提交申請后,顧問老師會電話與您溝通安排學(xué)習(xí)

免費課程推薦 >>
技術(shù)文檔推薦 >>
主站蜘蛛池模板: 免费久草视频 | 国产精品免费大片一区二区 | 亚洲图片欧美文学小说激情 | 久久精品亚洲欧美日韩久久 | 日韩手机在线免费视频 | 国产色综合久久无码有码 | 91抖音成人| 视频精品一区二区 | 欧美超高清在线观看 | 久草新免费 | 国产日韩欧美视频 | 天天骑夜夜操 | 欧美日韩一区二区不卡 | 中文字幕亚洲欧美 | 国产欧美日韩另类va在线 | 欧美精品福利在线视频 | 成年在线观看视频免费看 | 99热网站 | 一级毛片看一个 | 夫妻一级| 五月婷婷在线观看 | 欧美亚洲欧美 | 日韩黄色在线播放 | 亚洲精品日韩专区在线观看 | 又黄又爽视频在线观看 | 九月丁香激情综合婷婷玉立 | 无遮挡无删动漫肉在线观看 | 成人在线视频网 | 色网站在线观看 | 日日夜夜人人 | 午夜免费伦费影视在线观看 | 一区二区影院 | 天天插天天插天天插 | 中日一级片| 国产一区二区三区在线视频 | 在线看黄网 | 亚洲影院一区 | 中文字幕天天躁日日躁狠狠 | 欧美一级高清免费a | 久久伊人婷婷 | 青青草狠狠操 |