Java哈希表和有序表實(shí)例代碼講解
哈希表(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ǔ)類型的話,并不能直接排序,需要該類型實(shí)現(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哈希表和有序表實(shí)例代碼講解的文章就介紹到這了,更多相關(guān)Java哈希表和有序表內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot如何優(yōu)雅地進(jìn)行字段校驗
這篇文章主要給大家介紹了關(guān)于Springboot如何優(yōu)雅地進(jìn)行字段校驗的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01Java并發(fā)編程包中atomic的實(shí)現(xiàn)原理示例詳解
這篇文章主要給大家介紹了關(guān)于Java并發(fā)編程包中atomic的實(shí)現(xiàn)原理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09手把手教你如何利用SpringBoot實(shí)現(xiàn)審核功能
審核功能經(jīng)過幾個小時的奮戰(zhàn)終于完成了,現(xiàn)在我就與廣大網(wǎng)友分享我的成果,這篇文章主要給大家介紹了關(guān)于如何利用SpringBoot實(shí)現(xiàn)審核功能的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05Spring Boot結(jié)合IDEA自帶Maven插件如何快速切換profile
IDEA是目前 Java 開發(fā)者中使用最多的開發(fā)工具,它有著簡約的設(shè)計風(fēng)格,強(qiáng)大的集成工具,便利的快捷鍵,這篇文章主要介紹了Spring Boot結(jié)合IDEA自帶Maven插件快速切換profile,需要的朋友可以參考下2023-03-03java面試常問的Runnable和Callable的區(qū)別
大家好,本篇文章主要講的是java面試常問的Runnable和Callable的區(qū)別,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-01-01Java結(jié)合Swing實(shí)現(xiàn)龍年祝福語生成工具
Swing是一個為Java設(shè)計的GUI工具包,屬于Java基礎(chǔ)類的一部分,本文將使用Java和Swing實(shí)現(xiàn)龍年祝福語生成工具,感興趣的小伙伴可以了解下2024-01-01關(guān)于springboot響應(yīng)式編程整合webFlux的問題
在springboot2.x版本中提供了webFlux依賴模塊,該模塊有兩種模型實(shí)現(xiàn):一種是基于功能性端點(diǎn)的方式,另一種是基于SpringMVC注解方式,今天通過本文給大家介紹springboot響應(yīng)式編程整合webFlux的問題,感興趣的朋友一起看看吧2022-01-01Spring Security保護(hù)用戶密碼常用方法詳解
這篇文章主要介紹了Spring Security保護(hù)用戶密碼常用方法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-09-09