个性化阅读
专注于IT技术分析

Java中的TreeMap、HashMap和LinkedHashMap之间的区别

本文概述

先决条件:Java中的HashMap和TreeMap

TreeMap, HashMap和LinkedHashMap:有何相似之处?

  • 它们都提供了一个键->值映射和一种遍历键的方法。这些类之间最重要的区别是时间保证和键的顺序。
  • 所有三个类HashMap, TreeMap和LinkedHashMap实现java.util.Map接口, 表示从唯一键到值的映射。

关键点

HashMap:HashMap提供0(1)查找和插入。但是,如果对键进行迭代,键的顺序基本上是任意的。它是由一个链表数组实现的。

语法如下:

public class HashMap extends AbstractMap 
implements Map, Cloneable, Serializable
  • HashMap包含基于键的值。
  • 它仅包含唯一元素。
  • 它可能具有一个null键和多个null值。
  • 它保持没有命令.

LinkedHashMap:LinkedHashMap提供0(1)查找和插入。键是按照它们的插入顺序排列的。它是由双链接桶实现的。

语法如下:

public class LinkedHashMap extends HashMap 
0implements Map
  • LinkedHashMap包含基于键的值。
  • 它仅包含唯一元素。
  • 它可能具有一个null键和多个null值。
  • 与HashMap相同维持广告订单.

TreeMap:TreeMap提供O(log N)查找和插入。键是有序的,所以如果你需要按排序顺序遍历键,你可以这样做。这意味着key必须实现Comparable接口。TreeMap是由一个红黑树实现的。

语法如下:

public class TreeMap extends AbstractMap implements
NavigableMap, Cloneable, Serializable
  • TreeMap包含基于键的值。它实现了NavigableMap接口并扩展了AbstractMap类。
  • 它仅包含唯一元素。
  • 它不能有空键, 但可以有多个空值。
  • 与HashMap相同保持升序(按其键的自然顺序排序)。

Hashtable:

“Hashtable”是基于散列的映射的通用名称。

语法如下:

public class Hashtable extends Dictionary implements
Map, Cloneable, Serializable
  • 哈希表是列表的数组。每个列表称为存储桶。桶的位置通过调用hashcode()方法来标识。哈希表包含基于键的值。
  • 它仅包含唯一元素。
  • 它可能没有任何空键或值。
  • 已同步。
  • 这是一个遗留类。

HashMap

//Java program to print ordering 
//of all elements using HashMap
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
     //This function prints ordering of all elements
     static void insertAndPrint(AbstractMap<Integer, String> map)
     { 
         int [] array= { 1 , - 1 , 0 , 2 , - 2 };
         for ( int x: array) 
         { 
             map.put(x, Integer.toString(x)); 
         } 
         for ( int k: map.keySet())
         {
             System.out.print(k + ", " ); 
         }
     } 
  
     //Driver method to test above method
     public static void main (String[] args)
     {
         HashMap<Integer, String> map = new HashMap<Integer, String>();
         insertAndPrint(map);
     }
}

LinkedHashMap

//Java program to print ordering 
//of all elements using LinkedHashMap
import java.util.*;
import java.lang.*;
import java.io.*;
  
class Main
{
     //This function prints ordering of all elements
     static void insertAndPrint(AbstractMap<Integer, String> map)
     { 
         int [] array= { 1 , - 1 , 0 , 2 , - 2 };
         for ( int x: array) 
         { 
             map.put(x, Integer.toString(x)); 
         } 
         for ( int k: map.keySet())
         {
             System.out.print(k + ", " ); 
         }
     } 
      
     //Driver method to test above method
     public static void main (String[] args)
     {
         LinkedHashMap<Integer, String> map = new LinkedHashMap<Integer, String>();
         insertAndPrint(map);
     }
}

TreeMap

//Java program to print ordering of
//all elements using TreeMap
  
import java.util.*;
import java.lang.*;
import java.io.*;
  
class Main
{
     //This function prints ordering of all elements
     static void insertAndPrint(AbstractMap<Integer, String> map)
     { 
         int [] array= { 1 , - 1 , 0 , 2 , - 2 };
         for ( int x: array) 
         { 
             map.put(x, Integer.toString(x)); 
         } 
         for ( int k: map.keySet())
         {
             System.out.print(k + ", " ); 
         }
     } 
  
     //Driver method to test above method
     public static void main (String[] args)
     {
         TreeMap<Integer, String> map = new TreeMap<Integer, String>();
         insertAndPrint(map);
     }
}

HashMap的输出:

-1, 0, 1, -2, 2, //ordering of the keys is essentially arbitrary (any ordering)

LinkedHashMap的输出:

1, -1, 0, 2, -2, //Keys are ordered by their insertion order

TreeMap的输出:

-2, -1, 0, 1, 2, //Keys are in sorted order

比较表

Java中的TreeMap,HashMap和LinkedHashMap之间的区别1

现实生活中的应用

  1. 假设你正在创建名称到Person对象的映射。你可能需要按名称的字母顺序定期输出人员。 TreeMap使你可以执行此操作。
  2. TreeMap还提供了一种命名后输出接下来的10个人的方法。对于许多应用程序中的”更多”功能而言, 这可能很有用。
  3. 每当你需要键的顺序以匹配插入的顺序时, LinkedHashMap就会很有用。当你要删除最早的项目时, 在缓存情况下这可能很有用。
  4. 通常, 除非有其他原因, 否则将使用HashMap。也就是说, 如果你需要按插入顺序找回密钥, 请使用LinkedHashMap。如果你需要以真实/自然的顺序找回密钥, 请使用TreeMap。否则, HashMap可能是最好的。它通常更快, 所需的开销也更少。

如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请写评论。

赞(0)
未经允许不得转载:srcmini » Java中的TreeMap、HashMap和LinkedHashMap之间的区别

评论 抢沙发

评论前必须登录!