![]() If you've used Vector, you're familiar with the general flavor of List. The user of a List generally has precise control over where in the list each element is inserted and can access elements by their integer index (position). List an ordered collection (sometimes called a sequence).This interface models the mathematical set abstraction 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. Set a collection that cannot contain duplicate elements.The Java platform doesn't provide any direct implementations of this interface but provides implementations of more specific subinterfaces, such as Set and List. Some are ordered and others are unordered. Some types of collections allow duplicate elements, and others do not. The Collection interface is the least common denominator that all collections implement and is used to pass collections around and to manipulate them when maximum generality is desired. A collection represents a group of objects known as its elements. Collection the root of the collection hierarchy.The following list describes the core collection interfaces: All of the Java platform's general-purpose implementations support all of the optional operations. Implementations are responsible for documenting which of the optional operations they support. If an unsupported operation is invoked, a collection throws an (Such variants might include immutable, fixed-size, and append-only.) Instead, the modification operations in each interface are designated optional a given implementation may elect not to support all operations. To keep the number of core collection interfaces manageable, the Java platform doesn't provide separate interfaces for each variant of each collection type. You'll also learn programming idioms for each interface to help you get the most out of it. ![]() This chapter discusses general guidelines for effective use of the interfaces, including when to use which interface. When you understand how to use these interfaces, you will know most of what there is to know about the Java Collections Framework. For information on generic types, see the Specifying the type allows the compiler to verify (at compile-time) that the type of object you put into the collection is correct, thus reducing errors at runtime. ![]() ![]() When you declare a Collection instance you can and should specify the type of object contained in the collection. Only stack operations are exposed which stops someone accidentally calling add(E) when push(E) was intended.The syntax tells you that the interface is generic. a LIFO could define pop(), push() and peek(), then in the context of LIFO stack = new ArrayDeque() IMHO the JDK should have a Stack interface and a Queue interface that could still be implemented by the likes of ArrayDeque but only expose the subset of methods required for that structure, i.e. The one thing that is great about a deque is that it provides both LIFO (stack) and FIFO (queue) support it can cause confusion as to which methods are for queue operations and which are for stack operations for newcomers. ![]() Specialised forms for concurrent access also exist and are implemented by ConcurrentLinkedDeque and LinkedBlockingDeque. ConcurrentLinkedDeque & LinkedBlockingDeque While this was asked a while ago it might be wise to provide a JDK6+ answer which now provides a Deque (deck) interface which is implemented by the ArrayDeque data structure and the LinkedList was updated to implement this interface. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |