Monday, September 27, 2010

Why Map is not under Collection interface?

HashMap is a part of the Collections Framework, but it is not a (does not extend) Collection type of collection(The Map interface is not an extension of Collection interface. Instead the interface starts of it’s own interface hierarchy, for maintaining key-value associations). All of the methods of the collection interface would appear to make sense for Map, except for iterator(). A Map, again, has two sets of objects: keys and values and seperate iterator for each. This is why a Map is not a collection.

How to Use an object as key for a hashmap in Java?

Using an object as key for HashMap then we need to make our class override the Object.equals() and Object.hashCode() methods.

I taken example as a ‘Person’ in a network, with my constructor looking something like the following:

public Person(String name) {
this.name = name;
}

now I use the following overrides:

public int hashCode()
{
return name.toString().hashCode();
}

public boolean equals(Object o) {
Person person = (Person)o;

if (person.name.equals(name))
{
return true;
}

return false;
}

and when i’m testing whether a particular key exists in the hashmap, I can create a new Person and check directly whether it currently exists as a key in the hashmap. Typically, if you didn’t implement this, then two person objects would have different references (even if they have the same name) and would not be equal.

File size in Java

import java.io.*;

public class FileSize {

public static void main(String[] args) {

//create file object
File file = new File("C://FileIO/demo.txt");

/*
* To get the size of a file, use
* long length() method of Java File class.
* This method returns size of a particular file in bytes. It returns 0L
* if file does not exists, and unspecified if file is a directory.
*/

long fileSize = file.length();

System.out.println("File size in bytes is: " + fileSize);

System.out.println("File size in KB is : " + (double)fileSize/1024);

System.out.println("File size in MB is :" + (double)fileSize/(1024*1024));

}

}

Sunday, September 5, 2010

How to get Date and Time Format in java?

private String getCurrentDateTime() {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
return dateFormat.format(date);
}

Friday, September 3, 2010

Can't start Server within 50 seconds from Eclipse. How to increase the Time out value?

Can't start Server within 50 seconds from Eclipse. How to increase the Time out value.

Eclipse gave the following error when I tried to run a jboss server for my webservice application.

Server JBoss v5.0 at localhost was unable to start within 50 seconds. If the server requires more time, try increasing the timeout in the server editor.

Fine, increase the time in server editor.. should be straight forward, I thought.. but then, I can't find a time out field to change the value...


After a minute or so, it turned out that you need to double click on the server that appears in the "Servers" tab("Servers" tab usually appears next to the "Console" tab). When you double click, a new window is opened in the main editor and then, Expand the "Timeouts" section..
Now I can change the 'Start time out value' to 300, then its worked fine....