Inheritance structure of Java container classes

Source: Internet
Author: User

Summary: Inheritance structure for Java container classes

The Java Container Class library defines two different concepts of containers, collection and map

    • Collection a sequence of independent elements that obey one or more rules. The list must save the elements in the order in which they are inserted. Set cannot have duplicate elements. The queue determines the order in which the objects are produced, according to the queuing rules.

(The JDK source code version of the article has no special instructions are jdk1.8.0_101)

Public interface Collection<e> extends iterable<e> {

As you can see, Java defines the basic operating methods of the collection interface and the internal collection, and collection by default can add elements to the end of the collection, delete the specified elements, and so on. The List, Set, and queue interfaces inherit from the collection and define their own different methods. If you want to learn Java can come to this group, the first is 220, the middle is 142, the last is 906, there is a large number of learning materials can be downloaded.

    • Map a pair of "key-value pairs" objects that allow us to use keys to look up values.

Public interface Map<k,v> {

The map internal interface entry<k,v> corresponds to the key value pairs of the map.

Specific introduction

Iterators

Let's introduce an iterator. The iterator itself is also a design pattern, the original idea is: the implementation of the container by many kinds, and we want to traverse the container, first of all should not care about the details of the container implementation, and then the traversal operation should be lightweight. Iterators unify the way the container is accessed, and the cost of creating it is minimal. It is important to note that iterator can only be moved one way.

Public interface Iterator<e> {

The container's iterator is obtained through the container's iterator () method

Next () of the iterator gets the next element

Hasnext () Determine if there are elements

Remove () deletes the specified element

Listiterator

Listiterator is an extension of iterator for various list class accesses and supports bidirectional movement.

Collection

List

The List promises to maintain elements in a particular sequence. The list interface adds a number of methods based on the collection, allowing you to insert and remove elements in the middle of a list.

Public interface List<e> extends collection<e> {

There are two types of list,arraylist and LinkedList

The
List Type Advantages Disadvantagesunderlying implementation
ArrayList Faster random access elements Slow insertion and deletion of intermediate elements Array
LinkedList Insertion and deletion of intermediate elements, optimization of sequential access Slow random access elements Doubly linked list

Set

Set does not save duplicate elements, which are often used to quickly find elements. It is worth mentioning that the set has exactly the same interface as the collection, without any additional functionality. The deposited element must define the Equals () method

The
set Type Usage Scenariosunderlying implementation
HashSet Quick Find, element must define HASHCODE () Linked list
TreeSet Keep order, element must implement comparable interface Red-black tree structure
Linkedhashset HashSet of maintenance order, elements must be defined hashcode () Linked list

Queue

In addition to concurrent applications, the only two implementations of the queue are LinkedList and Priorityqueue, where LinkedList simultaneously implements the list, deque interface. They differ in the sort behavior rather than the performance.

???????

Original link

Inheritance structure of Java container classes

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.