Java Collection framework series tutorial 2: Collection Interface

Source: Internet
Author: User

From: http://docs.oracle.com/javase/tutorial/collections/interfaces/index.html

The core set interface encapsulates different types of sets, as shown in. These interfaces allow us to operate the set without having to worry about their specific implementation details. The core set interface is the basis of the Java set framework. As we can see, the core set interface forms a hierarchical structure:

SetIs a special set, andSortedsetIs a special set, and so on. Note that the hierarchy contains two independent trees-MapIs not a set in the true sense.

Note that all core set interfaces are generic (Generic). For exampleCollectionInterface declaration:

public interface Collection<E>...

<E> the syntax tells us that this interface is generic. When we declare a collection instance, we can or must specify the object type in the set. The specified type allows the compiler to verify that the element types in the set are correct during compilation, thus reducing runtime errors. For more information about generics, see Java generic turtorial.

After you learn to use these interfaces, you will understand most of the content in the Java Collection framework. This chapter discusses some suggestions on how to effectively use these interfaces, including when to use them. We will also learn some common usage of each interface.

To ensure that the number of core set interfaces is controllable, the Java platform does not provide separate interfaces for each set type variant (these variants include immutable, fixed size, and appendable only ). On the contrary, the operations on each set interface are customizable (optional), and the implementation of a set interface may not support all set operations. If you call an operation not supported by the setUnsupportedoperationexception. To implement a set interface, you must use a document to record the set operations that it supports.

The following list describes several core interfaces of the Java Collection framework:

  • Collection: The Root Interface of the hierarchy tree. A collection represents a group of element objects. The collection interface is required by all collection objects. Some collection interfaces allow repeated elements, while others do not. The Java platform does not provide any direct implementation of this interface, but provides some more dedicated sub-interfaces, suchSetInterfaces andListInterface. For more information, see the collection Interface
  • Set: A set that cannot contain repeated elements. This interface corresponds to a set in mathematics and is used to represent a set in a mathematical sense, such as a hand card, a course selected by a student, or a process running on a machine. For more information, see the set Interface
  • List: An ordered set (sometimes called a sequence ). A list can contain duplicate elements. You can use list to precisely control the position of each element in the list. You can specify an index to insert an element or access an element. If you have already usedVectorYou are familiar with the general functions of list. For more information, see the list Interface
  • Queue: Queue. In addition to basic set operations, a queue provides additional insert, delete, and review element operations. Queue generally organizes elements into FIFO (first-in-first-out) forms. One exception is the priority queue, which sorts the elements in the user-provided comparator or in the natural order. No matter how sorted, the queue header is always the first to be removed.RemoveOrPollMethod. In an FIFO queue, newly inserted elements are always placed at the end of the queue. Other types of queues may use different placement rules. The sorting attribute must be specified for all queue implementations. For more information, see the queue Interface
  • Map: Maps the key (KEYS) to the value (values) object. A map cannot contain duplicate keys. Each key can only be mapped to one value at most. If you have usedHashtableYou are familiar with the basic usage of map. For more information, see the map interface

The last two core set interfaces are only the sorting versions of set and map:

  • Sortedset: Set of elements in ascending order. Sortedset provides several additional operations to take advantage of the sorting feature. Sortedset is often used to generate natural sorting sets, such as word lists and member lists. For more information, see the sortedset Interface
  • Sortedmap: Map of keys in ascending order. Soretedmap is often used as a key/value pair for natural sorting, such as a dictionary and a phone book. For more information, see the soretedmap Interface

For more information about how to sort elements, see the object ordering

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.