Beruflich Dokumente
Kultur Dokumente
Java.util package
Java.util package contains a large assortment of classes and interfaces that support a board range of functionality. For example. Java.util has classes that generate random numbers, manage date and time, observe events, manipulate sets of bits, tokenize strings and handle formatted data. The java.util package also contains one of javas most powerful subsystems: The Collections Framework. The collection framework is sophisticated hierarchy of interfaces and classes that provide state-of-the-art technology for managing groups of objects. Because java.util contains a wide array of functionality, it is quite large. There are 49 classes and 16 interfaces in java.util package.
Collection Overview
The Java Collections Framework standardizes the way in which groups of objects are handled by your programs. Collections were not part of the original Java release, but were added by J2SE 1.2. Prior to the Collections Frameworks, Java provides ad hoc classes such as Dictionary, Vector, Stack and Properties to store and manipulate groups of objects. But ad hoc approach was not designed to be easily extended or adapted. Collection are an answer to this problems
Collection
List
Queue
Set
SortedSet
Collection : Enables you to work with group of objects; it is at the top of the collection hierarchy. List : Extends Collection to handle sequences(list of objects). Queue : Extends Collection to handle special types of lists in which elements are removed only from the head. Set : Extends Collection to handle sets, which must contain unique elements. SortedSet : Extends Set to handle sorted sets.
Collection Interface
The Collection interface is the foundation upon which the Collections Framework is built because it must be implemented by any class that defines a collection. Collection is a generic interface. Collection declares the core methods that all collections will have. These methods can throw following Exception
UnsupportedOperationException ClassCastException
Several of these methods can throw an UnsupportedOperationException. This occurs if a collection cannot be modified. Collections that do not allow their contents to be changed are called unmodifiable. All the built-in collections are modifiable.
A ClassCastException is generated when object is incompatible with another, such as when an attempt is made to add an incompatible object to a collection.
List Interface
The List interface extends Collection and declares the behavior of a collectin that stores a sequence of elements. Elements can be inserted or accessed by their position in the list, using zero-based index. A List may contain dulpicate elements. In addition to the methods defined by Collection, List defines some of its own methods.These methods will throw UnsupportedOperationException, ClassCaseException and an IndexOutOfBoundsException IndexOutOfBoundsException will raise when an invalid index is used.
Public void setElementAt(Object element, int index) Public void insertElementAt(Object element, int index) Public boolean contains(object element) Public boolean removeElement(Object element) Public void removeElementAt(int index) public void removeAllElements() Public boolean contains(Object element) Public String toString() Public void trimTosize() Public boolean containsAll(Collection)
Public void setDate(int) Public void setYear(int) Public void setMonth(int) Public void setHours(int) Public void setMinutes(int) Public void setSeconds(int) Public void setTime(long) Public boolean before(Date date) Public boolen after(Date date) Public boolean equals(Date date)
StringTokenizer
The StringTokenizer class provides the first step in this parsing process, often called the lexer or scanner. StringTokenizer implements Enumeration interface To use StringTokenizer, you specify an input string and a string that contains delimiters. Delimiters are characters that separate tokens. For example, ,;: sets the delimiters to a comma, semicolon and colon. The default set of delimiters consists of the whitespace characters: space, tab, newline. The StringTokenizer constructors are shown here:
StringTokenizer(String str) StringTokenizer(String str, String delimiters)
Enumeration Interface
The Enumration interface defines the methods by which you can enumerate(obtain one at a time) the elements in a collection of objects. Enumration specifies the following two methods
public boolean hasMoreElements() public Object nextElement()
Example of Enumeration
Vector v = new Vector(); v.addElement(1); v.addElement(2); v.addElement(3); System.out.println(v); System.out.println(elements in vector :); Enumeration ev = v.elements(); While(ev.hasMoreElement()) System.out.println(ev.nextElement());
Set classes
HashSet class extends AbstractSet and implements the Set interface. The elements are not stored in sorted order, and the output may very. LinkedHashSet class extends HashSet.the output will be in the order in which the elements were inserted. TreeSet class extends AbstractSet and implements the Set and SortedSet interface.Objects are stored in sorted, ascending order. Access and retrieval times are quite fast, which makes TreeSet an excellent choice when storing large amount of sorted information that must be found quickly.