nodejs java,java中treemap_Java TreeMap – Java中的TreeMap

 2023-11-19 阅读 26 评论 0

摘要:java中treemapJava TreeMap is one of the Map implementation and it’s part of Java Collections framework. Java TreeMap是Map實現之一,它是Java Collections框架的一部分。 Java TreeMap (Java TreeMap) Some of the important points to remember about TreeMap

java中treemap

Java TreeMap is one of the Map implementation and it’s part of Java Collections framework.

Java TreeMap是Map實現之一,它是Java Collections框架的一部分。

Java TreeMap (Java TreeMap)

Some of the important points to remember about TreeMap in java are;

nodejs java。 關于Java中的TreeMap,需要記住的一些重點是:

  1. Apart from implementing Map interface, Java TreeMap also implements NavigableMap and indirectly implements SortedMap interface. TreeMap also extends AbstractMap class.

    除了實現Map接口之外,Java TreeMap還實現了NavigableMap并間接實現了SortedMap 接口 。 TreeMap還擴展了AbstractMap類。
  2. TreeMap entries are sorted in the natural ordering of its keys. It also provides a constructor to provide Comparator to be used for ordering. So if you are using any class as key, make sure it’s implementing Comparable interface for natural ordering. Check out java collections interview questions to understand the importance of these methods.

    TreeMap條目按其鍵的自然順序排序。 它還提供了一個構造函數,以提供用于排序的Comparator 。 因此,如果您使用任何類作為鍵,請確保其實現了Comparable接口以實現自然排序。 查看Java集合面試問題,以了解這些方法的重要性。
  3. Java TreeMap implementation provides guaranteed log(n) time cost for the containsKey, get, put and remove operations.

    Java TreeMap實現為containsKey,get,put和remove操作提供了保證的log(n)時間成本。
  4. TreeMap is not synchronized and hence not thread-safe. For multithreaded environments, you can get a wrapped synchronized using Collections.synchronizedSortedMap method.

    TreeMap不同步,因此不是線程安全的。 對于多線程環境,可以使用Collections.synchronizedSortedMap方法獲取包裝的同步。
  5. TreeMap methods to get keyset and values return Iterator that are fail-fast in nature, so any concurrent modification will throw ConcurrentModificationException.

    用于獲取鍵集和值的TreeMap方法返回本質上是快速失敗的Iterator ,因此任何并發修改都會拋出ConcurrentModificationException 。
  6. TreeMap in java doesn’t allow null keys, however you can have multiple null values associated with different keys.

    Java中的TreeMap不允許使用空鍵,但是您可以將多個空值與不同的鍵關聯。

Java TreeMap示例 (Java TreeMap Example)

Let’s look at java TreeMap example program to see it’s natural sorting in action.

讓我們看一下Java TreeMap示例程序,看看它是自然排序的。

package com.journaldev.java;import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;public class JavaTreeMapExample {public static void main(String[] args) {Map<Integer,String> map = new TreeMap<>();map.put(10, "10");map.put(1, "1");map.put(5, "5");System.out.println(map);map = new TreeMap<>(new Comparator<Integer>() {@Overridepublic int compare(Integer x, Integer y) {return (x > y) ? -1 : ((x == y) ? 0 : 1);}});map.put(10, "10");map.put(1, "1");map.put(5, "5");System.out.println(map);}}

It will produce below output.

它將產生以下輸出。

{1=1, 5=5, 10=10}
{10=10, 5=5, 1=1}

java transient、Notice that when we are not providing Comparator while creating TreeMap, it’s using Integer compareTo method for ordering of keys. That’s why keys are in increasing order even though we insert it in any order.

請注意,當我們在創建TreeMap時未提供Comparator時,它使用Integer compareTo方法對鍵進行排序。 這就是即使我們按任意順序插入鍵,鍵也按遞增順序排列的原因。

Next time, we are providing Comparator implementation to reverse the order and it’s being used by TreeMap. So the keys are stored in descending order.

下次,我們將提供Comparator實現來逆轉順序,TreeMap會使用該順序。 因此,密鑰以降序存儲。

For simplicity, I am providing an anonymous class implementation of Comparator above, we can use lambda expressions to do the same in a single line.

java map.entry。 為簡單起見,我在上面提供了Comparator的匿名類實現,我們可以使用lambda表達式在同一行中執行相同的操作。

map = new TreeMap<>((x,y) -> {return (x > y) ? -1 : ((x == y) ? 0 : 1);});

TreeMap與HashMap (TreeMap vs HashMap)

TreeMap and HashMap both implements Map interface and part of collection framework. Let’s look at some of the differences between TreeMap vs HashMap.

TreeMap和HashMap都實現了Map接口和集合框架的一部分。 讓我們看一下TreeMap與HashMap之間的一些區別。

  1. TreeMap entries are sorted in natural ordering of keys whereas HashMap doesn’t store entries in any order.

    TreeMap條目以鍵的自然順序排序,而HashMap則不以任何順序存儲條目。
  2. TreeMap doesn’t allow null key whereas we can have one null key in HashMap.

    TreeMap不允許使用null鍵,而我們在HashMap中可以使用一個null鍵。
  3. Since TreeMap stores entries in sorted way, it’s a bit slower that HashMap in storing and retrieving objects.

    由于TreeMap以排序的方式存儲條目,因此HashMap在存儲和檢索對象方面要慢一些。
  4. TreeMap uses Red-Black tree based NavigableMap implementation whereas HashMap uses hashing algorithm implementation.

    TreeMap使用基于Red-Black樹的NavigableMap實現,而HashMap使用哈希算法實現。
  5. TreeMap implements NavigableMap, so you get some extra features that are not present in HashMap. For example – submap, first key, last key, head map, tail map etc.

    TreeMap實現了NavigableMap,因此您獲得了HashMap中不存在的一些額外功能。 例如–子圖,第一個鍵,最后一個鍵,頭部圖,尾部圖等。

何時在Java中使用TreeMap (When to use TreeMap in Java)

Most of the time HashMap will be enough to use as Map implementation in your program. But if you have some special requirements related to sorting, finding next lower and higher key, work on a submap then you can go for TreeMap.

大多數時候,HashMap足以在您的程序中用作Map實現。 但是,如果您有一些與排序有關的特殊要求,查找下一個較低和較高的鍵,在子圖上工作,則可以使用TreeMap。

java map.get。Let’s look at a simple TreeMap example program showing usage of NavigableMap methods.

讓我們看一個簡單的TreeMap示例程序,該程序顯示NavigableMap方法的用法。

package com.journaldev.java;import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;public class JavaTreeMapNavigationExamples {public static void main(String[] args) {//we have to define object as TreeMap to use NavigableMap functionsTreeMap<Integer,String> map = new TreeMap<>();for(int i=0;i<10;i++) {map.put(i, i+"");}System.out.println(map);//find id closest to 5, lower and higherEntry<Integer,String> entry = map.lowerEntry(5);System.out.println("Closest Lower key than 5 is "+entry);entry = map.higherEntry(5);System.out.println("Closest Higher key than 5 is "+entry);System.out.println("Closest Lower key than 4 is "+map.lowerKey(4));entry = map.floorEntry(5);System.out.println("Closest floor entry than 5 is "+entry);entry = map.ceilingEntry(4);System.out.println("Closest ceiling key than 4 is "+entry);entry = map.firstEntry();System.out.println("First Entry is "+entry);entry = map.lastEntry();System.out.println("Last Entry is "+entry);Map<Integer, String> reversedMap = map.descendingMap();System.out.println("Reversed Map: "+reversedMap);//poll and remove first, last entriesentry = map.pollFirstEntry();System.out.println("First Entry is "+entry);entry = map.pollLastEntry();System.out.println("Last Entry is "+entry);System.out.println("Updated Map: "+map);//submap exampleMap<Integer, String> subMap = map.subMap(2, true, 6, true);System.out.println("Submap: "+subMap);subMap = map.headMap(5, true);System.out.println("HeadMap: "+subMap);subMap = map.tailMap(5, true);System.out.println("TailMap: "+subMap);}}

When we execute above TreeMap example program, it produces following output.

當我們執行以上TreeMap示例程序時,它將產生以下輸出。

{0=0, 1=1, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9}
Closest Lower key than 5 is 4=4
Closest Higher key than 5 is 6=6
Closest Lower key than 4 is 3
Closest floor entry than 5 is 5=5
Closest ceiling key than 4 is 4=4
First Entry is 0=0
Last Entry is 9=9
Reversed Map: {9=9, 8=8, 7=7, 6=6, 5=5, 4=4, 3=3, 2=2, 1=1, 0=0}
First Entry is 0=0
Last Entry is 9=9
Updated Map: {1=1, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8}
Submap: {2=2, 3=3, 4=4, 5=5, 6=6}
HeadMap: {1=1, 2=2, 3=3, 4=4, 5=5}
TailMap: {5=5, 6=6, 7=7, 8=8}

All the above operations are self understood, please look into official documentation or comment here if you are confused about any of these.

java Vector、 以上所有操作都是可以理解的,如果對這些操作感到困惑,請查閱官方文檔或在此處評論。

That’s all for a quick roundup for TreeMap in java, I hope you enjoyed reading it.

這就是Java中TreeMap的快速匯總,希望您喜歡閱讀它。

References: Official API Documentation

參考: 官方API文檔

java hashset?翻譯自: https://www.journaldev.com/14512/java-treemap-treemap-in-java

java中treemap

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/183290.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息