Java Collection framework-similarities and differences between List \ set \ map

Source: Internet
Author: User

Java Collection framework-similarities and differences between List \ set \ map

A Java set is a capacity method for multiple objects. A set (volume method) is an object that aggregates multiple elements of the same nature into a whole.

Collections Framwork is a unified architecture used to represent and manipulate a collection.

 

Common collections include HashMap, HashSet, ArrayList ......, Because the meaning of each set is vague, you may not know the specific use environment. We can distinguish the similarities and differences through comparison:

Set)

Set is the simplest Set, mainly two implementation classes, HashSet and TreeSet. The biggest difference between the two is that TreeSet is ordered, hashSet is unordered (For details, refer to the next section)

Use HashSet as an example:

 

Import java. util. hashSet; import java. util. set; import java. util. *; public class hashset {/*** use Set sample code */public void hashSetExample () {Set vehicles = new HashSet (); // Declare some string items String item_1 = apple; String item_2 = orange; String item_3 = banana; boolean result; // Add result = vehicles to each object using the set method. add (item_1); System. out. println (item_1 +: + result); result = vehicles. add (item_2); System. out. println (item_2 +: + result); result = vehicles. add (item_3); System. out. println (item_3 +: + result); // try to use item_1 result = vehicles again. add (item_1); System. out. println (item_1 +: + result) ;}@ SuppressWarnings (unchecked) public static void main (String [] args) {new hashset (). hashSetExample ();}}
The final output result is: apple: true; orange: true; banana: true; apple: false. The reason is that duplicate elements are not allowed in Set.

 

List)

List is common in comparison. The List feature is that its elements are stored in a linear manner, and repeated objects can be stored in the collection. When "Stacks" and "queues" are involved ", list is used for "linked List" and so on, which is actually the knowledge used by our database structure.

The specific usage depends on the situation, for example:

ArrayList (): applies to arrays with Variable Length and allows Random Access to elements, but inserts the elements, reducing the deletion speed;

Slow list (): it is mainly used for the data structure of the linked list. The insertion and deletion speed is very fast, but the access speed is slow.

Vector (): The underlying layer is the array data structure thread synchronization (the array length is a extension) (query or addition/deletion are slow, so it is generally not commonly used, use ArrayList instead.

Map)

Map A double-row set is a set that maps Key objects and Value objects. It has a Key Value and a Value. The Key Value represents unique, so it cannot be repeated; value is a variable that can be stored, so it can be repeated. Map does not inherit from the Collection interface. When retrieving elements in a Map set, the corresponding value can be returned as long as a key object is provided.

For Map, the execution efficiency is a big problem. Two common implementations are as follows:

HaspMap (): Map is implemented based on the hash list. The overhead of inserting and querying "key-value pairs" is fixed. HashMap uses the HashCode of the object for query, this method can significantly improve the performance

TreeMap (): as long as it is a Tree, it must be sorted. TreeMap is the only method with SubMap (), so it can return a subtree.

Example of Map-HashCode:

 

Import java. util. *; public class hashcode {public static void main (String [] args) {HashMap h2 = new HashMap (); for (int I = 0; I <10; I ++) h2.put (new Element (I), new result (); System. out. println (h2 :); System. out. println (Get the result for Element :); Element test = new Element (5); if (h2.containsKey (test) System. out. println (result) h2.get (test); else System. out. println (Not found);}/* The running result is null, that is, the result is Not obtained. The value * // * class Element {int number; public Element (int n) {number = n ;}} * // * is changed as follows: */class Element {int number; public Element (int n) {number = n;} public int hashCode () {return number;} public boolean equals (Object o) {return (o instanceof Element) & (number = (Element) o ). number) ;}} class result {Random r = new Random (); boolean possible = r. nextDouble ()> 0.5; public String toString () {if (possible) Return OK !; Else return Impossible !; }}
For an Element, the comparison between the two is that there is a return value, and one has no return value. The HashCode method inherits from the Object, because the Element overwrites the hashCode () and equals () methods here, in this way, the two keys are consistent, so a value is returned.

 

For HashCode, it is not necessary to generate a unique HashCode for each different object, as long as the returned value can be obtained. Furthermore, for HashCode, it is best to scatter it, the "Decentralization principle" helps improve performance.

Conclusion:

 

We still need to know a lot about Collection, which is just the tip of the iceberg. Through comparative learning, we can get the optimal solution in the Process of use, so that our system performance can be optimized and the price can be reasonable.

 

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.