List and Set analysis for two Java container classes

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 a Set composed of all its keys and all its values, or a Set composed of its key-value pairs. It can also expand multi-dimensional Map like an array, you only need to make each "value" of the key-value pair in the Map a 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 order of elements is maintained. List adds many methods to the Collection so that you can insert and remove elements to the List (only recommended for using the sort 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 to traverse the ArrayList from the back to the back, rather than to insert and delete elements, because this is much more overhead than the aggregate list.

Optimize List: the sequential access is optimized. The overhead of inserting and deleting data into the List is not large, and random access is relatively slow (can be replaced by ArrayList ). It has methods such as addFirst (), addLast (), getFirst (), getLast (), removeFirst (), removeLast (), these methods (not defined in any interface or base class) allow the consumer list to 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. HashSet uses the hash function to speed up the query, at the same time, the linked list is used to maintain the order of elements, so that it seems that the elements are saved in the order of insertion. Note that when generating your own class, Set needs to maintain the storage sequence of the 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.