Java Basics: Containers

Source: Internet
Author: User
Tags comparable

Reprint Please specify the Source:Jiq ' s technical Blog



One, Collection: storage of independent elements

The interfaces in collection are optional, and their implementation classes do not necessarily implement all of their interfaces, in order to prevent "interface explosion". The most common unsupported operation come from containers backed by fixed-size data structures, such as when using arraylist.aslist to convert arrays to lists.

(1) List of Collection

The list adds a lot of extra interfaces to collection. Especially LinkedList.

Advantages

Disadvantages

Order of saving elements

Application

ArrayList

random access is fast and is implemented internally using arrays.

iterations, inserting and deleting elements are slow, especially when the list size is larger.

Insert Order

Variable-length arrays

LinkedList

Iterations (sequential access is optimized), insertions, deletions are quick

Internal use of bidirectional linked list implementation

Slow random access speed

Insert Order

Sequential access, bulk insertion of deleted elements

(2) Collection of Set

Each element in the set must be unique because the set does not hold duplicate elements, but the elements that are stored in the set must define the equals () method to ensure the uniqueness of the object. In addition, set and collection have exactly the same interface, set does not guarantee the order of the maintenance elements. Therefore, it is important to note that the set does not contain random access to the Get method, because the set is maintained in its own internal order and does not require random access.

Advantages

Order of saving elements

Requirements

HashSet

For quick Find design

Hash storage

The hashcode () method must be defined

Linkedhashset

The same query speed as hashset, but the insertion is slower than hashset because it maintains the elements by maintaining the list.

Maintaining element order using a list (insert order)

The hashcode () method must be defined

TreeSet

Save the ordered set, the bottom layer is implemented by TREEMAP.

Maintaining elements in sort order

Must implement comparable interface (contains CompareTo method)

Note that if there are no special restrictions, HashSet should be your default choice because it optimizes speed.

(3) Collection of Queue :

Characteristics

Order of saving elements

LinkedList

In addition to the General list, LinkedList adds a number of ways to implement < queue, stack, bidirectional queue > Three data structures.

Especially when simulating a queue, inserting and deleting elements at both ends is fast (optimized).

Order of Insertions

Priorityqueue

The element is removed in sort order, so the comparable interface must be implemented.

Sort order

Second, Map: Store key-value pair

To ensure uniqueness in the map, any "key" needs to have a equals () method that determines whether the current key is the same as the key in the table.

Characteristics

Order of saving elements

Requirements

HashMap

The cost of inserting and querying a "key-value pair" is fixed based on the hash store for map.

Hash storage

The key that is deposited needs to have the hashcode () method, and of course, the returned identity is not necessarily unique

Linkedhashmap

To increase the speed of hashing all the elements, the insert query is only a little slower than hashmap because it maintains the list (insert order) while maintaining the hash data structure. But iterative access is faster because the chain list maintains the order internally.

Insert Order

The key implementation of the Hashcode () method is also required

TreeMap

Map is based on the implementation of red-black trees . So the resulting results are sorted.

Red and black Trees

In order to sort, the comparable interface must be implemented.

Other maps designed to solve special problems are identityhashmap,weakhashmap,concurrenthashmap and so on.

Experiments have shown that, in addition to all maps other than Identityhashmap, as map sizes become larger, insertions become noticeably slower, but the cost of finding them is much smaller. I guess this is because each insertion is ensured by the Equals method to ensure the uniqueness of the key value, but the most common operation of the map is the query operation, so the situation is optimistic.


Summarize:

(1) To make your container type safe, you need to use generics (otherwise the compiler allows you to insert a variety of different types into the container, as long as it is an object);

(2) When creating a container, try to transform it upward into an interface, like this:

List<apple> apples = newarraylist<apple> ();

The purpose of using an interface is that when you decide to modify your implementation, you just need to modify it where you created it.

(3) Java has a large number of excellent methods for containers, which are encapsulated in the Java.util.Collections class, all of which are static methods. For example, the collections class has Unmodifiablemap/list/set () method settings collection and map are not modifiable, there are synchronizedcollection/list/set/ Map and other methods to synchronize the entire container. In addition, many methods such as sorting, padding, inverse, maximum minimum, etc. can also be found.

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

Java Basics: Containers

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.