Lesson: Interfaces

Source: Internet
Author: User

Lesson: Interfaces

TheCore collection interfacesEncapsulate different types of collections, which are shown in the figure below. these interfaces allow collections to be manipulated independently of the details of their representation. core collection interfaces are the foundation of the Java Collections Framework. as you can see in the following figure, the core collection interfaces form a hierarchy.

The core collection interfaces.

ASetIs a special kindCollection,SortedSetIs a special kindSet, And so forth. Note also that the hierarchy consists of two distinct trees-MapIs not a trueCollection.

Note that all the core collection interfaces are generic. For example, this is the declaration ofCollectionInterface.

public interface Collection
 
  ...
 

The Syntax tells you that the interface is generic. When you declareCollectionInstance you canAnd shocouldSpecify the type of object contained in the collection. specifying the type allows the compiler to verify (at compile-time) that the type of object you put into the collection is correct, thus grouping errors at runtime. for information on generic types, see the Generics (Updated) lesson.

When you understand how to use these interfaces, you will know most of what there is to know about the Java Collections Framework. this chapter discusses general guidelines for valid tive use of the interfaces, including when to use which interface. you'll also learn programming idioms for each interface to help you get the most out of it.

To keep the number of core collection interfaces manageable, the Java platform doesn' t provide separate interfaces for each variant of each collection type. (Such variants might include immutable, fixed-size, and append-only .) instead, the modification operations in each interface are designatedOptional-A given implementation may elect not to support all operations. If an unsupported operation is invoked, a collection throwsUnsupportedOperationException. Implementations are responsible for documenting which of the optional operations they support. All of the Java platform's general-purpose implementations support all of the optional operations.

The following list describes the core collection interfaces:

Collection-The root of the collection hierarchy. A collection represents a group of objects known as its Elements. CollectionInterface is the least common denominator that all collections implement and is used to pass colleum und and to manipulate them when maximum generality is desired. some types of collections allow duplicate elements, and others do not. some are ordered and others are unordered. the Java platform doesn' t provide any direct implementations of this interface but provides implementations of more specific subinterfaces, such SetAnd List. Also see The Collection Interface section. Set-A collection that cannot contain duplicate elements. this interface models the mathematical set between action and is used to represent sets, such as the cards comprising a poker hand, the courses making up a student's schedule, or the processes running on a machine. see also The Set Interface section. List-An ordered collection (sometimes called Sequence). ListS can contain duplicate elements. The user of ListGenerally has precise control over where in the list each element is inserted and can access elements by their integer index (position). If you 've used Vector, You're familiar with the general flavor List. Also seeThe List Interface section. Queue-A collection used to hold multiple elements prior to processing. Besides basic CollectionOperations, QueueProvides additional insertion, extraction, and inspection operations.

Queues typically, but do not necessarily, order elements in a FIFO (first-in, first-out) manner. among the exceptions are priority queues, which order elements according to a supplied comparator or the elements 'natural ordering. whatever the ordering used, the head of the queue is the element that wocould be removed by a callremoveOrpoll. In a FIFO queue, all new elements are inserted at the tail of the queue. Other kinds of queues may use different placement rules. EveryQueueImplementation must specify its ordering properties. Also see The Queue Interface section.

Deque-A collection used to hold multiple elements prior to processing. Besides basic CollectionOperations, DequeProvides additional insertion, extraction, and inspection operations.

Deques can be used both as FIFO (first-in, first-out) and LIFO (last-in, first-out ). in a deque all new elements can be inserted, retrieved and removed at both ends. also seeThe Deque Interface section.

Map-An object that maps keys to values. MapCannot contain duplicate keys; each key can map to at most one value. If you 've used Hashtable, You're already familiar with the basics Map. Also seeThe Map Interface section.

The last two core collection interfaces are merely sorted versionsSetAndMap:

SortedSet- SetThat maintains its elements in ascending order. several additional operations are provided to take advantage of the ordering. sorted sets are used for naturally ordered sets, such as word lists and membership rolls. also see The SortedSet Interface section. SortedMap- MapThat maintains its mappings in ascending key order. This is MapAnalog SortedSet. Sorted maps are used for naturally ordered collections of key/value pairs, such as dictionaries and telephone directories. Also seeThe SortedMap Interface section.

To understand how the sorted interfaces maintain the order of their elements, see theObject Ordering section.

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.