Friday, September 3, 2010

Use Java Collections Framework

First, an overview of the data structure has a profound impact on program design, process-oriented C language, with a struct to describe the database structure, while in object-oriented programming, data structures are described using class, and includes methods of operation of the data structure. In the Java language, Java language designers commonly used data structures and algorithms to do a number of specification (interface) and implementation (concrete realization of interface). All abstract ...

I. Introduction
Data structure of the program design has a profound impact on the process-oriented C language, database structure with the struct be described in the object-oriented programming, data structures are used to describe the class, and contains a Duigaishuoju structure operation method.


In the Java language, Java language design are commonly used data structures and algorithms to do a number of specification (interface) and implementation (concrete realization of interface). Out of all the abstract data structures and operations (algorithms) collectively referred to as Java Collections Framework (Java Collection Framework).

Java programmers in specific applications, regardless of data structure and algorithm details, simply use these classes to create out of some object, and then applied directly on it. This greatly increased the efficiency of programming.

Second, set the framework hierarchy

Collection is a collection of interfaces

|---- Set sub-interface: disorderly, not allowed to repeat.

|---- List sub-interface: order, you can have duplicate elements.

Difference: Collections are collections

Set and List Comparison:

Set: Search inefficient elements, delete, and insert a high efficiency, insert and delete elements does not cause position changes.

List: and an array of similar, List can be dynamically increased, find the elements of high efficiency, low efficiency of insertion elements removed, as it will cause other elements to change location.

Set and List the specific sub-category:

Set

|---- HashSet: hash table to store elements of the form, insert delete fast.

List

|---- ArrayList: Dynamic array

|---- LinkedList: list, queue, stack.

Array and java.util.Vector

Vector is an old, dynamic arrays, a thread synchronization, efficiency is very low, generally not in favor of use.

3, Iterator iterator (interface)

Iterator is a collection of elements in the process of obtaining, in fact, to help get the collection element.

Iterator instead of the Java Collections Framework in the Enumeration. Iterator with two different enumeration:

Iterators allow the caller using well-defined semantics of the iteration period from the set of the iterator points to remove the element.

Method name has been improved.

Only a sub-Iterator interfaces ListIterator, is a list iterator that allows programmers to traverse in either direction according to the list, modify the list during iteration, and obtain the iterator's current position in the list. ListIterator no current element; its cursor position always in the call previous () and call the elements returned by next () between the elements returned. In the list of length n, there are n +1 valid index values, from 0 to n (inclusive).

4, set outside the framework of Map Interface

Map the keys mapped to the value of the object. A map can not contain duplicate keys; each key can only map a value.

Map interface is Dictionary (dictionary) abstract class alternatives.

Map interface provides three types of collection view allows key set, set of values or key - the value set in the form of mapping view a map of the content. Order is defined as the maps in the map collection iterator returns the elements in view of the order. Some of the maps can be clearly guaranteed in order to achieve, such as the TreeMap class; some of the maps are not guaranteed to achieve the order, such as the HashMap class.

There are two common sub-type has been achieved:

HashMap: Map-based hash table implementation of the interface. This implementation provides all optional map operations, and allows the use of null values and null keys. (In addition to asynchronous and allows the use of null addition, HashMap Hashtable class with roughly the same.) Mapping the sequence of such does not guarantee, in particular, it does not guarantee that the order of constancy.

TreeMap: SortedMap interface it implements red-black tree implementation based. Such guarantees mapping keywords in ascending order according to the structure using different methods, may follow the natural order of keys to sort the class (see Comparable), or provided in accordance created sort comparator.

Hashtable: these implement a hash table, the hash table key is mapped to the corresponding value. Any non-null object can be used as a key or value.

5, thread-safe class

Framework in the collection, some classes are thread-safe, these are emerging in JDK1.1. In JDK1.2, the respect of many non-thread safe there class.

Here is a thread-safe synchronization of class:

Vector: ArrayList on more than one synchronization mechanism (thread safe).

Statck: stack class, advanced out after.

Hashtable: HashMap on more than one thread-safe.

Enumeration: enumerate, the equivalent of an iterator.

Other than that, the rest are non-thread-safe classes and interfaces.

Thread-safe class of its methods are synchronized, only one visit each. Is a heavyweight object, and less efficient. For non-thread-safe classes and interfaces needed in a multi-thread thread-safety issues dealing with the programmer.

6, other interfaces and classes introduced

Dictionary and Hashtable class:

Dictionary providing key mapping feature, is an abstract class. Subclass of general use HashTable class. Traverse the Hashtable class to use to enumerate.

Properties class

Properties inherited from Hashtable, Properties class represents a persistent set of attributes. Properties can be saved in the stream or loaded from a stream. Property list and the corresponding value of each key is a string. The general properties configuration file can be read to fill the Properties object.

No comments:

Post a Comment