Summary of collection class HashSet, ArrayList, LinkedList in Java

Source: Internet
Author: User

"HashSet"

1. HashSet storage cannot store the same elements, and whether the elements are the same: overriding the Equals method of the element. The Equals method and the Hashcode method must be compatible, such as: The Equals method determines the name of the user, then the hashcode returned hashcode must be name. Hashcode ();

2. HashSet storage is unordered, and the order of the Save is inconsistent with the order in which it was added, not as a linear structure, but as a hash structure (through the hash table: the hash cell points to the linked list). Therefore, the query efficiency of hashset is comparatively high.

3. HashSet is not thread-safe, not thread-synchronized. This requires that you implement thread synchronization yourself: Collections.synchronizedcollection (), method implementation.

"ArrayList"

1. Not thread-safe, not thread-synchronized.

2.ArrayList is implemented through a variable sized array, allowing for all elements of NULL.

The order of storage in 3.ArrayList is consistent with the order of additions. and can repeat elements.

4.ArrayList is suitable for reading elements by seat.

"LinkedList"

1. Not thread-safe, not thread-synchronized.

2.LinkedList is achieved by bidirectional cyclic chain list.

3. The order of storage and the order of additions are consistent. You can add duplicate elements.

4. Suitable for the operation of the chain table and its operation and inserts the specified position element.

Data transfer between ArrayList and LinkedList can be achieved through the ToArray () method.

"HashMap"

1. Not thread-safe, not thread-synchronized.

2. The sequence of additions and the order of preservation are inconsistent.

3. The Equals method and the Hashcode method of the key must be rewritten.

The actual capacity of 4.HASHMAP = capacity * Factor, default is 16*0.75=12. So considering the efficiency of the HASHMAP, the default capacity is designed according to the actual situation.

The action method is based on the API to find.

5. Added values are allowed to exist with null values.

"Hashtable"

1. Thread-safe, thread-synchronized, in the implementation of thread synchronization does not require manual to achieve thread synchronization. Therefore, the relative efficiency is low.

2. The order of additions and the order of preservation are inconsistent.

3. The added value does not allow null values to exist.

"TreeMap"

1. Not thread-safe, not thread-synchronized. Thread synchronization must be implemented manually, if necessary.

2. Efficiency is relatively hashmap in element additions, deletions, and positioning mappings. But in the sort aspect treemap is more practical.

3. Null values are allowed in the added value, but null pointer exceptions are reported at the time of output.

In order to achieve add, delete efficiency to achieve the function of sorting at the same time, you can also practical hashmap and TreeMap. Now add and remove the HashMap object Hmap, and then implement the sort in TreeMap object Map.putall (Hmap). Some of the code is as follows:

Map<number, person> map = new Hashmap<number, person> ();
...//Due to space limitations, the code to add a mapping relationship to the collection is omitted here
SYSTEM.OUT.PRINTLN ("Map set by HashMap class, unordered:");
for (iterator<number> it = Map.keyset (). iterator (); It.hasnext ();) {//Traversal collection
Person person = Map.get (It.next ());
System.out.println (Person.getid_card () + "" + person.getname ());
}
SYSTEM.OUT.PRINTLN ("Map set implemented by TreeMap class, Key object ascending:");
Treemap<number, person> TreeMap = new Treemap<number, person> ();
Treemap.putall (map);
...//Due to space limitations, the code to traverse the collection is omitted here
SYSTEM.OUT.PRINTLN ("Map collection implemented by TreeMap class, Key object descending:");
Treemap<number, person> treeMap2 = new Treemap<number, person> (
Collections.reverseorder ());//initialized to reverse sort
Treemap2.putall (map);
...//Due to space limitations, the code to traverse the collection is omitted here
}

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.