Thursday 17 July 2008

Difference between a comparator and comparable

If the List consists of String elements, it will be sorted into alphabetical order. If it consists of Date elements, it will be sorted into chronological order.

How does this happen?

String and Date both implement the Comparable interface. Comparable implementations provide a natural ordering for a class, which allows objects of that class to be sorted automatically

If you try to sort a collection (yourCollection), the elements of which do not implement Comparable, Collections.sort(yourCollection) will throw a ClassCastException.

Writing Your Own Comparable Types
The Comparable interface consists of the following method.

public interface Comparable {
public int compareTo(T o);
}

The compareTo method compares the receiving object with the specified object and returns a negative integer, 0, or a positive integer depending on whether the receiving object is less than, equal to, or greater than the specified object.

Now Any collection which maintains the natural ordering like TreeMap, TreeSet would automaticaly sort the entries on the logic of compareTo method. For others cases you would just call the Collection.sort(Collection col) on your collection whenever you want the entries to be sorted on natural order.

Comparators

What if you want to sort some objects in an order other than their natural ordering? Or what if you want to sort some objects that don't implement Comparable?
e.g. naturally String sort the collection in dictionary order. What is you want the sorting to be just opposite of it ?
you'll need to provide a Comparator in such cases — an object that encapsulates an ordering.

Like the Comparable interface, the Comparator interface consists of a single method.

public interface Comparator {
int compare(T o1, T o2);
}

The implementation symantics are same to Comparable.

import java.util.*;

public class ReverseSort {
static final Comparator REVERSE_ORDER =
new Comparator() {
public int compare(String e1, String e2) {
return e2.compareTo(e1.hireDate()); //reverse comparison
}
};

static final Collection names = ... ;

public static void main(String[] args) {
List e = new ArrayList(names);
Collections.sort(e); //This would sort the names in Natural Order ... Dictionary order ...
System.out.println(e);

System.out.println("");
System.out.println("Reverse Order ...");
Collections.sort(e, REVERSE_ORDER);
System.out.println(e);
}
}


Wednesday 16 July 2008

java 5 jconsole is VisulaVM in Java 6.0

The jconsole has been upgraded with some exciting features in Java
6.0.It is able to monitor applications running on JDK 1.4 and higher.
It utilizes various available technologies like jvmstat, JMX, the
Serviceability Agent (SA), and the Attach API to get the data and
automatically uses the fastest and most lightweight technology to
impose minimal overhead on monitored applications.

Display local and remote Java applications.
VisualVM automatically detects and lists locally and remotely running Java applications (jstatd
must be running on the remote host). You can also define applications
manually by JMX connection. This way you can easily see what Java
applications are running on your system or check if a remote J2EE
server process is alive.

Display application configuration and runtime environment.
For each application VisualVM shows basic runtime information: PID, main class, arguments passed to java process, JVM version, JDK home, JVM flags and arguments and system properties.

Monitor application memory consumption and runtime behaviour.

VisualVM monitors application heap and permanent generation memory,
number of loaded classes and running threads. You can easily detect
suspicious memory consumption and take an action - invoke garbage
collection in the application or take a heap dump and browse the
contents of application heap.

Monitor application threads.

All threads running in a Java process are displayed in a timeline. You
can track thread activity and uncover inefficient patterns like blocked
Event Dispatch Thread or unused worker threads.

Profile application performance or analyze memory allocation.

VisualVM has a built-in application profiler which can visualize where
most of the time is being spent or which objects consume most of the
memory by just one mouse click without any additional configuration.

Take and display thread dumps.

Taking and displaying a thread dump is as easy as clicking a mouse
button. You don't need to deal with the command line at all to
determine what's currently happening in the application. Moreover,
simultaneous thread dumps of multiple applications can be taken at once
to start uncovering distributed deadlocks.

Take and browse heap dumps.

When you need to browse contents of application memory or uncover a
memory leak in your application, you'll find the built-in HeapWalker
tool really handy. It can read files written in hprof format and is also able to browse heap dumps created by the JVM on an OutOfMemoryException.

Analyze core dumps.

When a Java process crashes, a core dump can be generated by the JVM
containing important information about application state at the time of
the crash. VisualVM is able to display application configuration and
runtime environment and to extract thread and heap dumps from the core
dump.

Analyze applications offline.

VisualVM is able to save application configuration and runtime
environment together with all taken thread dumps, heap dumps and
profiler snaphots into a single application snapshot which can be later
processed offline. This is especially useful for bug reports where
users can provide a single file containing all the necessary
information to identify runtime environment and application state.

visit for more details https://visualvm.dev.java.net