Monday, November 19, 2012

Program to get contents of an excel sheet

import java.io.File;
import java.io.IOException;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class ReadExcelSheet {
   private String inputFile;
   
  public void setInputFile(String inputFile) {
        this.inputFile = inputFile;
   }

  public void read() throws IOException  {
        File inputWorkbook = new File(inputFile);
        Workbook w;
        try {
            int rowCount = 0;
            int columnCount = 0;
            Cell rowData[] = null;
            // Get workbook instance
            w = Workbook.getWorkbook(inputWorkbook);
            // Get the first sheet
            Sheet s = w.getSheet(0);
            rowCount = s.getRows();          
            columnCount = s.getColumns();
         
            // Reading Individual Row Content
            for (int i = 0; i < rowCount; i++) {
                // Get Individual Row
                rowData = s.getRow(i);
                if (rowData.length > 1) {
                    System.out.println("\n");
                    for (int j = 0; j < columnCount; j++) {
                        if (rowData[j] != null)
                            System.out.print(rowData[j].getContents() +"----");
                    }
                }
            }
        } catch (BiffException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) throws IOException {
        ReadExcelSheet test = new ReadExcelSheet();
        test.setInputFile("D:\\testcodeline\\test\\Published_List 186.xls");
        test.read();
    }
}

Wednesday, May 11, 2011

Tricky way to convert an array list into a comma separated string

Today my husband suggested me a good way to convert an array list into string, i found it very efficient so sharing with you all.

Created a list l, added megha, dwivedi and jain into it. Need to store - megha, dwivedi, jain into a string so used toString() method.

List l = new ArrayList();
l.add("megha");
l.add("dwivedi");
l.add("jain");
String s = l.toString().substring(1, l.toString().length()-1);

If we perform toString on the list the output will be -

[megha, dwivedi, jain]

but i dont need square brackets so used substring method to cut 1st and last character, so variable 's' will contain - megha, dwivedi, jain

Other inefficient way which comes first in our mind is to iterate through list, get string one by one and append it comma separated, but using the above mentioned way we can avoid all those unnecessary loops and variables.

Thanks to my husband for explaining the same ;)

Monday, May 9, 2011

Run java code without main method

public class test{

 static{

System.out.println("M printing :p");

System.exit(0);

}

}

As soon as class loads it will run static initializer and before it tries to search for main method, we are exiting the program with System.exit, so prints the message and then will exit normally(without throwing any exception)

SQL related interview question

How to get the below outputs when executing a select on say employee table(suppose employee table is empty)-

1) No rows found

2) Zero rows found

Any idea??? :)


select * from employee  ---- it will give 1st output

and

select count(*) from employee -- it will give 2nd output

It looks so simple but doesn't click in mind when being interviewed :)

Thursday, January 6, 2011

Making Collections read only

Making a Collection Read-Only

Making a collection read-only involves wrapping the collection in another object whose mutation methods all throw UnsupportedOperationException.

List stuff = Arrays.asList(new String[]{"m", "d"});

// Make a list read-only

List list = new ArrayList(stuff);

list = Collections.unmodifiableList(list);

try {

// Try modifying the list

list.set(0, "value");

} catch (UnsupportedOperationException e) {

// Can't modify

}

// Make a set read-only

Set set = new HashSet(stuff);

set = Collections.unmodifiableSet(set);

// Make a map read-only

Map map = new HashMap();

// Add key/value pairs ...

map = Collections.unmodifiableMap(map);

Shuffling the Elements of a List or Array

Shuffling the Elements of a List or Array

Use Collections.shuffle() to randomly reorder the elements in a list.

// Create a list

List list = new ArrayList();

// Add elements to list

// Shuffle the elements in the list

Collections.shuffle(list);

// Create an array

String[] array = new String[]{"M", "e", "g", "h", "a"};

// Shuffle the elements in the array

Collections.shuffle(Arrays.asList(array));

Comparing three implementations of List interface

Comparing three implementations of List interface 

Classes implementing List

Underlying data structure

Pros

Cons

ArrayList

array

1.        Faster to access by index

 

2.        Faster than Vector as methods are not synchronized

1.        Insertion or deletion in the middle is slower.

2.        Not thread –safe.

3.        Searching is sequential and hence not very efficient.

Vector

array

1.        Faster to access by index.

 

2.        Thread-safe access

1.        Insertion or deletion in the middle is slower.

2.        Slower than ArrayList as methods are synchronized.

3.        Searching is sequential and hence not very efficient.

LinkedList

doubly linked list

1.        Allows faster inserts/delete at any location*

2.        Faster than Vector as methods are not synchronized.

1.        Accessing element by index is slower.

2.        Searching is sequential and hence not very efficient.

Comparing three implementations of Set interface

Comparing three implementations of Set interface 

Classes implementing Set

Underlying data structure

Pros

Cons

HashSet

hash-table

1.        Useful when you want to impose only uniqueness among elements.

2.        Faster than other Set implementation as it does not need to maintain any order.

1.        Do not guarantee any order among elements.

2.        Not thread –safe.*

LinkedHashSet

hash-table

and

doubly linked list

1.        Maintains the insertion order.

1.        Slower than HashSet as it maintains the insertion order.

2.        Not thread –safe.*

TreeSet

tree

1.        Maintains the natural order among elements.

2.        You can impose different order by passing your own Comparator.

1.        Not thread –safe.*

Wednesday, December 8, 2010

Determine if Class or Interface??

How to determine if a Class object represents a Class or Interface??

You can determine by calling isInterface method in Class-
...
Class cls = java.lang.String.class; 
// returns true as String is a class and not an Interface
boolean isClass = !cls.isInterface();
cls = java.lang.Cloneable.class; 
isClass = !cls.isInterface(); // false as Cloneable is an interface, note negation sign
...

So at runtime if you want to know about the entity you are working with, the Class or Interface, you can use the above method.

Tuesday, December 7, 2010

Break Oop's concepts - Java Reflection API

As you all know that we can not access private members of a class, but using Java Reflection API you can. Yes you can break Oops concept by accessing private variable, by calling private methods and also you can create an instance of class which is having private constructor-

Access private members of a class

/**
Class having only one private member
*/

package com.examples;
class test
{
private int youCanAccessMe;
}


Now to access "youCanAccessMe" variable -

package com.examples;
import java.lang.reflect.Field;
public class ReflectExample
{
public static void main(String args[])
{
try
{
Class cls = Class.forName("com.examples.test");
Object obj = cls.newInstance();
Field[] fields = cls.getDeclaredFields();
for( int i = 0 ; i <>fields[i].setAccessible(true);
System.out.println("Field Name-->"+fields[i].getName()+"\t"
+"Field Type-->"+ fields[i].getType().getName()+"\t"
+"Field Value-->"+ fields[i].get(obj));

}
}
catch(Exception e)
{
System.out.println("Exception occured" + e);
}
}
}

You can get the Class object and using Class object you can also create an object of class.
Class cls = Class.forName("com.examples.test");
Object obj = cls.newInstance();
To access private member variable, you have to call setAccessible method to set accessibility level to true.
fields[i].getName() - To get name of variable
fields[i].getType().getName() - To get the type of variable
fields[i].get(obj) - To get its values
Thus Reflection is a powerful approach to analyze the class at runtime.

New post - which is irrelevant in this blog

Hi, i am publishing this post here, which i liked and is not supposed to post in this blog, but i can't resist posting it. One of my friend(Uday Shankar) has written a very touchy poem(Only people who are in love or had break up can understand).

"beautiful memories".

We had our first glance,
that was the start of our romance.
Then we had a small talk
while we had a pleasant walk.

We had some fortunate encounters
and we became regular chatters.
We always enjoyed each others company
so we started meeting few times ... then many.

Love was in the air,
each other was all, we care.
One day those feelings were expressed.
From that moment life get blessed.

We had our first fight,
but we can't live without others sight.
so we make up our mind,
to never leave each other behind.

Don't know what had gone wrong
I'm feeling dreadful for so long
The life has parted our ways
But those were my most beautiful days.

Monday, December 6, 2010

Java 5 feature - Covariant return types

Now with Java 5 you can override a function and can make their return type different* which was not allowed before Java 5.
The return type of the subclass' method must be a subclass of the return type of the superclass' method. In other words you can make the return type of the subclass' method the same or less general.

Confused???? Lets understand with an example -
class Clothe { 
     public clothe getDesign() {
        System.out.println("clothe");
        return new Clothe();
    }
}
class Shirt extends Clothe {
    public Shirt getDesign() {  
     System.out.println("shirt");
     return new Shirt();
   }
}

An overridden method in a derived class can return a type derived from the type returned by the base-class method.

public class CovariantReturn {
    public static void main(String[] args) {
       clothe clothe = new clothe();
       System.out.println(clothe.getDesign());
       clothe = new Shirt();
       System.out.println(clothe.getDesign());
   } 
}   
Output:
clothe
clothe@923e30
shirt
Shirt@130c19b

Allowing covariant return types in Java has been in Sun's bugparade for a long time (http://bugs.sun.com/bugdatabase/view_bug.do? bug_id=4144488). Now finally, JDK 1.5 supports it.

Saturday, December 4, 2010

How to create Case Insensitive Map in Java

In order to create case insensitive map, just extend HashMap class and override get and put method. Using the below code you can give keys irrespective of case.
class CaseImap extends HashMap<String, Object>
{
// Overriding get method
public String
get(String key)
{
return (String)super.get(key.toLowerCase());
}
// Overriding put method
public Object
put(String key, Object value)
{
return super.put(key.toLowerCase(), value);
}
}

Friday, December 3, 2010

Using isInfoEnabled in Java

-Logging using log.info
 Just have the below "if" block before using log.info or log.debug or log.error
  if(log.isInfoEnabled()
 {
  log.info(yourFunctionName + " :--- " + getVeryLargeString() + someVar);
 } 
Why?
Suppose the current log4j level is set to Error then log.info messages will not be displayed, it will take only few nanoseconds to execute log method but it may take time to execute the function call, if any, in the message.
Drawback of not using if condition
1) Unnecessary call to the functions in the log.info message 
2) Many string objects will be created in the string pool
Drawback of using if condition-
1) For each and every log method call u have to have this condition, code will not be readable and not be clear much.
2) Extra if condition in many places.

Jdk1.5 has solution for this problem, it removes log calls automatically removed if they're not being used.

It is possible to do this using bytecode manipulation - searching for and removing calls to the logger as the class is loaded. The new Java 1.5 instrumentation classes make this particularly easy, by allowing ClassTransformer classes to be registered on the command line that are allowed to manipulate class files as they are loaded.

To use this code, launch Java with the arguments:
java -javaagent:net.surguy.logfilter.LogPreloader=debug [your own class]

The argument is optional, and can be one of "debug", "info", "warn", "error" or "fatal", corresponding to the standard Log4J log levels.

How does it work
As Java loads each class, it passes it to each of the registered ClassFileTransformers, which get a chance to alter its bytecode. It uses the ASM bytecode manipulation library to do so.