What is the main difference between Hashset, Treeset and LinkedHashset, Hashmap and how does it work in Java?

后端 未结 4 1591
余生分开走
余生分开走 2020-12-20 01:50

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 Hashta

相关标签:
4条回答
  • 2020-12-20 02:18
    • HashSet does't preserve the order of elements.So you can't identify its order.
    • LinkedHashSet preserve the order as elements are added in set. It keep the order as we insert in it.

    • TreeSet maintains the order of element. TreeSet is slowest because it arrange its elements after every element addition or deletion.

      Otherwise it all repends on your requirement , whether you need ordered list, thread-safe etc.

    0 讨论(0)
  • 2020-12-20 02:24

    I'll be succinct.

    A Set follows the math theory of sets. A Set (AbstractSet is the supertype in Java) is similar to a list except it cannot have the same element twice.

    HashSet implements it with a HashMap, TreeSet implements it with a Tree, LinkedHashset implements it with a doubly-linked list.

    0 讨论(0)
  • 2020-12-20 02:25

    First of all, you need to know that all Set implementations share the same feature: they don't allow duplicates. It's not just a feature of the LinkedHashSet.

    Second, one important difference is that, out of the 3 set types you asked about, the TreeSet is a sorted set, that is elements are ordered according to their natural ordering or according to a logic described imperatively using a Comparator or implementing the Comparable interface.

    Switching to the difference between HashSet and LinkedHashSet, please note that LinkedHashSet is a subclass of HashSet. They are not sorted sets.

    HashSet is the fastest implementation of a set and it ensures the uniqueness of elements using (first) their hash value returned by the hashCode() method and (then) their equals() method. Behind the scenes, it uses a HashMap.

    The LinkedHashSet ensures consistent ordering over the elements of the set with the help of a LinkedList, which basic HashSets do not provide.

    0 讨论(0)
  • 2020-12-20 02:30

    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.

    0 讨论(0)
提交回复
热议问题