首页 > 编程知识 正文

Java中HashMap和TreeMap之间的区别

时间:2023-05-06 17:52:04 阅读:284451 作者:3388

Java中的HashMap和TreeMap (HashMap and TreeMap in Java)

First, we will see how TreeMap differs from HashMap in Java?

首先,我们将看到TreeMap与Java中的HashMap有何不同?

树状图 (TreeMap)

This class is available in java.util package.

此类在java.util包中可用。

This class is an implementation class of Map interface.

此类是Map接口的实现类。

The underlying data structure of TreeMap is RED-BLACK Tree.

TreeMap的基础数据结构是RED-BLACK树。

In TreeMap "insertion order of the elements" is not preserved because elements will be inserted according to some sorting order of keys (Here sorting will be done based on keys).

在TreeMap中,“元素的插入顺序”未保留,因为将根据键的某种排序顺序插入元素(此处将根据键进行排序)。

In TreeMap object is represented as a group of elements as a key-value pair.

在TreeMap中,对象表示为一组元素,这些元素是键/值对。

In TreeMap "duplicates insertion is not possible" for keys (i.e. it is not allowed to insert duplicate elements for keys).

在TreeMap中,键“不能重复插入”(即,不允许插入键的重复元素)。

In TreeMap "duplicates insertion is possible" for values (i.e. it is allowed to insert duplicate elements for values or there are no restrictions on values).

在TreeMap中,值可以“重复插入”(即,允许为值插入重复元素,或者对值没有限制)。

In TreeMap sorting will be done of two types:

在TreeMap中,排序分为两种类型:

Default Natural Sorting (Ascending order)Customized Sorting(Either Ascending or Descending)

In Default natural sorting "TreeMap keys" should be homogenous and Comparable.

在“默认”自然排序中,“ TreeMap键”应该是同质且可比较的。

In Customized sorting "TreeMap keys" need not be homogenous and Comparable.

在自定义排序中,“ TreeMap键”不必是同质的和可比较的。

In TreeMap "null insertion is possible" for keys as the first elements or in other words if we will insert null after the first element then we will get an exception.

在TreeMap中,将键作为第一个元素“可以插入空”,换句话说,如果我们将在第一个元素后插入null,则将获得异常。

In TreeMap "null insertion is not possible" for keys for non-empty Map.

在TreeMap中,非空Map的键“无法插入空值”。

Example:

例:

// Java program to demonstrate the behavior of TreeMap import java.util.*;class TreeMapClass { public static void main(String[] args) { // Creating an instance of TreeMap TreeMap tm = new TreeMap(); // By using put() to add elements in TreeMap tm.put(1, "Java"); tm.put(3, "C"); tm.put(2, "C++"); tm.put(4, "Java"); tm.put(6, null); tm.put(7, 10); tm.put(2, "Ruby"); /* tm.put("Java" , "is a programming");             Here hetrogenous object is not allowed for keys */ /* tm.put(null , "Python");             Here null insertion for keys is not             possible for non-empty TreeMap */ /* tm.put(2 , "Ruby");             Here we will not get any exception but             only one will be considerable */ // Display Current TreeMap System.out.println("Display Current TreeMap is :" + tm); }}

Output

输出量

E:Programs>javac TreeMapClass.javaE:Programs>java TreeMapClassDisplay Current TreeMap is :{1=Java, 2=Ruby, 3=C, 4=Java, 6=null, 7=10}

Second, we will see how HashMap differs from TreeMap in Java?

其次,我们将看到HashMap与Java中的TreeMap有何不同?

哈希图 (HashMap)

This class is available in java.util package.

此类在java.util包中可用。

This class is an implementation class of Map interface.

此类是Map接口的实现类。

The underlying data structure of HashMap is Hashtable.

HashMap的基础数据结构是Hashtable。

HashMap is a parent of LinkedHashMap.

HashMap是LinkedHashMap的父级。

In HashMap "insertion order of the elements" is not preserved because elements will be inserted according to some hashCode of keys (i.e. insertion order is not needed to be same as the retrieval order).

在HashMap中,“元素的插入顺序”未保留,因为将根据某些键的hashCode插入元素(即,插入顺序不必与检索顺序相同)。

In HashMap object is represented as a group of elements as a key-value pair.

在HashMap中,对象被表示为一组作为键值对的元素。

In HashMap "duplicates insertion is not possible" for keys (i.e. it is not allowed to insert duplicate elements for keys).

在HashMap中,键“不能重复插入”(即,不允许插入键的重复元素)。

In HashMap "duplicates insertion is possible" for values (i.e. it is allowed to insert duplicate elements for values or there is no restrictions on values).

在HashMap中,值可以“重复插入”(即,允许为值插入重复元素,或者对值没有限制)。

In HashMap "null insertion is possible" for keys and values but once for keys and multiple for values.

在HashMap中,键和值“可以空插入”,但键一次,值多个。

In HashMap "Heterogenous objects" are allowed for both keys and values.

在HashMap中,键和值均允许使用“异构对象”。

Example:

例:

// Java program to demonstrate the behavior of HashMap import java.util.Collection;import java.util.HashMap;class HashMapClass { public static void main(String[] args) { // Creating an instance of HashMap HashMap hm = new HashMap(); //By using put() method to add some values in HashMap hm.put("Java", 1000); hm.put("C", 2000); hm.put("C++", 3000); hm.put("Ruby", 4000); hm.put("Python", 1000); hm.put("null", null); hm.put("Django", null); /* hm.put("null",null);             Here we will not get any error but             one null is accepted for keys*/ // Display retrieval order of HashMap System.out.println("Current HashMap list is :" + hm); // by using values() to find values of HashMap Collection values = hm.values(); // Display Values of HashMap System.out.println("Current HashMap Key values is :" + values); }}

Output

输出量

E:Programs>javac HashMapClass.javaE:Programs>java HashMapClassCurrent HashMap list is :{Ruby=4000, C=2000, Django=null, Python=1000, C++=3000, null=null, Java=1000}Current HashMap Key values is :[4000, 2000, null, 1000, 3000, null, 1000].

翻译自: https://www.includehelp.com/java/differences-between-hashmap-and-treemap-in-java.aspx

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。