Hashset,Treeset和LinkedHashset,Hashmap之间的主要区别是什么?它在Java中如何工作? [英] What is the main difference between Hashset, Treeset and LinkedHashset, Hashmap and how does it work in Java?

查看:86
本文介绍了Hashset,Treeset和LinkedHashset,Hashmap之间的主要区别是什么?它在Java中如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只知道LinkedHashSet插入时不允许重复的元素。但是,我不了解Hashset在Hava中如何工作?我知道哈希表中使用了哈希表,因此哈希表用于存储元素,这里也不允许重复的元素。然后,Treeset也类似于Hashset,它也不允许重复输入,因此将看到唯一的元素,并且遵循升序。

I just understand that LinkedHashSet does not allows duplicate elements when it is inserting. But, I dont understand how does Hashset works in Hava? I know a bit that Hashtable is used in Hashset so the hashtable used to store the elements and here also does not allow the duplicate elements. Then, Treeset is also similar to Hashset it also does not allows duplicate entries so unique elements will be seen and it follows ascending order.

我对HashMap还有一个疑问- Hashmap不会维护顺序。它可能具有一个null键和多个null值。我只是不明白这一点,这实际上意味着什么?有什么实际的例子吗?

I have one more doubt regarding HashMap - Hashmap does not maintains order. It may have one null key and multiple null values. I just dont understand this and what does it mean actually? Any practical example for this?

我知道一点,用于此操作的Hashmap用于此操作-用于放入存储桶的键和值也具有唯一的编号。这样就可以识别并从存储桶中获取键和值。当我将键/值对放入标识为键的哈希码的存储桶中时。

I know a bit, Hashmap used to work based on this - Key and values used to put in buckets also bucket has unique numbers. So that, can identify and get the key and value from the buckets. When I put the key/value pair in the bucket of which identifier is the hash code of the key.

例如:键的哈希码为101,因此存储在存储桶101中。一个存储桶可以存储多个键和值对。假设举个例子,对象1是 A,对象2是 A,对象3是 B,那么它具有相同的哈希码。因此,它通过在同一存储桶中共享相同的哈希码来存储不同的对象。我的疑问是,具有相同哈希码的对象应该相等,而不同对象应该具有不同的哈希码吗?

For an eg: Hash code of the key is 101 so it is stored in bucket 101. One bucket can store more than key and value pairs. Suppose take an example as Object1 is "A", object2 is "A"and object3 is "B" then it has a same Hash code. So, it stores the different objects by sharing the same Hashcode in same bucket. My doubt is, objects with same Hashcode should be equal and different objects should have different Hashcodes?

这是使用HashSet的程序:

This is the program using HashSet:

    import java.util.*;
    public class Simple{
    public static void main(String[] args){
    HashSet hh=new HashSet();
    hh.add("D");
    hh.add("A");
    hh.add("B");
    hh.add("C");
    hh.add("a");        
    System.out.println("Checking the size is:"+hh.size()+"");
    System.out.println(hh);

    Iterator i=hh.iterator();
    while(i.hasNext()){
    System.out.println(i.next());
    }      
    }
    }

输出为

Checking the size is:5
[D, A, B, a, C]
D
A
B
a
C

我的疑问是,为什么

现在,我正在使用LinkedHashSet,

Now, I am using LinkedHashSet so,

public class Simple{
public static void main(String[] args){
    LinkedHashSet hh=new LinkedHashSet();
            hh.add("D");
            hh.add("A");
    hh.add("B");
    hh.add("C");
            hh.add("a");  

        System.out.println("Checking the size is:"+hh.size()+"");
    System.out.println(hh);

    Iterator i=hh.iterator();
    while(i.hasNext()){
        System.out.println(i.next());
    }      
}
}

我只是明白,遵循插入顺序,避免重复元素。因此输出为

I just understand that, it follows insertion order and it avoids duplicate elements. So the output is,

Checking the size is:5
[D, A, B, C, a]
D
A
B
C
a

现在,使用树集:

import java.util.*;
public class Simple{
public static void main(String[] args){
    TreeSet hh=new TreeSet();
            hh.add("1");
            hh.add("5");
            hh.add("3");
            hh.add("5");
            hh.add("2");
            hh.add("7");  

System.out.println("Checking the size is:"+hh.size()+"");
System.out.println(hh);

    Iterator i=hh.iterator();
    while(i.hasNext()){
        System.out.println(i.next());
    }      
}
}

这里,我只知道-树集遵循升序。

Here, I just understand that - Treeset follows ascending order.

The output is,
Checking the size is:5
[1, 2, 3, 5, 7]
1
2
3
5
7

那么我的疑问是,哈希集如何在Java中工作?而且我知道LinkedHashset遵循双重链表。如果它使用双向链表,那么它将如何存储元素?双向链表是什么意思,它是如何工作的?那么在Java中将使用这三个Hashset,Treeset,LinkedHashset以及在Java中哪个具有更好的性能呢?

Then my doubt is, how does Hashset works in Java? And I know that LinkedHashset follows doubly linkedlist. If it uses doubly linked list then how does it stores the elements? What does mean by doubly linkedlist and how does it works? Then where all these three Hashset, Treeset, LinkedHashset would be used in Java and which one has better performance in Java?

推荐答案


我的疑问是,为什么在 B和 C之间插入 a。

My doubt is, why "a" is inserting in between "B" and "C".

A TreeSet对条目进行排序。

A TreeSet orders the entries.

LinkedHashSet保留插入顺序。

A LinkedHashSet preserves the insertion order.

HashSet不保留插入顺序。插入,并且不会对条目进行排序/排序。这意味着,当您遍历集合时,将按难以理解的顺序返回条目,并且没有实际意义。此时没有特殊的理由插入 a 。事实就是这样……给定一组输入键及其插入顺序。

A HashSet does not preserve the order of insertion, and is does not sort/order the entries. That means that when you iterate over the set, the entries are returned in an order that is hard to fathom ... and of no practical significance. There is no particular "reason" that "a" is inserted at that point. That's just how it turned out ... given the set of input keys and the order in which they were inserted.


我唯一的疑问是,Hashset如何在Java中工作。

My only doubt is, how does Hashset works in Java.

它实现了一个哈希表。阅读哈希表上的Wikipedia页面以获取一般概述,以及源代码 java.util.HashMap java.util.HashSet 了解详情。

It is implemented a hash table. Read the Wikipedia page on hash tables for a general overview, and the source code of java.util.HashMap and java.util.HashSet for the details.

简短的答案是 HashSet HashMap 都是作为哈希链数组实现的哈希表。 / p>

The short answer is that HashSet and HashMap are both a hash table implemented as an array of hash chains.


我知道,LinkedHashset遵循双重链表。如果它使用双向链表,那么它将如何存储元素?

And I know that, LinkedHashset follows doubly linkedlist. If it uses doubly linked list then how does it stores the elements?

LinkedHashSet 本质上是一个带有附加链表的哈希表,该链表记录了插入顺序。元素存储在主哈希表中……这就是提供快速查找的功能。再次,请参见源代码以获取详细信息。

LinkedHashSet is essentially a hash table with an additional linked list that records the insertion order. The elements are stored in the main hash table ... and that is what provides fast lookup. Again, refer to the source code for details.


双向链表的含义是什么?

What does mean by doubly linkedlist and how does it works?

双向链接上阅读维基百科中的文章列表


然后将这三个Hashset,Treeset和Linkedhashset都放在其中可以在Java中使用,哪个在Java中具有更好的性能?

Then where all these three Hashset, Treeset, Linkedhashset would be used in Java and which one has better performance in java?

在这三个之间进行选择时,有很多事情要考虑类(和其他类):

There are a number of things to think about when choosing between these three classes (and others):


  • 它们是否提供所需的功能。例如,我们已经看到它们在迭代顺序方面具有不同行为。

它们是否具有必需的并发属性?例如,它们是线程安全的吗?他们处理争执吗?他们允许并发修改吗?

Do they have the required concurrency properties? For example, are they thread-safe? do they deal with contention? do they allow concurrent modification?

它们需要多少空间?

什么? (时间)特性是什么?

What are the performance (time) characteristics.

最后两点是?


  • A TreeSet 使用的空间最少,而 LinkedHashSet 使用最多。

  • A TreeSet uses the least space, and a LinkedHashSet uses the most.

A HashSet 倾向于查找,插入和删除较大的集合最快,而 TreeSet 往往最慢。

A HashSet tends to be fastest for lookup, insertion and deletion for larger sets, and a TreeSet tends to be slowest.

这篇关于Hashset,Treeset和LinkedHashset,Hashmap之间的主要区别是什么?它在Java中如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆