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

IDEONE: http://ideone.com/9SENHc
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");
        workMap(strr);
    }

    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);
        }
        System.out.println("Unsorted:");
        System.out.println(map);

        Map sortedMap = new TreeMap(map);
        System.out.println("Sorted:");
        System.out.println(sortedMap);
    }
}
Output:
Unsorted: {ika=1, pika=1, ting=2, tong=2, me=1}
Sorted: {ika=1, me=1, pika=1, ting=2, tong=2}

Wednesday, April 30, 2014

Finding the JAR files which contain a specific .class file on Linux machine

This program searches through all .jar files in the current directory, and in any sub-directories, looking for the class that you specify. This can be very handy if you need to use a class in a Java program, but aren't sure which .jar file contains it.

Logic of script: The key is to get a list of all the jar files in present directory, open each jar archive and search it with our input classname, and for all positive output, print the name of the jar file on screen.