Linkedhashset detailed __java

Source: Internet
Author: User
Tags addall set set concurrentmodificationexception

1. Inheritance Relationship

Java.lang.Object
  java.util.abstractcollection<e>
      java.util.abstractset<e>
          java.util.hashset<e>
              java.util.linkedhashset<e>

2, with a hash table and linked list implementation of the set set interface, iterative mode and we expected the same (because the query speed of the reason for the bottom of the use of hash, but it seems to use a linked list to maintain the insertion order of elements), this and hashset implementation is different, Because in all of its inserts the property of a doubly linked list is maintained. The list defines the sequence of iterations, which is the order in which we insert elements into set sets. Note that if an element is repeatedly inserted into the collection, it does not change the order in which the collection is inserted. (if element e is added to the set S, the S.add (e) method is invoked, but S.contains (e) Calls and returns True before the S.add (e) method).

The realization of the linked list avoids the disorder of the order of the elements inserted into the hashset, nor does it incur the cost of increasing the treeset. This can be used as a copy of a set collection that, regardless of how the original set is implemented, will be the same as the original collection, and the order of the elements will not change, for example:

void foo (set m) {
       set   s=new linkedhashset (m);
}
This technique is also useful, especially when we copy a set as input, and the order of the elements in the copy set can be determined by the copy itself. (The order of elements in a collection is usually the same as the order in which they exist.) )

3, this class provides all the operations of the set collection and allows the element to be null. As with HashSet, we assume that the hash function has already allocated the element storage location in the container, so its basic operation (Add,remove,contains) run time is constant time. Its performance is slightly lower than hashset because the cost of maintaining the list feature is added: iterations require a certain amount of time to calculate the size of the set set, regardless of its capacity size. The HashSet iteration cost is higher, and its capacity needs a certain time proportion.

4, Linkedhashset contains two parameters, both of which affect its function: initializing capacity and loading properties. In terms of hashset, it is precisely defined. However, it is worth noting that the cost of initializing a large capacity for an instance is less costly than for hashset, because the number of iterations of such a class is not affected by capacity.

Note that this implementation is not synchronized. If multiple threads enter the Linkedhashset container at the same time, and at least one thread modifies the set container, it must be synchronized from the outside. This is typically synchronized with objects that contain this set set. If no such object exists, then the set set should be Collections.synchronizedset by the method "package". This is best done at the time of creation to prevent some unexpected events from occurring:

Set s = collections.synchronizedset (new Linkedhashset (...));
5. The number of iterations returned by the iterator of the class is very effective: if the set of sets changes after the iteration is created, the iteration throws an exception concurrentmodificationexception, in addition to any other method that passes through the Remove method of the iterator itself. Therefore, when modifying elements at the same time, the iterations are quickly and completely invalidated, rather than risking some behavior that is uncertain at some point in the future.

Note that the fail-fast characteristics of the iterator are not guaranteed, just as it is not possible to make a guarantee in an asynchronous modification. The number of Fail-fast iterations throws concurrentmodificationexception exceptions on the basis of passive effort. Therefore, the idea of writing a program that relies on this exception to determine its correctness is wrong: the fail-fast in the iterator

Behavior should only be used to find bugs.

6. Summary of methods

Methods inherited from class Java.util.HashSet
Add, clear, clone, contains, IsEmpty, iterator, remove, size
Methods inherited from class Java.util.AbstractSet
Equals, Hashcode, RemoveAll
Methods inherited from class Java.util.AbstractCollection
AddAll, Containsall, Retainall, ToArray, ToArray, toString
Methods inherited from class Java.lang.Object
Finalize, GetClass, notify, Notifyall, wait.
Methods inherited from Interface Java.util.Set
Add, AddAll, clear, contains, Containsall, Equals, Hashcode, IsEmpty, iterator, remove, RemoveAll, retainall, size, Toarra Y, ToArray


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.