Java overall collection framework

Source: Internet
Author: User

1. Two interview questions about the set

Let's take a look at several questions:

1. Create an unchangeable set:

As you can see, creating an immutable set mainly calls the unmodifiableSet () method of Collections, and the Collections class encapsulates a general set through the decoration mode.

2. Remove duplicate elements from the List set and maintain the original order.

The code above passes the original list into a LinkedHashSet to remove repeated elements. In this case, the hashset can maintain the original sequence of elements. If this sequence is not required, the HashSet above can be replaced by HashSet.

2. Comparison of set framework class charts and sets

In fact, the above two questions are not difficult, but they do not have a deeper understanding of the set. Let's take a rough look at the set. The inheritance class of the set is as follows.

Iterable <T> iterator ();}

The Iterable interface is under the java. lang Package and defines an instance method for getting Iterator.

The source code of the Iterator class is as follows:

The Iterator mode is a standard access method used to traverse collection classes. It can abstract the access logic from a collection class of the same type to avoid exposing the internal structure of the set to the client. For example, if the Iterator is not used, the index is used to traverse an array:

When accessing a linked list, you must use the while loop:

The traversal methods of the above two methods must know the internal storage structure of the set in advance. The access code and the set itself are tightly coupled and the access logic cannot be separated from the set class, each set corresponds to a Traversal method, and client code cannot be reused. In addition, if you need to replace ArrayList with an existing list, the original client code must be rewritten. To solve the above problem, the Iterator mode always uses the same logic to traverse the set:

The clever design lies in that the client itself does not maintain the "Pointer" of the traversal set, and all the internal states (such as the current element location, whether there is a next element) are maintained by Iterator, this Iterator is generated by the collection class through the factory method, so it knows how to traverse the entire set. It is worth noting that this factory method is the iterator () method we mentioned earlier. Because all classes inherit the Iterable interface, its implementation class must implement iterator () method.

If you want to access a collection later, you can control the Iterator and send the "forward", "backward", "take the current element" command to indirectly traverse the entire set.

Three methods are used to traverse the set. The output results are consistent, as shown below:

Abc
Def
Abc
Def
Abc
Def

The Iterator types returned by each collection class may be different. Array may return ArrayIterator, Set may return SetIterator, and Tree may return TreeIterator, but they all implement the Iterator interface. Therefore, the client does not care about which Iterator it is. It only needs to obtain this Iterator interface, which is the benefit of object-oriented.

Continue to check the Collection interface. The source code is as follows:

The Collection interface defines some basic operations for the Collection, so any specific Collection implementation class contains these methods. However, because of the high abstraction level, a specific set implementation class, such as ArrayList and HashMap, does not directly inherit this interface, but inherits some subclasses of this interface for implementation. Therefore, the specific implementation classes of each set directly or indirectly inherit this interface.

An important method also needs to be described as follows:

Take the add (E) method as an example. This method adds a new element to the set. Method returns a boolean value, but the returned value does not indicate that the value is successfully added. Read the doc carefully and you can see that Collection rules: If a Collection refuses to add this element, an exception must be thrown for any reason. The returned value indicates whether the content of the set has changed after the add () method is executed (that is, whether the number and position of the element have changed), which is implemented by a specific class. That is, if a method error occurs, an exception is always thrown. The returned value only indicates whether the content of the Collection changes after the method is executed.

We can see that many of the input parameters are Collection. <?> Type, which facilitates the implementation of different classes in each set. For example:

Add the elements in the collections list set to the HashSet. You only need to call the addAll () method. The output result is as follows:
Mn
Abc
Def
Xy

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.