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

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

问题描述

我只是理解LinkedHashSet不允许重复的元素,当它插入。但是,我不明白Hashset如何在java中工作?我知道有一点Hashtable在Hashset中使用,因此hashtable用于存储元素,在这里也不允许重复的元素。然后,Treeset也类似于Hashset它也不允许重复的条目,因此将会看到唯一的元素,它按照升序。



我还有一个疑问HashMap - Hashmap不维护顺序。它可以有一个空键和多个空值。我只是不明白这个,它是什么意思呢?这个的任何实际示例?



我知道一点,Hashmap用于工作基于此 - 键和值用于放入桶也桶具有唯一的编号。这样,可以识别和获取桶中的键和值。当我把密钥/值对放在桶中,其标识符是密钥的哈希码。对于eg:密钥的哈希码是101,所以它被存储在桶101中。一个桶可以存储多于键和值对。假设有一个例子,因为Object1是A,object2是A,object3是B,那么它具有相同的哈希码。因此,它通过在相同的桶中共享相同的Hashcode来存储不同的对象。我的疑问是,具有相同的Hashcode的对象应该是相等的,不同的对象应该有不同的Hashcode?



我是初学者,请澄清一下我的疑问,并对这种愚蠢的疑问道歉。



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(检查大小是:+ hh.size()+);
System.out.println(hh);

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

输出为

 检查大小:5 
[D,A,B,a,C]
D
A
B
a
C

a正在插入B和C之间。



现在,我使用LinkedHashSet,

  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(检查大小是:+ hh.size()+);
System.out.println(hh);

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

遵循插入顺序,并避免重复元素。
所以输出是,

 检查大小是:5 
[D,A,B, C,a]
D
A
B
C
a

现在,使用Treeset: -

  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(检查大小是:+ hh.size()+);
System.out.println(hh);

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

- Treeset按照升序排序。

 输出结果是
检查大小是否为5
[1 ,2,3,5,7]
1
2
3
5
7

然后我的疑问是,Hashset如何在Java中工作?我知道,LinkedHashset遵循双重链表。如果它使用双向链表,那么它如何存储元素?什么是双链表的意思,它是如何工作的?那么所有这三个Hashset,Treeset,Linkedhashset将在Java中使用,哪一个在Java中有更好的性能?

解决方案


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


TreeSet命令条目。



一个LinkedHashSet保留插入顺序。



HashSet不保留顺序插入,并且不排序/排序条目。这意味着,当你遍历集合,条目返回的顺序是难以理解...和没有实际意义。没有特定的原因,在那时插入a。这是只是它怎么会证明...给定的输入键的设置和它们被插入的顺序。


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


它是一个哈希表。请阅读哈希表上的维基百科页面,了解一般概述,并查看 java.util.HashMap java.util.HashSet



简单的回答是 HashSet HashMap 都是一个哈希表实现为一个散列链的数组。 / p>


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


LinkedHashSet 本质上是具有记录插入顺序的附加链接列表的哈希表。元素存储在主哈希表...,这是什么提供快速查找。有关详细信息,请参阅源代码。


doublely linkedlist是什么意思,它是如何工作的?


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







然后,所有这三个Hashset,Treeset,Linkedhashset在Java中使用,哪一个在Java中有更好的性能?


类(和其他):




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


  • 需要并发属性?例如,它们是线程安全的吗?他们是否处理争用?


  • 他们需要多少空间?

  • 是性能(时间)特性。




在最后两点?




  • A TreeSet 使用最小空间, $ c> LinkedHashSet 使用最多。


  • A 对于较大集合的查找,插入和删除最快,并且 TreeSet 往往最慢。



I just understand that LinkedHashSet does not allows duplicate elements when it is inserting. But, I dont understand how does Hashset works in java? 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.

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?

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. 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??

I am beginner please clarify my doubts and apologizing for this kinda silly doubts!

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());
    }      
    }
    }

Output is,

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

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

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

Now, Using Treeset :-

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

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?

解决方案

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

A TreeSet orders the entries.

A LinkedHashSet preserves the insertion order.

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.

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

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.

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

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

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?

Read the article in Wikipedia on doubly linked lists.


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 provide the required functionality. For example, we've already seen that they have different behaviour with respect to the order of iteration.

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

  • How much space do they require?

  • What are the performance (time) characteristics.

On the last two points?

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

  • 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天全站免登陆