69pao国产精品视频-久久精品一区二区二三区-精品国产精品亚洲一本大道-99国产综合一区久久

Java HashMap

java hashmap

java 集合框架java 集合框架

hashmap 是一個(gè)散列表,它存儲(chǔ)的內(nèi)容是鍵值對(duì)(key-value)映射。

hashmap 實(shí)現(xiàn)了 map 接口,根據(jù)鍵的 hashcode 值存儲(chǔ)數(shù)據(jù),具有很快的訪問速度,鍵允許為 null,不支持線程同步。

hashmap 繼承于abstractmap,實(shí)現(xiàn)了 map、cloneable、java.io.serializable 接口。

hashmap 的 key 與 value 類型可以相同也可以不同,可以是字符串(string)類型的 key 和 value,也可以是整型(integer)的 key 和字符串(string)類型的 value。

hashmap 中的元素實(shí)際上是對(duì)象,一些常見的基本類型可以使用它的包裝類。

基本類型對(duì)應(yīng)的包裝類表如下:

基本類型引用類型
booleanboolean
bytebyte
shortshort
intinteger
longlong
floatfloat
doubledouble
charcharacter

hashmap 類位于 java.util 包中,使用前需要引入它,語法格式如下:

import java.util.hashmap; // 引入 hashmap 類

以下范例我們創(chuàng)建一個(gè) hashmap 對(duì)象 sites, 整型(integer)的 key 和字符串(string)類型的 value:

hashmap<integer, string> sites = new hashmap<integer, string>();

 

1. 添加元素

hashmap 類提供類很多有用的方法,添加鍵值對(duì)(key-value)可以使用 put() 方法:

// 引入 hashmap 類      
import java.util.hashmap;

public class yapftest {
    public static void main(string[] args) {
        // 創(chuàng)建 hashmap 對(duì)象 sites
        hashmap sites = new hashmap();
        // 添加鍵值對(duì)
        sites.put(1, "google");
        sites.put(2, "yapf");
        sites.put(3, "taobao");
        sites.put(4, "zhihu");
        system.out.println(sites);
    }
}

執(zhí)行以上代碼,輸出結(jié)果如下:

{1=google, 2=yapf, 3=taobao, 4=zhihu}

以下范例創(chuàng)建一個(gè)字符串(string)類型的 key 和字符串(string)類型的 value:

// 引入 hashmap 類      
import java.util.hashmap;

public class yapftest {
    public static void main(string[] args) {
        // 創(chuàng)建 hashmap 對(duì)象 sites
        hashmap sites = new hashmap();
        // 添加鍵值對(duì)
        sites.put("one", "google");
        sites.put("two", "yapf");
        sites.put("three", "taobao");
        sites.put("four", "zhihu");
        system.out.println(sites);
    }
}

執(zhí)行以上代碼,輸出結(jié)果如下:

{four=zhihu, one=google, two=yapf, three=taobao}

 

2. 訪問元素

我們可以使用 get(key) 方法來獲取 key 對(duì)應(yīng)的 value:

// 引入 hashmap 類      
import java.util.hashmap;

public class yapftest {
    public static void main(string[] args) {
        // 創(chuàng)建 hashmap 對(duì)象 sites
        hashmap sites = new hashmap();
        // 添加鍵值對(duì)
        sites.put(1, "google");
        sites.put(2, "yapf");
        sites.put(3, "taobao");
        sites.put(4, "zhihu");
        system.out.println(sites.get(3));
    }
}

執(zhí)行以上代碼,輸出結(jié)果如下:

taobao

 

3. 刪除元素

我們可以使用 remove(key) 方法來刪除 key 對(duì)應(yīng)的鍵值對(duì)(key-value):

// 引入 hashmap 類      
import java.util.hashmap;

public class yapftest {
    public static void main(string[] args) {
        // 創(chuàng)建 hashmap 對(duì)象 sites
        hashmap sites = new hashmap();
        // 添加鍵值對(duì)
        sites.put(1, "google");
        sites.put(2, "yapf");
        sites.put(3, "taobao");
        sites.put(4, "zhihu");
        sites.remove(4);
        system.out.println(sites);
    }
}

執(zhí)行以上代碼,輸出結(jié)果如下:

{1=google, 2=yapf, 3=taobao}

刪除所有鍵值對(duì)(key-value)可以使用 clear 方法:

// 引入 hashmap 類      
import java.util.hashmap;

public class yapftest {
    public static void main(string[] args) {
        // 創(chuàng)建 hashmap 對(duì)象 sites
        hashmap sites = new hashmap();
        // 添加鍵值對(duì)
        sites.put(1, "google");
        sites.put(2, "yapf");
        sites.put(3, "taobao");
        sites.put(4, "zhihu");
        sites.clear();
        system.out.println(sites);
    }
}

執(zhí)行以上代碼,輸出結(jié)果如下:

{}

 

4. 計(jì)算大小

如果要計(jì)算 hashmap 中的元素?cái)?shù)量可以使用 size() 方法:

// 引入 hashmap 類      
import java.util.hashmap;

public class yapftest {
    public static void main(string[] args) {
        // 創(chuàng)建 hashmap 對(duì)象 sites
        hashmap sites = new hashmap();
        // 添加鍵值對(duì)
        sites.put(1, "google");
        sites.put(2, "yapf");
        sites.put(3, "taobao");
        sites.put(4, "zhihu");
        system.out.println(sites.size());
    }
}

執(zhí)行以上代碼,輸出結(jié)果如下:

4

 

5. 迭代 hashmap

可以使用 for-each 來迭代 hashmap 中的元素。

如果你只想獲取 key,可以使用 keyset() 方法,然后可以通過 get(key) 獲取對(duì)應(yīng)的 value,如果你只想獲取 value,可以使用 values() 方法。

// 引入 hashmap 類      
import java.util.hashmap;

public class yapftest {
    public static void main(string[] args) {
        // 創(chuàng)建 hashmap 對(duì)象 sites
        hashmap sites = new hashmap();
        // 添加鍵值對(duì)
        sites.put(1, "google");
        sites.put(2, "yapf");
        sites.put(3, "taobao");
        sites.put(4, "zhihu");
        // 輸出 key 和 value
        for (integer i : sites.keyset()) {
            system.out.println("key: " + i + " value: " + sites.get(i));
        }
        // 返回所有 value 值
        for(string value: sites.values()) {
            // 輸出每一個(gè)value
            system.out.print(value + ", ");
        }
    }
}

執(zhí)行以上代碼,輸出結(jié)果如下:

key: 1 value: google
key: 2 value: yapf
key: 3 value: taobao
key: 4 value: zhihu
google, yapf, taobao, zhihu,

 

6. java hashmap 方法

hashmap

java hashmap 常用方法列表如下:

方法 描述
clear() 刪除 hashmap 中的所有鍵/值對(duì)
clone() 復(fù)制一份 hashmap
isempty() 判斷 hashmap 是否為空
size() 計(jì)算 hashmap 中鍵/值對(duì)的數(shù)量
put() 將鍵/值對(duì)添加到 hashmap 中
putall() 將所有鍵/值對(duì)添加到 hashmap 中
putifabsent() 如果 hashmap 中不存在指定的鍵,則將指定的鍵/值對(duì)插入到 hashmap 中。
remove() 刪除 hashmap 中指定鍵 key 的映射關(guān)系
containskey() 檢查 hashmap 中是否存在指定的 key 對(duì)應(yīng)的映射關(guān)系。
containsvalue() 檢查 hashmap 中是否存在指定的 value 對(duì)應(yīng)的映射關(guān)系。
replace() 替換 hashmap 中是指定的 key 對(duì)應(yīng)的 value。
replaceall() 將 hashmap 中的所有映射關(guān)系替換成給定的函數(shù)所執(zhí)行的結(jié)果。
get() 獲取指定 key 對(duì)應(yīng)對(duì) value
getordefault() 獲取指定 key 對(duì)應(yīng)對(duì) value,如果找不到 key ,則返回設(shè)置的默認(rèn)值
foreach() 對(duì) hashmap 中的每個(gè)映射執(zhí)行指定的操作。
entryset() 返回 hashmap 中所有映射項(xiàng)的集合集合視圖。
keyset() 返回 hashmap 中所有 key 組成的集合視圖。
values() 返回 hashmap 中存在的所有 value 值。
merge() 添加鍵值對(duì)到 hashmap 中
compute() 對(duì) hashmap 中指定 key 的值進(jìn)行重新計(jì)算
computeifabsent() 對(duì) hashmap 中指定 key 的值進(jìn)行重新計(jì)算,如果不存在這個(gè) key,則添加到 hasmap 中
computeifpresent() 對(duì) hashmap 中指定 key 的值進(jìn)行重新計(jì)算,前提是該 key 存在于 hashmap 中。

java 集合框架java 集合框架

下一節(jié):java iterator(迭代器)

java語言 教程

相關(guān)文章