Java Collection classes such as map list set in Java

Source: Internet
Author: User

I. Collections class andCollectionInterface

CollectionsIt is a help class for collection classes. It provides a series of static methods for searching, sorting, thread security, and other operations on various sets.

Collection is the most basic collection interface. A collection represents a group of objects, namely, elements of the collection ). Some collections allow the same elements while others do not. Some can be sorted, while others cannot. The Java SDK does not provide classes that directly inherit from collections. The classes provided by the Java SDK are the "subinterfaces" that inherit from collections, such as list and set.

2. Now let's talk about some implementation classes of Java collections.

Collection
BytesList

│BytesArreylist

│BytesVector

│BytesShortlist

│ Elastic Stack

Sorted set

│BytesHashset
│BytesLinkedhashset

│ BytesTreeset

 

List indicates an ordered and repeated set.

1. arraylist class
Arraylist implements an array of variable sizes. It allows all elements, including null. Arraylist is not synchronized.
Size, isempty, get, set method running time is constant. However, the overhead of the add method is the constant of the allocation. It takes O (n) to add n elements. The running time of other methods is linear.
Each arraylist instance has a capacity, that is, the size of the array used to store elements. This capacity can automatically increase with the addition of new elements, but increasesAlgorithmNot defined. When a large number of elements need to be inserted, you can call the ensurecapacity method before insertion to increase the arraylist capacity to improve the insertion efficiency.
Like the synchronized list, arraylist is also non-synchronous (unsynchronized ).

2. Vector class
The vector is very similar to the arraylist, but the vector is synchronized. Although the iterator created by vector is the same interface as the iterator created by arraylist, because vector is synchronous, when an iterator is created and in use, another thread changes the state of the vector (for example, adding or deleting some elements). When calling the iterator method, concurrentmodificationexception is thrown. Therefore, this exception must be caught.

 

3. Sort list class
The listlist interface allows null elements. In addition, the values list provides additional get, remove, and insert methods at the beginning or end of the values list. These operations enable the queue list to be used as a stack, queue, or two-way Queue (deque ).
Note that the synchronized list method is not available. If multiple threads access a list at the same time, they must implement access synchronization by themselves. One solution is to construct a synchronized list when creating a list:
List list = collections. synchronizedlist (new collections list (...));

 

4. Stack
Stack inherits from vector to implement a post-import, first-out stack. Stack provides five additional methods to make the Vector used as a stack. The basic push and pop methods also include the elements of the peek method to get the top of the stack. The empty method tests whether the stack is empty. The search method checks the position of an element in the stack. The stack is empty after being created.

 

Set indicates an unordered and non-repeated set.

 

Map
BytesHashmap

BytesHashtable

BytesTreemap
└ Weakhashmap

Map does not inherit the collection Interface

1. hashtable class
Hashtable inherits the map interface and implements a key-value ing hash table. Any non-null object can be used as a key or value.Hashtable is synchronous.

 

2. hashmap class
Hashmap is similar to hashtable. The difference is that hashmap is non-synchronous and allows null, that is, null value and null key ., However, when hashmap is treated as a collection (the values () method can return the collection), its iteration suboperation time overhead is proportional to the capacity of hashmap. Therefore, if the performance of iterative operations is very important, do not set the hashmap initialization capacity too high or the load factor too low.

3. weakhashmap class
Weakhashmap is an improved hashmap that implements "weak references" to keys. If a key is no longer referenced by external entities, it can be recycled by GC.

 

 

Iii. traversal of collection classes

Traverse commonCollection

Iterator it = collection. iterator (); // obtain an iterator
While (it. hasnext ()){
Object OBJ = it. Next (); // obtain the next element.
}

Related Article

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.