Java哈希表和有序表實例代碼講解
哈希表(HashMap)
hash查詢的時間復(fù)雜度是O(1)
按值傳遞
Character,Short,Integer,Long, Float,Double,String,Boolean,在java當(dāng)中哈希表內(nèi)部以值的形式傳遞,而不是一地址的形式傳遞。
例如:
HashMap<Integer, String> intMap = new HashMap<>();
intMap.put(1234567, "111");
Integer a = 1234567;
Integer b = 1234567;
System.out.println("a==b = " + (a == b));
System.out.println("a.equals(b) = " + a.equals(b));
System.out.println("intMap.get(a) = " + intMap.get(a));
System.out.println("intMap.get(b) = " + intMap.get(b));
// 輸出結(jié)果
// a==b = false
// a.equals(b) = true
// intMap.get(a) = 111
// intMap.get(b) = 111
由上邊的案例中 a!= b,但是intMap.get(a) == intMap.get(b).我們可以看出,在我們從hashmap里面查詢或者操作某些值的話,是以值的形式去傳遞和匹配的,而不是以內(nèi)存地址的形式去匹配。
按址傳遞
如果是非原生的類型的話,以內(nèi)存地址的形式傳遞。例如:
public static class Node {
private int value;
public Node(int value) {
this.value = value;
}
}
HashMap<Node, String> map = new HashMap<>();
Node node1 = new Node(1);
Node node2 = new Node(1);
map.put(node1, "ziop");
System.out.println("map.containsKey(node1) = " + map.containsKey(node1));
System.out.println("map.containsKey(node2) = " + map.containsKey(node2));
//輸出結(jié)果
//map.containsKey(node1) = true
//map.containsKey(node2) = false
內(nèi)存大小比較
基礎(chǔ)類型,一條記錄的內(nèi)存大小是Key的大小加上Value的大小。
非基礎(chǔ)類型, 一條記錄的內(nèi)存大小是 兩個地址的大小, 一個地址8字節(jié),key和value 共16字節(jié)
如果是 基礎(chǔ)類型和非基礎(chǔ)類型的混合類型的話,就是各自按照各自的方式計算
有序表(TreeMap)
- 有序表會根據(jù)key的大小進(jìn)行 升序排列 ,我們可以用他來做
hashmap中的所有操作,并且擴(kuò)展出了,查找第一個key或者最后一個key的操作,也擴(kuò)展出了查找小于某個區(qū)間的最大值和大于某個區(qū)間的最小值 - 所有操作時間復(fù)雜度都是
O(logn)級別。 - 但是如果key是非基礎(chǔ)類型的話,并不能直接排序,需要該類型實現(xiàn)了排序接口,有可排序功能?;蛘咴趎ew treeMap的時候傳入比較方法
存放基礎(chǔ)類型操作
TreeMap<Integer, String> treeMap = new TreeMap<>();
treeMap.put(3,"我是3 ");
treeMap.put(0,"我是3 ");
treeMap.put(7,"我是3 ");
treeMap.put(2,"我是3 ");
treeMap.put(5,"我是3 ");
treeMap.put(9,"我是3 ");
treeMap.put(1,"我是3 ");
System.out.println("treeMap.containsKey(3) = "+treeMap.containsKey(3));
System.out.println("treeMap.containsKey(6) = "+treeMap.containsKey(6));
System.out.println("treeMap.get(3) = "+treeMap.get(3));
treeMap.put(3,"他是3");
System.out.println("treeMap.get(3) = "+treeMap.get(3));
treeMap.remove(3);
System.out.println("treeMap.get(3) = "+treeMap.get(3));
treeMap.remove(3);
System.out.println("treeMap.firstKey() = "+treeMap.firstKey());
System.out.println("treeMap.lastKey() = "+treeMap.lastKey());
// 返回 小于等于五 并且最近的 key
System.out.println("treeMap.floorKey(5) = "+treeMap.floorKey(5));
System.out.println("treeMap.floorKey(6) = "+treeMap.floorKey(6));
// 返回 大于等于 4 并且最靠近的值
System.out.println("treeMap.ceilingKey(4) = "+treeMap.ceilingKey(4));
//輸出結(jié)果如下
//treeMap.containsKey(3) = true
//treeMap.containsKey(6) = false
//treeMap.get(3) = 我是3
//treeMap.get(3) = 他是3
//treeMap.get(3) = null
//treeMap.firstKey() = 0
//treeMap.lastKey() = 9
//treeMap.floorKey(5) = 5
//treeMap.floorKey(6) = 5
//treeMap.ceilingKey(4) = 5
存放非基礎(chǔ)類型進(jìn)行操作
// 存放非基礎(chǔ)類型
public static void main(String[] args) {
TreeMap<Node, Integer> treeMap1 = new TreeMap<>();
Node node3 = new Node(3);
Node node4 = new Node(4);
treeMap1.put(node3, 3);
treeMap1.put(node4, 4);
System.out.println("treeMap1.firstEntry().getValue() = " + treeMap1.firstEntry().getValue());
System.out.println("treeMap1.lastEntry().getValue() = " + treeMap1.lastEntry().getValue());
}
public static class Node implements Comparable<Node> {
private int value;
public Node(int value) {
this.value = value;
}
@Override
public int compareTo(Node node) {
return this.value - node.value;
}
}
到此這篇關(guān)于Java哈希表和有序表實例代碼講解的文章就介紹到這了,更多相關(guān)Java哈希表和有序表內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
idea運(yùn)行java項目main方法報build failure錯誤的解決方法
當(dāng)在使用 IntelliJ IDEA 運(yùn)行 Java 項目的 main 方法時遇到 "Build Failure" 錯誤,這通常意味著在項目的構(gòu)建過程中遇到了問題,以下是一些詳細(xì)的解決步驟,以及一個簡單的代碼示例,用于展示如何確保 Java 程序可以成功構(gòu)建和運(yùn)行,需要的朋友可以參考下2024-09-09
詳解Spring Cloud Zuul 服務(wù)網(wǎng)關(guān)
本篇文章主要介紹了詳解Spring Cloud Zuul 服務(wù)網(wǎng)關(guān),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12
SpringBoot 定制化返回數(shù)據(jù)的實現(xiàn)示例
這篇文章主要介紹了SpringBoot 定制化返回數(shù)據(jù)的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
springMVC如何對輸入數(shù)據(jù)校驗實現(xiàn)代碼
數(shù)據(jù)的校驗是交互式網(wǎng)站一個不可或缺的功能,數(shù)據(jù)驗證分為客戶端驗證和服務(wù)器端驗證,這篇文章主要介紹了springMVC如何對輸入數(shù)據(jù)校驗,需要的朋友可以參考下2020-10-10
springboot中關(guān)于自動建表,無法更新字段的問題
這篇文章主要介紹了springboot中關(guān)于自動建表,無法更新字段的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
java中MultipartFile和File最簡單的互相轉(zhuǎn)換示例
這篇文章主要給大家介紹了關(guān)于java中MultipartFile和File最簡單的互相轉(zhuǎn)換的相關(guān)資料,MultipartFile和File都是Java中用于處理文件上傳的類,MultipartFile用于處理上傳的文件,File用于處理本地磁盤上的文件,需要的朋友可以參考下2023-09-09
java之使用多線程代替for循環(huán)(解決主線程提前結(jié)束問題)
這篇文章主要介紹了java之使用多線程代替for循環(huán)(解決主線程提前結(jié)束問題),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03
Jmeter命令行執(zhí)行腳本如何設(shè)置動態(tài)參數(shù)
這篇文章主要介紹了Jmeter命令行執(zhí)行腳本如何設(shè)置動態(tài)參數(shù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08

