Java Collection source code analysis (1), java Collection source code analysis

Source: Internet
Author: User

Java Collection source code analysis (1), java Collection source code analysis

The Java Collection toolkit is located under the Java. util package and contains many common data structures, such as arrays, linked lists, stacks, queues, sets, and hash tables. The Java Collection framework can be divided into the following five parts: List, Set, Map ing, Iterator, Enumeration, and Arrays and Collections ).

It can be seen that Collection classes are mainly divided into two categories: Collection and Map.

 

Collection is a List, Set, and other sets of highly abstract interfaces, it contains the basic operations of these sets, it is mainly divided into two parts: List and Set.

The List interface usually represents a List (array, queue, linked List, stack, etc.), where elements can be repeated, commonly used implementation classes are ArrayList and sorted List, and there is also a commonly used Vector. In addition, the Queue list still implements the Queue interface, so it can also be used as a Queue.

The Set interface usually indicates a Set, where elements cannot be repeated (guaranteed by the hashcode and equals functions). Common Implementation classes include HashSet and TreeSet, and HashSet is implemented through HashMap in Map, treeSet is implemented through TreeMap in Map. In addition, TreeSet also implements the SortedSet interface, so it is an ordered set (the elements in the set must implement the Comparable interface and override the Compartor function ).

We can see that the abstract classes AbstractCollection, AbstractList, and AbstractSet implement the Collection, List, and Set interfaces respectively. This is a lot of adapter design patterns used in the Java Collection framework. These abstract classes are used to implement interfaces, implement several or all methods in the interface in the abstract class, so that the following classes only need to inherit the abstract class directly and implement the methods they need, instead of implementing all the abstract methods in the interface.

Map is a ing interface, where each element is a key-value pair. Similarly, the abstract class AbstractMap implements most functions of the Map interface through the adapter mode, implementation classes such as TreeMap, HashMap, and WeakHashMap are implemented by inheriting AbstractMap. In addition, the commonly used HashTable directly implements the Map interface. Both the Map and Vector are the collection classes introduced by JDK1.0.

Iterator is an iterator used to traverse a set (Map cannot be traversed, but Collection can be traversed). The Collection implementation class implements the Iterator () function, which returns an Iterator object to traverse the set, listIterator is used to traverse the List. The Enumeration is introduced in JDK and serves the same purpose as Iterator, but it has fewer functions than Iterator. It can only be used in Hashtable, Vector, and Stack.

Arrays and Collections are two tool classes used to operate Arrays and Collections. For example, Arrays are called in a large number in ArrayList and Vector. copyof () method, while many static methods in Collections can return the synchronized version of each collection class, that is, the thread-safe version. Of course, if you want to use a thread-Safe Combination class, select the corresponding collection class under the Concurrent package.

-- Most of the above content is organized from the network

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.