Summary of common Java Containers (collections)

Source: Internet
Author: User

When I first came into contact with the container, I always felt a lot of things difficult. Learn to summarize, commonly used is nothing less than the following, methods do not need to remember all, remember a few key, where the red method must be remembered.

Collection
├list
│├linkedlist
ArrayList
│└vector
│└stack
└set

Hashset
Map
├hashtable
HashMap 
└weakhashmap

1, Collection: (Common sub-interface set and list)

Common methods:

int size ();     boolean isEmpty ();   void Clear ();   Boolean contains (Object element);

equals Boolean Add (Object element);    Boolean remove (Object element); Boolean containsall (Collection c);

Boolean AddAll (Collection c);    Boolean RemoveAll (Collection c);   Boolean retainall (Collection c); Ask for Intersection

eg:size (): The number of elements in the container at this time; contains (object O): whether the given O value is included;

Array read fast change slow

Linked fast reading Slow

Hash search extremely fast, traverse very slow

Tree Insert/Search is fast, suitable for indexing

(1) Set: unordered and non-repeatable ; Common Implementation class: Hashset,treeset;

Unordered: The first random, and then if no other elements of the join or exit, then keep this order, different from the random;

Do not repeat: Beware of "object" problems, you must first override the Equals and hashcode () methods; Hashcode () determines whether two objects point to the same object;

Common method: The set interface itself does not provide additional methods, all methods are from collection;

Common Implementation classes: HashSet (), TreeSet ();

(2) List: order can be repeated; Common Implementation class: ArrayList; LinkedList;

Common methods: In addition to Collection inside the method, there are the following:

Object get (int index); Output subscript corresponding value by subscript

object Set (int index, object element);//subscript to replace the corresponding subscript value    

void Add (int index, Object element); Represents the insertion of the given O value from the index position add (Oject o);

Object Remove (int index); Delete the element that corresponds to the subscript

int indexOf (Object o); Gets the index of the first occurrence of the O value in a given container

int lastIndexOf (Object o); Gets the index of the last occurrence of a 0 value in a given container


Remove (int index); //indicates that the corresponding underlying value is removed by subscript,

Note: If the given value has an integer type, it is not removed with a specific value, but instead the following value is removed; Remove (Object o);

Note: ArrayList has two add (), remove ()? A: One method comes from collection, and the other is from its own method.    

(3) Map interface: It is a combination of k,v value, and K value can not be duplicated;

Common Implementation classes: HashMap: disorder;

TreeMap: According to the size of the K value to output;

Linkedhashmap: Output According to the order of input;

Common methods:

Object put (object key, object value);

Object get (object key); Returns the value that corresponds to the key value;

Object remove (object key);

Boolean ContainsKey (Object key); A method that contains key values that are commonly used to determine whether a user name exists

Boolean Containsvalue (Object value);

int size ();

Boolean isEmpty ();

void Putall (Map t);

void Clear ();

Keyset (): assigns the K value inside the map to the set interface;

VALUES (): Assigns the V value inside the map to the collection interface;

This article is from the "Not _ Heroes" blog, please be sure to keep this source http://notahero.blog.51cto.com/10306439/1673531

Summary of common Java Containers (collections)

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.