Sunday, June 7, 2015

Comparable and Comparator- When and how to use? [Concise]

When there comes the need of sorting objects, be it using Collections.sort() or just adding elements to Treeset etc, java.lang.Comparable and java.util.Comparator come into picture. For default sorting of objects in the collection, we need to implement Comparable and override below method in that class, unless we are using some pre-defined class that already implements Comparable like String, Integer etc.,
int compareTo(T o)
If we want to provide an external sorting logic to override the default one, the class needs to implement Comparator interface and override the following method:
int compare(T o1, T o2)
The question: Why use Comparator when we already have Comparable?
Answer: If you have authored that class, then you can give a default sorting behavior by implementing Comparable and use that logic to sort whenever needed. Now, let's say that you are using an existing class whose source you can not modify and the default sorting logic of which you are not satisfied with. What now? Just write a Comparator using your own sorting process and use that instance to sort. See below sample programs.

The API information is here:
1. java.lang.Comparable
2. Java.util.Comparator
Lets dig into this with our custom class.

Thursday, January 29, 2015

native2ascii - Native-to-ASCII Converter in Java

This utility converts a file with characters in any supported character encoding to one with ASCII and/or Unicode escapes, or visa versa.

You already have this tool if you have JDK. It is present in your JDK installation bin folder. Its recommended to add JAVA_HOME to your PATH environment variable for access of java tools throughout your machine.

Saturday, December 20, 2014

Falling in love with InteliJ Idea Community IDE

The local file history that keeps a track on my changes to a file, I am all smiles now! :~)

Local History - Click on the image to enlarge

Tuesday, May 20, 2014

Java Map/Collections Cheat Sheet : Simply 'When and What to use'?

This is a notoriously simple when and what to use Java collection API cheat sheet, containing most common implementations.

Monday, May 19, 2014

Find the occurrence of words in a given string

package com.learning.collection;
import java.util.*;
 * Count frequency of words occurring in a string
public class MapExample {
    public static void main(String[] args){
        String str = "ika pika ting tong ting tong me";
        String[] strr = str.split("\\s");

    static void workMap(String...words){
        Map map = new HashMap();
        Integer ONE = new Integer(1);

        for(String word : words){
            Integer frequency = (Integer)map.get(word);
            'frequency' is the count of the words.
            For a new word getting added to the Map, we set its frequency as 1.
            For existing words, we take their existing frequency (value in the map)
            and increment it and put back into the map correspondint to that word (key in the map)
            frequency = (frequency == null) ? ONE : new Integer(frequency.intValue() + 1);
            map.put(word, frequency);

        Map sortedMap = new TreeMap(map);
Unsorted: {ika=1, pika=1, ting=2, tong=2, me=1}
Sorted: {ika=1, me=1, pika=1, ting=2, tong=2}

--Suggestion by Dr. Heinz Kabutz (Java Champion)
How you would count words in Java 8 :-) And if you want to do it in parallel, just add parallel() into the stream...
import java.util.*;
import java.util.function.*;

public class MapExample {
  public static void main(String[] args) {
    String str = "hello world how are you how is life i am into ascent";

    Map<String, Integer> map ="\\s"))
            n -> 1,
            (n1, n2) -> n1 + n2,
    System.out.println("map = " + map);