Comparison between Java container class list and set

Source: Internet
Author: User
The container class can greatly improve the programming efficiency and programming capability. In Java2, all containers are re-designed by Sun's Joshua Bloch, enriching the functions of the container class library.

The Java 2 Container class library is used to save objects. It is divided into two types:

Collection-a group of independent elements that generally follow certain rules. The list must maintain the specific sequence of elements, and the set cannot have repeated elements.

 
Map ---- A key-Value Pair object composed of a pair, that is, its elements are paired objects. The most typical application is data dictionary, and there are other extensive applications. In addition, map can return all its key groups.
A collection composed of a set and all its values, or a set composed of its key-value pairs, and can expand multi-dimensional map like an array, if each "value" of the key-value pair in the map is set to one
Map.

1. iterator

An iterator is a design pattern. It is an object that can traverse and select objects in a sequence. Developers do not need to understand the underlying structure of the sequence. An iterator is usually called a "lightweight" object because it is easy to create.

The iterator function in Java is relatively simple and can only be moved one way:

(1) The method iterator () requires the container to return an iterator. When the next () method of iterator is called for the first time, it returns the first element of the sequence.

(2) Use next () to obtain the next element in the sequence.

(3) Use hasnext () to check whether there are any elements in the sequence.

(4) use remove () to delete the elements returned by the iterator.

Iterator is the simplest implementation of the Java iterator. The listiterator designed for list has more functions. It can traverse the list in two directions, or insert and delete elements from the list.

2. Functional methods of list

 
List (Interface ):
Order is the most important feature of list; it ensures that the specific sequence of elements is maintained. List adds many methods to the collection so that you can insert and remove elements to the list (only recommended ).
Used by the consumer list ). A list can generate listiterator, which can be used to traverse the list in two directions, or to insert and delete elements from the list.

Arraylist:
List implemented by arrays. It allows fast and Random Access to elements, but it is slow to insert and remove elements into the list. Listiterator should only be used for backward traversing
Arraylist is not used to insert or delete elements, because it is much more overhead than the aggregate list.

Shortlist:
The sequential access is optimized, with little overhead for inserting and deleting data into the list. Random Access is relatively slow (can be replaced by arraylist ). It has methods addfirst (),
Addlast (), getfirst (), getlast (), removefirst (), removelast (), these methods (not in any interface or base
Class) so that the consumer list can be used as a stack, queue, and bidirectional queue.

3. Functions and methods of Set

Set (Interface): Each element stored in the set must be unique because the set does not store duplicate elements. The equals () method must be defined for the object added to the set to ensure the uniqueness of the object. Set has the same interface as collection. The Set interface does not guarantee the order of maintenance elements.

Hashset: set designed for quick search. The object stored in the hashset must define hashcode ().

Treeset: Set that maintains the order. The bottom layer is the tree structure. It can be used to extract ordered sequences from the set.

Linkedhashset: query speed with a hashset, and internal use of the linked list to maintain the order of elements (insert order ). When you use the iterator to traverse the set, the result is displayed in the order of element insertion.

 
Hashset uses the hash function to sort elements, which is specially designed for fast query. treeset uses the data structure of the red/black tree to sort elements.
Linkedhashset uses hashes internally to speed up the query, and uses the linked list to maintain the order of elements so that it looks like elements are saved in the order of insertion. Note that you can generate your own
Set needs to maintain the storage sequence of elements. Therefore, you must implement the comparable interface and define the compareto () method.

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.