溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Java哈希表和有序表如何實(shí)現(xiàn)

發(fā)布時(shí)間:2023-04-14 09:53:34 來(lái)源:億速云 閱讀:97 作者:iii 欄目:編程語(yǔ)言

這篇文章主要介紹“Java哈希表和有序表如何實(shí)現(xiàn)”,在日常操作中,相信很多人在Java哈希表和有序表如何實(shí)現(xiàn)問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Java哈希表和有序表如何實(shí)現(xiàn)”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!

    哈希表(HashMap)

    hash查詢(xún)的時(shí)間復(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里面查詢(xún)或者操作某些值的話(huà),是以值的形式去傳遞和匹配的,而不是以?xún)?nèi)存地址的形式去匹配。

    按址傳遞

    如果是非原生的類(lèi)型的話(huà),以?xún)?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ǔ)類(lèi)型,一條記錄的內(nèi)存大小是Key的大小加上Value的大小。

    非基礎(chǔ)類(lèi)型, 一條記錄的內(nèi)存大小是 兩個(gè)地址的大小, 一個(gè)地址8字節(jié),key和value 共16字節(jié)

    如果是 基礎(chǔ)類(lèi)型和非基礎(chǔ)類(lèi)型的混合類(lèi)型的話(huà),就是各自按照各自的方式計(jì)算

    有序表(TreeMap)

    • 有序表會(huì)根據(jù)key的大小進(jìn)行 升序排列 ,我們可以用他來(lái)做hashmap中的所有操作,并且擴(kuò)展出了,查找第一個(gè)key或者最后一個(gè)key的操作,也擴(kuò)展出了查找小于某個(gè)區(qū)間的最大值和大于某個(gè)區(qū)間的最小值

    • 所有操作時(shí)間復(fù)雜度都是O(logn)級(jí)別。

    • 但是如果key是非基礎(chǔ)類(lèi)型的話(huà),并不能直接排序,需要該類(lèi)型實(shí)現(xiàn)了排序接口,有可排序功能?;蛘咴趎ew treeMap的時(shí)候傳入比較方法

    存放基礎(chǔ)類(lèi)型操作

    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ǔ)類(lèi)型進(jìn)行操作

    //        存放非基礎(chǔ)類(lèi)型
    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í)現(xiàn)”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!

    向AI問(wèn)一下細(xì)節(jié)

    免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI