Java Collection framework

Source: Internet
Author: User

Java provides several ways to hold objects, including the built-in array of the language and the container class provided in utilities, also known as collection classes ). The collection is very important in Java. Before discussing it, let's take a look at several typical questions during the interview.
1. The difference between collection and collections.
2 list, set, and map are inherited from the collection interface.
3. Differences between arraylist and vector.
4. Differences between hashmap and hashtable.

At the end of the article, we will start with an answer.

Collection Interface

-- Collection is an object group with independent elements and has the same rules. Set list is derived from it.

Add elements (Object OBJ) and addall (collection C) to basic operations );

Delete element remove (Object OBJ); removeall (collection C );

Returns the intersection retainall (collection C );

Delete element remove (Object OBJ); removeall (collection C );

Returns the intersection retainall (collection C );

A good way to access/traverse collection elements is to use the iterator interface (the iterator is used to replace enumeration)

Public Interface Iterator {

Public Boolean hasnext (}
;

Public object next (};

Public
Void Remove (};

}

Set 
--Set with no repeated Projects

Three specific types of sets are available

Hashset-based on the hash set, the hashcode () method must be implemented for the elements added to the hash list.

Linkedhashset-elements returned in the order of addition during iteration of the pair set

Treeset-Data Structure Based on (balance) Tree

List 

--Location set. The elements added to the List can be added to a specific position in the list or to the end.

There are two specific versions

Arraylist (array table)-similar to a vector, it is used to zoom in and maintain a set of arrays. Differences:

1. Synchronization: the vector is thread-safe, that is, synchronous, And the arraylist is a line.ProgramNot secure, not synchronous
2. Data growth: When we need to increase, the vector grows to the original training by default, while the arraylist is half of the original growth.

Linked List-a two-way linked list. Each node has two pointers pointing to the previous node and the next node.

Used in FIFO and added the removefirst () element with addlist () to delete the element.
Used in filo, addfirst ()/removelast ()

Listiterator provides two-way traversal of next () Previous () to delete, replace, and add elements.

Ing table Map

--Used for keyword/value pairs, like a dictionary

Three sets of Map Processing

Keyset ()

Value ()

Project set enryset ()

Four specific versions

Hashmap-General ing table of the hash table

Linkedhashmap-extended hashmap: maintains the insertion sequence during iteration of returned sets.

Weakhashmap- ing table based on the weak reference hash list. If the reference of the keyword outside the ing table is not maintained, the memory recycle program recycles it.

Treemap-Balance Tree-based ing table

Hashmap-General ing table of the hash table

Linkedhashmap-extended hashmap: maintains the insertion sequence during iteration of returned sets.

Weakhashmap- ing table based on the weak reference hash list. If the reference of the keyword outside the ing table is not maintained, the memory recycle program recycles it.

Treemap-Balance Tree-based ing table
Collections class, used to synchronize collections, but also to change the set read-only mode.
E.g .:

Map MP = New Hashmap ();

MP
= Collections. synchronizedmap (MP ); // Generate a thread-safe ing table

MP
= Collections. unmodifiablemap (MP ); // Generate a read-only ing table

Comparable natural order sorting class comparator tree-oriented set sorting class

Container categorization (container taxonomy)

Collection interface: collection list set; Map iterator listiterator.

Abstract class: abstractcollection into actlist into actset into actmap into actsequentialist.

Collection types in earlier versions

Vector

Vector is the vector. A heterogeneous mixture that dynamically increases the capacity. The procedure is as follows:

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

Get the length of the vector: myvec. Size ();

Value assignment: Set (INT position, object OBJ)/setelementat (Object OBJ, int position)-dynamic growth not supported

Add (Object OBJ)/addelement (Object OBJ) add objects to the end of the Vector

E.g.: myvec. Add (New a_array [0]);

Retrieve element: Get (INT position)/getelement (INT position)

Stack

Is a subclass of vector. It is the overutilized stack in the data structure (This term can be referred to as the stack, so do not confuse it with heap-heap ). The method of access.

Stack () construct an empty Stack

Empty () rebellion

Search () checks whether the stack has elements

Peek () gets the top element of the stack

Pop () Stack

Push () inbound Stack

Enumeration Interface

Dictionary class

Dictionary. Keyword/value method to access data. If the ing does not have this keyword, return null.

Hashtable class

Is the specific implementation of the dictionary structure.

Answer to interview questions

The difference between collection and collections.

Collections is a Java. util class that contains various static methods related to set operations.

Collection is an interface under java. util. It is the parent interface of various collection structures.

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

The difference between arraylist and vector.
1. Synchronization: the vector is thread-safe, that is, synchronous, while the arraylist is not secure or synchronous
2. Data growth: When we need to increase, the vector grows to the original training by default, while the arraylist is half of the original growth.

Differences between hashmap and hashtable
I. Historical Reasons: hashtable is based on the obsolete dictionary class, And hashmap is an implementation of the map interface introduced by Java 1.2.

2. Synchronization: hashtable is thread-safe, that is, synchronous, while hashmap is not secure or synchronous.

3. Value: Only hashmap allows you to use a null value as the key or value of a table entry.

 

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.