Friday, May 29, 2009

NavigableMap & NavigableSet interface

The NavigableMap interface extends the SortedMap interface in which the elements are ordered either by natural ordering or by using a Comparator. It contains methods to find out the closest matches for given search targets. All keys inserted into a sorted map must implement the Comparable interface and must be mutually comparable. The elements of the NavigableSet interface can be accessed and traversed in either ascending or descending order.

The NavigableSet interface extends the SortedSet interface in which the elements are ordered either by natural ordering or using a Comparator. It contains methods to find out the closest matches for given search targets. The elements of the NavigableSet interface must be mutually comparable. The elements of the NavigableSet interface can be accessed and traversed in either ascending or descending order.

Matcher class

Matcher is a final class present in the java.util.regex package. It is used to match a regular expression using the pattern defined by the Pattern class.

The Matcher class defines no public constructors. It is created from a pattern by invoking the Pattern's matcher method as follows:

public Matcher matcher(CharSequence input)

This method creates a matcher that will match the given input against the given pattern.

The other important methods of the Matcher class are as follows:

  • int start(): This method returns the index of the first character matched.

  • public int end(): This method returns the index of the last character matched, plus one.

  • boolean find(): This method searches the subsequence of the input sequence that matches the pattern. It returns true if the match is found; otherwise, it returns false.

  • boolean lookingAt(): This method searches through the input sequence to match the pattern. It returns true if the match is found; otherwise, it returns false.

Externalizable interface

The Externalizable interface allows a programmer to have a complete control over the serialization and deserialization processes.

The Externalizable interface has the following two methods:

  • public void writeExternal(ObjectOutput objout): This method is used to save the contents of an object by calling the methods of the DataOutput interface for its primitive values or calling the writeObject method of ObjectOutput for objects, strings, arrays, etc.


  • public void readExternal(ObjectInput objin): This method is used to restore the contents of an object that was saved using the writeExternal() method. It calls the methods of the DataInput interface for primitive types and the readObject() method for objects, strings, arrays, etc.

Comparable & Comparator interface

The Comparable interface is used to sort collections and arrays of objects using the Collections.sort() and java.utils.Arrays.sort() methods respectively. The objects of the class implementing the Comparable interface can be ordered.

The Comparable interface in the generic form is written as follows:

interface Comparable, where T is the name of the type parameter.

All classes implementing the Comparable interface must implement the compareTo() method that has the return type as an integer. The signature of the compareTo() method is as follows:

int i = object1.compareTo(object2)

  • If object1 <>
  • If object1 > object2: The value of i returned will be positive.

  • If object1 = object2: The value of i returned will be zero.

The Comparator interface is used to sort the elements of collections and arrays. It differs from the Comparable interface in that the Comparable interface sorts the elements only in the natural order. In contrast, the Comparator interface sorts the collections and arrays in a number of different ways.

The Comparator interface has a single compare() method, which returns an integer. It has the following signature:

int Obj1.compare(Obj1, Obj2): This method compares the order of the method arguments and returns the following results:

  • negative if Obj1 <>
  • positive if Obj1 > Obj2

  • zero if Obj1 == Obj2
equals(Object obj): This method is used to compare the equality of two objects. The objects being compared must also implement the Comparator interface.

The Comparator interface can sort only the mutually comparable objects. It cannot be used to sort primitives. Sorting elements using the Comparator interface requires building a class separate from the class whose instances are to be sorted. This is in contrast to the Comparable interface, in which case the class to be sorted itself uses the Comparable interface.

Serializable

Serializable is a marker interface. (A marker interface is one without any method). The classes that do not implement this interface will not have any of their states serialized or deserialized. Serialization is the process of converting an object into byte stream, so that it can be transferred over a network and stored into a persistent medium such as a file.

Serialization is the process of converting the state of an object into a form that can be transported or stored. In this process, an object's state is written to a temporary or persistent storage. Later, the object can be recreated by deserializing or reading the object's state from the storage.

Coupling

Coupling is a term that describes the degree to which one module relies on another module for its proper functioning. Coupling is mainly of the following two types:

  • Loose coupling: Loose or weak coupling implies that a change in one module does not require changes in the implementation of another module.

  • Tight coupling: Tight or strong coupling implies that a module relies on another module so strongly that a small change in one will require an implementation change in the other.
For a good object-oriented design, loose coupling is desirable.

Cohesion

Cohesion is a measure of degree of how strongly a class focuses on its responsibilities. It is of the following two types:

  • High cohesion: This means that a class is designed to carry on a specific and precise task. Using high cohesion, methods are easier to understand, as they perform a single task.
  • Low cohesion: This means that a class is designed to carry on various tasks. Using low cohesion, methods are difficult to understand and maintain.
In object-oriented design, high cohesion is desirable, as it is easy to understand and maintain. Moreover, it is easier to reuse.

Differences between exceptions and assertions

ExceptionsAssertions
An exception tells the user of the program that something in the program went wrong.An assertion documents a program. When it fails, it informs that the program has a bug.
Exceptions are created to deal with problems that might occur in the program. Assertions are written to state the concepts of a program.
Exceptions are used to test input/output as well as whether parameters are legal or not.Assertions are documentations that can be checked by running the program.

checked and unchecked exceptions

Unchecked Exceptions: Exceptions that are defined by the Error and RuntimeException classes and their subclasses are known as unchecked exceptions. It is not necessary that a method must deal with these kinds of exceptions. If the exceptions are irrecoverable, the program should not attempt to deal with them. The method can only deal with the exceptions that are recoverable.

Checked Exceptions: All exceptions except the Error and RuntimeException classes and their subclasses are known as checked exceptions. If a method throws a checked exception, it is necessary that the method must explicitly deal with the exception. The method must catch the exception and take the necessary action.

Closeable and Flushable interfaces

The Closeable and Flushable interfaces define a uniform way for specifying that a stream can be closed or flushed.

Closeable: The objects that implement the Closeable interface can be closed. This interface defines the close() method.

void close() throws IOException

It closes the invoking stream and releases all the resources. It is implemented by all the I/O classes that open a stream.

Flushable: The objects that implement the Flushable interface can force buffered output to be written to a stream to which the objects are attached. This interface defines the flush() method.

void flush() throws IOException