java中treemap
Java TreeMap is one of the Map implementation and it’s part of Java Collections framework.
Java TreeMap是Map實現之一,它是Java Collections框架的一部分。
Some of the important points to remember about TreeMap in java are;
nodejs java。 關于Java中的TreeMap,需要記住的一些重點是:
NavigableMap
and indirectly implements SortedMap
interface. TreeMap also extends AbstractMap
class. 除了實現Map接口之外,Java TreeMap還實現了NavigableMap
并間接實現了SortedMap
接口 。 TreeMap還擴展了AbstractMap
類。 Collections.synchronizedSortedMap
method. TreeMap不同步,因此不是線程安全的。 對于多線程環境,可以使用Collections.synchronizedSortedMap
方法獲取包裝的同步。 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 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之間的一些區別。
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
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态