In this article, we will examine the data type in Java, but we will introduce the concept of abstract data type (ADT). We will also learn some of the ADTs defined by Java Collections Framework (Java Collection Architecture).
ADT A ADT is a saved data type and actions that may be performed on this data type. Developers can only access the properties of the ADT through the ADT operation method, and they will not know how the various operations inside this data type are implemented.
In Java, we often use an interface to give an operational collection without the need to disclose the details of these operations. Remember an interface defines a method set and the Java class must implement this collection to meet its mandatory conditions or implement an instance of this interface.
Linear tables, stacks and queues often say linear tables, stacks, and queues often say when we talk about ADT. We will not discuss details of these data structures, but we will discuss why they are called ADT.
A linear table is a collection of limited elements, and its element is arranged in a linear manner and provides direct access to its elements. A stack is an ordered linear table for a backward first out (LIFO), and the element is added from the stack head and takes out from the stack head. A queue is a first-orderly order linear table, and the element is added from the queue and takes out from the queue header.
The internal structure of the linear table, the stack and queue can be implemented in many ways. For example, we can use an ordered array or a linked list to implement each structure. The key point is whether you implement its internal structure, its external interface is always constant. This allows you to modify or upgrade the underlying implementation process without changing the common interface part.
Java Collection Architecture Java 2 Software Development Cap (SDK) provides some new classes to support most commonly used ADTs. These classes are called Java set classes (similar to a collection class in MFC), which work together to form a Java collection architecture. This collection architecture provides an interface and class that represents data to a so-called collection abstract data.
The java.util.collection interface is used to represent any group of objects, which is an element. This interface provides basic such as adding, deleting, and querying this. The Collection interface also provides an Iterator method. The Iterator method returns an instance of the Java.util.Iiterator interface. The Iterator interface provides HasNext, Next, and Remove methods. With the method provided by the Iterator interface, you can cycle the instances in a Collection object and can safely delete the elements represented by the iTerator (Cursor) from the beginning to the end.
Java.util.AbstractCollection is the basis for all collection architectures. The AbstractCollection class provides an implementation of all methods other than the Iterator and Size methods in the Java.util.Collection interface. These two exceptions are achieved by all subclasses of Java.util.AbStractCollection.
A class that implements an interface must provide an implementation of all interface methods. Because some of the interface methods in the collection architecture are optional, there must be a method to notify the caller some method is not implemented. When an optional method is implemented and this method is not implemented, a unsupportedOperationException exception is thrown. The UNSUPPORTEDOPERATIONException class inherits the RuntimeException class. This allows the caller to call all the collection operations without having to put each call in a try-catch pair.
The List linear table List interface inherits the Collection interface and defines an ordered collection that allows the same elements. The LIST interface has also attached some ways to operate the elements in the line based on the location of the elements in the linear table. These operations include Add, Get, Set, and Remove.
The List interface also provides a listiterator method. This method returns an instance of the Java.util.Listiterator interface, which allows you to travel from the head or from the tail to the head. Java.util.ListIterator inherits the Java.util.Iiterator interface. Therefore, it supports the addition and modification of the elements in the Collection representative. The following example demonstrates how to traverse a list of elements that are traversed forward. To complete this work, you must position the ListITORTOR after the traversal start of the list of last elements.
Listiterator it = alist.listiterator (alist.size ()); while (it (it)) system.out.println (iter.previous (). TOSTRING ());} Collection architecture provides two to the List interface Implement: LinkedList (Link) and ArrayList (array list, a static list). Both implementations support random access to their elements. A ArrayList instance supports an array-style operation and supports changes in array size. An instance of a LinkedList provides explicit support for adding, deleting, and providing elements at the beginning and end of the list. Using these new methods, a programmer can simply use a LINEDLIST as a stack or queue, as follows:
LinkedList aQueue = new LinkedList (aCollection); aQueue.addFirst (newElement); Object anElement = aQueue.removeLast (); LinkedList aStack = new LinkedList (aCollection); aStack.addFirst (newElement); Object anElement = aStack.removeFirst (); The code snippet in Table A uses java.util.ArrayList and java.util.linkedList to demonstrate some commonly used operations for implementing instances of Java.util.List interfaces. These operations include adding elements, random access elements, and explicitly deleting elements at the tail tail.
Knowing that it is a good ADT to provide a strong tool in the public interface and its specific achievement of the operation in the public interface. This makes an implementation of an ADT to change and evolve while maintaining its common interface. The Java Collection Architecture provides a large number of interfaces and its implementation to represent the collection of basic elements and can be used to create useful ADTs.