A world of similarities and differences between Java Collection Framework-list\ set \map

Source: Internet
Author: User

Java A collection is a tolerant method of multiple objects. A set (tolerant method), simple point, is actually an object that can aggregate multiple elements of the same nature into a single whole.

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

the common collections are HashMap , HashSet , ArrayList ... , because the connotation of each set is very vague, so it will lead to not know the specific use of the environment. We distinguish similarities and differences by comparison:


Set (SET)

Set can be said to be the simplest set, the main two implementation classes, Hashset,treeset, the two biggest difference is that TreeSet is ordered, and HashSet is unordered (see below for details decomposition)

Examples of HashSet are as follows:

import Java.util.hashset;import Java.util.set;import java.util.*;p Ublic class HashSet {/** * Use Set Sample code */public void Hashsetexample () {Set vehicles = N        EW HashSet ();        Declare some string items string item_1 = "Apple";        String item_2 = "Orange";        String item_3 = "banana";        Boolean result;        Use the Set method for each file to add result = Vehicles.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 again with item_1 result = Vehicles.add (item_1);    System.out.println (item_1 + ":" + result);    } @SuppressWarnings ("Unchecked") public static void main (string[] args) {new HashSet (). Hashsetexample (); }          }
The result of the final output is: Apple:true;orange:true;banana:true;apple:false, the reason for this is that we don't allow duplicate elements to appear in our set.

List (lists)

List is common in comparison, the list feature is that its elements are stored in a linear way, the collection can hold duplicate objects, when it comes to "stack", "queue", "linked list" and so on, is the use of list, in fact, the database structure used by those knowledge.

As for what to use, it also depends on the situation, such as:

ArrayList (): For arrays that can change the length, you can randomly access the elements, but insert them, and delete the elements at a slower speed;

LinkedList (): Mainly used for linked list data structure, the insertion and deletion speed is very fast, but the speed of access is slow.

Vector (): The bottom is the array data structure thread synchronization (array length is variable 100% extension) (whether the query or additions and deletions are very slow, so generally not commonly used, directly using ArrayList instead of use.)

Map (map)

Map<key,value> is a double-column collection, which is a set of key objects and value object mappings, with a key value and a value, the key value is unique, so it cannot be duplicated; The value is a variable that can be stored, so it can be duplicated. Map does not inherit from the collection interface, and when retrieving an element in the map collection, it is possible to return the corresponding value as long as the key object is given.

For map, execution efficiency is a big problem, and two common implementations are:

Haspmap (): Map based on the implementation of a hash table, the cost of inserting and querying "key-value pairs" is fixed, HashMap is to use the object's hashcode to query, this method can significantly improve performance

TreeMap (): As long as it is a tree, he must be sorted, TreeMap is the only one with the Submap () method, so it can return a subtree.

Map-hashcode Example:

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");         }//* This results in NULL, i.e. no value *//* class element{int number;         public Element (int n) {number=n;         }} *//* change the method as follows: */class element{int number;         public Element (int n) {number=n;         } public int hashcode () {return number; The 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 element, the comparison between the two is a return value, a no return value, the Hashcode method is inherited from object, because element here overrides the Hashcode () and the Equals () method, so that the front and back two keys remain consistent , so the value is eventually returned.

For Hashcode, it is not necessary to produce a unique hashcode for each different object, as long as the return value can be achieved, and, for Hashcode, it is best to be dispersed, using the "dispersion principle", which helps to improve performance.

Final Summary:

for collection need to know a lot of, this is just the tip of the iceberg, through comparative learning, can be used in the process of obtaining the best solution, so as to make our system performance optimization, reasonable price.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

A world of similarities and differences between Java Collection Framework-list\ set \map

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.