Java container Summary

Source: Internet
Author: User

Java container Summary

Java containers refer to the List, Set, and Map classes. Due to translation problems, Collection refers to the number of them.

List

Fast Random Access to ArrayList

LinkedList insertion and deletion are fast

It is easy to understand that array is an array and random access is fast. Link is a linked list. Of course, it is faster to insert and delete links.

 

Set each element can only be placed once

Use Hash

TreeSet uses the red/black tree to sort elements. The interface is SortedSet. When initializing a TreeSet, you can pass in the Comparator object implementation as a sort function.

LinkedHashSet also uses hash, but uses a linked list to maintain the insertion sequence.

Set access sequence

HashSet follows the HashCode sequence.

TreeSet has two methods to sort elements: 1. Object implements Comparable. 2. The new TreeSet input parameter T extends Comparator.

Easy to make mistakes: do not reverse the two. When creating a TreeSet, you can not initialize any sorting method, and no error is reported. However, when an object is added, an error is reported, indicating that cannot be cast to java. lang. Comparable.

LinkedHashSet is displayed in the insert order.

About HashCode

HashCode is an int value. The same object must have the same HashCode in get and put. HashCode of different objects is not required to be different, as long as the equals method can be separated.

A Practical HashCode should be based on the object content and be evenly distributed.

 

The same Map key can only be placed once. This key uses the equals of the key object to determine whether it is repeated.

A HashMap is similar to a HashSet and is stored by the HashCode of the key.

LinkedHashMap can be stored in the insert sequence or at least LRU sequence recently. It is a little slower than HashMap, but faster than iterative access

TreeMap is similar to TreeSet and is sorted Based on the red/black tree.

In addition, WeakHashMap, ConcurrentHashMap, and IdentityHashMap

 

Traversal of various containers

List <Integer> list = new ArrayList <Integer> (); // use Iterator to traverse. The disadvantage is that you cannot randomly access Iterator iter = list. iterator (); while (iter. hasNext () {Integer I = (Integer) iter. next ();} // This is better. However, when calling get (I), do not exceed the List length range, especially after the delete operation! For (int I = 0; I <list. size (); I ++) {Integer n = list. get (I) ;}for (Integer I: list) {// you can directly perform I operations} // The combination of forEach and lambda expressions in java8 is the best. It is suitable for Traversing input or operations. List. forEach (s-> System. out. println (s ));

 

HashSet <Integer> iset = new HashSet <Integer> (); iset. add (3); iset. add (8); iset. add (7); iset. add (11); // The simplest must be iset. forEach (I-> System. out. println (I); // you cannot use for because there is no get function for random access! Iterator <Integer> iter = iset. iterator (); while (iter. hasNext () {iter. next ();}
Map <String, String> map = new HashMap <String, String> (); map. put ("111", "one"); map. put ("zz", "zzzzzzzz"); map. put ("a", "apple"); // For entrySet iteration Iterator iter = map. entrySet (). iterator (); while (iter. hasNext () {Entry entry = (Entry) iter. next (); System. out. println (entry. getKey () + ":" + entry. getValue ();} // set iteration Iterator iter2 = map for the key. keySet (). iterator (); while (iter2.hasNext () {String key = (String) iter2.next (); System. out. println (key + ":" + map. get (key ));}

 

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.