A brief talk on the Java Collection Framework (reprint) _ commonly used vectors and HashMap

Source: Internet
Author: User
Tags set set

The original author dripping: http://www.cnblogs.com/eflylab/archive/2007/01/20/625237.html

Java provides several ways to hold objects, including the built-in array of languages, and the container Class (container classes) provided in utilities, also known as the Cluster Class (collection classes). The collection is very important in Java, and before the discussion, let's look at some of the classic questions in the interview.
1 The difference between Collection and collections.
2 List, Set, whether the map inherits from the collection interface.
3 The difference between ArrayList and vectors.
4 the difference between HashMap and Hashtable.

There is an answer at the end of the article.

Collection Collection Interface

--collection are any group of objects that are independent of each other and usually have the same rules. The Set list is derived from it.

Basic operations add element Add (Object obj); AddAll (Collection c);

Remove the element remove (Object obj); RemoveAll (Collection c);

Finding the intersection Retainall (Collection c);

Remove the element remove (Object obj); RemoveAll (Collection c);

Finding the intersection Retainall (Collection c);

A good way to access/traverse a collection element is to use the iterator interface (iterators are used to replace enumeration)

public interface Iterator{

Public Boolean Hasnext (};

Public Object Next (};

public void Remove (};

}  

Set Set
--No collection of duplicate items

There are three specific types of sets available

Hashset-based on a hash list of the elements to be added to the hash list to implement the Hashcode () method

Linkedhashset-returns an element in ascending order when iterating over a set

treeset-data structure based on (balanced) tree

Checklist List

--a set of positions. Elements added to the list can be added to a specific position in the list or added to the end

There are two specific versions of

ArrayList (array table)-Similar to vector, is used to scale array maintenance collections. Difference:

I. Synchronization: vector is thread-safe, that is, synchronous, and ArrayList is a thread program is not secure, not synchronous
Two. Data growth: When growth is needed, vectors grow by default to the original, while ArrayList is half the original

LinkedList (linked list)-is a doubly linked list, each node has two pointers pointing to the previous node and the next node.

Use Addlist () to add Element Removefirst () to delete elements in FIFO
Used in Filo, with AddFirst ()/removelast ()

Listiterator provides bidirectional traversal of next () previous () to delete, replace, and add elements

Mapping Table Map

--for keyword/value pairs, like a dictionary

Three collections for working with maps

Keyword set keyset ()

Value set Value ()

Project Set Enryset ()

Four specific versions

Common mapping table for hashmap-hash list

linkedhashmap-extension HashMap, maintaining the insertion order when iterating back the collection

weakhashmap-A mapping table based on a weakly-referenced hash list, if a reference to a keyword outside the map table is not persisted, the memory collector reclaims it

treemap-mapping table based on the balance tree

Common mapping table for hashmap-hash list

linkedhashmap-extension HashMap, maintaining the insertion order when iterating back the collection

weakhashmap-A mapping table based on a weakly-referenced hash list, if a reference to a keyword outside the map table is not persisted, the memory collector reclaims it

treemap-mapping table based on the balance tree
Collections class, for synchronizing collections, and for changing the read-only class of a collection
e.g.:

Map mp=new HashMap ();

Mp=collections.synchronizedmap (MP); Generate a thread-safe mapping table

Mp=collections.unmodifiablemap (MP); To generate a read-only mapping table

Comparable sort class of natural order Comparator tree-oriented set sorting class

Container taxonomy (Container taxonomy)

Collection interface: Collection List set; Map Iterator Listiterator.

Abstract class: Abstractcollection abstractlist abstractset abstractmap abstractsequentialist.

Collection types in the old version

Vector class

Vector, that is, vectors. A heterogeneous hybrid that can dynamically increase capacity. The operation of it is briefly as follows

For example we have a vector:vector myvec=new Vector (a_array.length)

Length of Vector obtained: myvec.size ();

Assignment: Set (int position,object obj)/setelementat (Object obj, int position) – does not support dynamic growth

Add (Object obj)/addelement (object obj) to add objects at the end of the vector

E.g.:myvec.add (new a_array[0]);

remove element: get (int position)/getelement (int position)

Stack class

is a subclass of a vector. Is the data structure in the indiscriminate stack (the word can be referred to as a stack, do not confuse the heap-heap). Last-in, first-out access mode.

Stack () constructs an empty stack

Empty () rebel

Search () checks if the stack has elements

Peek () Gets the top element of the stack

Pop () bomb stack

Push () into the stack

Enumeration interface

Dictionary class

Dictionary. Keyword/numeric access to the data, or null if the map does not have this keyword.

Hashtable class

is the concrete implementation of the dictionary structure.

Answer to the interview question

The difference between Collection and collections.

Collections is a Java.util class that contains a variety of static methods related to collection operations.

Collection is a Java.util interface, which is the parent interface of various collection structures.

Does the List, Set, and map inherit from the collection interface? List,set is map is not

The difference between ArrayList and vectors.
I. Synchronization: vector is thread-safe, that is, synchronous, and ArrayList is a thread program is not secure, not synchronous
Two. Data growth: When growth is needed, vectors grow by default to the original, while ArrayList is half the original

The difference between HashMap and Hashtable
I. Historical reasons: Hashtable is based on the old dictionary class, HashMap is an implementation of the map interface introduced by Java 1.2

Two. Synchronization: Hashtable is thread-safe, that is, synchronous, and HashMap is a thread program is not secure, not synchronous

Three. Value: Only HashMap can let you use NULL as the key or value of an entry for a table

A brief talk on the Java Collection Framework (reprint) _ commonly used vectors and HashMap

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.