Leaderboard1

Leaderboard2

Wednesday, May 28, 2014

Importance of equals() and hashCode() methods in Java objects

equals() and hashCode() methods are required when you insert objects in s collection. Both play a very important role for Java objects. Both methods are defined in Object class. As Object class is the parent of all classes by default, so we override these methods in our objects and give our own definitions. 

equals()

equals() is required to compare the properties of one object with other. equals() method uses identity operator(= =)to determine whether object are equal. In equals()we compare only those properties on the basis of them objects are compared. For primitive data types like int, double identity operator is sufficient but in case on objects we need more functionality for comparing. For example:

class Employee{
private String name;
private int age;
public Employee(String name, int age){
this.name = name;
this.age = age;
}
@Override
public boolean equals(Object obj){
if(obj != null && obj instanceof Employee){
if(((Employee) obj).name.equals(this.name) && ((Employee) obj).age == this.age){
return true;
} else
return false;
} else
return false;
}
}

Comparing Employee class objects.

Employee e1 = new Employee("john", 25);
Employee e2 = new Employee("Dev", 26);
System.out.println(e1.equals(e2)); // false


hashcode()

hashCode() method is used when object are inserted in HashMap, HashTable, HashSet. When inserting an object into a hastable you use a key. The hash code of this key is calculated, and used to determine where to store the object internally. When you need to lookup an object in a hashtable you also use a key. The hash code of this key is calculated and used to determine where to search for the object. 

The hash code only points to a certain "area" (or list, bucket etc) internally. Since different key objects could potentially have the same hash code, the hash code itself is no guarantee that the right key is found. The hashtable then iterates this area (all keys with the same hash code) and uses the key's equals() method to find the right key. Once the right key is found, the object stored for that key is returned.

Here are two rules that are good to know about implementing the hashCode() method in your own classes, if the hashtables in the Java Collections API are to work correctly:

  1. If object1 and object2 are equal according to their equals() method, they must also have the same hash code.
  2. If object1 and object2 have the same hash code, they do NOT have to be equal too.

In shorter words:
  1. If equal, then same hash codes too.
  2. Same hash codes no guarantee of being equal.

class Employee{
private String name;
private int age;
public Employee(String name, int age){
this.name = name;
this.age = age;
}
@Override
public boolean equals(Object obj){
if(obj != null && obj instanceof Employee){
if(((Employee) obj).name.equals(this.name) && ((Employee) obj).age == this.age){
return true;
} else
return false;
} else
return false;
}
@Override
public int hashCode(){
return this.name.hashCode() * this.age; // calculating hashCode using Employee properties   
}
}


Please post comments !

Saturday, May 24, 2014

How To Install Hibernate / JBoss Tools In Eclipse IDE

As creating Hibernate mapping files manually can causes some issues so it's better to use Hibernate/Jboss tools for generating hibernate mapping files automatically.

1. Download Hibernate/JBoss tool

Go to Eclipse: help->Intall New Software, and copy this url:

For Eclipse Indigo





2. Restart Eclipse

Restart Eclipse to take effect.



Please like us !

How to Take Screenshot in Ubuntu Linux

In this post i'll show you how to take ScreenShot in Ubuntu Linux.

1. Use Print Screen key

Use PrintScreen key to take ScreenShot of whole desktop or Alt+PrintScreen to take ScreenShot of current active window.

2. Use gnome-screenshot

go to Applications and find "Screenshot", it's provided by Ubuntu.



click "Take ScreenShot" key and save it to desktop. Open image with image editor and crop it if required.

3. Use terminal to take ScreenShot

For entire screen screenshot type:

  gnome-screenshot

For current active windows screenshot type:

  gnome-screenshot -w

4. Take Screenshot after some delay


  $ gnome-screenshot -w -d 2

5. Capture a particular area

This is very easy way to capture a particular area by using terminal.

  $ gnome-screenshot -a


Please like this post !


Wednesday, May 21, 2014

Create a Java Project with Maven

Tools used:

  1. Maven 3.0.5
  2. Eclipse 4.2
  3. JDK 6

Steps:

1. Create a Project from Maven Template

Go to a folder where you want to create Maven project, and run this command on command prompt.

mvn archetype:generate -DgroupId=com.akash -DartifactId=MavenTest -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

This will download artifacts, dependencies and create a project structure.


Project Structure:


MavenTest

   |-src

   |---main

   |-----java

   |-------com

   |---------akash

   |-----------App.java

   |---test

   |-----java

   |-------com

   |---------akash

   |-----------AppTest.java

   |-pom.xml

Note: Keep source code in folder /src/main/java/project-package, all unit test code puts in /src/test/java/project-package.

Now a POM.xml file has been generated.

What is POM?

A Project Object Model or POM is the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details used by Maven to build the project. It contains default values for most projects. Examples for this is the build directory, which is target; the source directory, which issrc/main/java; the test source directory, which is src/main/test; and so on.

Update POM.xml



















3. Make Maven project compatible with Eclipse

Run this command: mvn eclipse:eclipse

4. Update AppTest.java


package com.akash;

import org.junit.Assert;
import org.junit.Test;

public class AppTest {

@Test
public void testLengthOfTheUniqueKey() {

App obj = new App();

}

}



5. Compile Project and create jar

run mvn package on cmd

It will compile Maven project and create a jar.

Create A Web Application Project With Maven

In this post we will learn to create Web Application using Maven. If Maven is not configured in your system, please refer this link; Configure Maven on Windows. 

Tools required:
  1. Eclipse IDE for Java EE Developers
  2. M2E plugin for Eclipse
  3. Maven
Steps:

1. Install Maven eclipse plugin

http://download.eclipse.org/technology/m2e/releases

You can install Maven plugin for Eclipse via update site, simply copy the above update site link address and paste it into Eclipse’s “Update” or “Install New Software” manager as explained below.


Restart Eclipse.

2. Create Dynamic Web project in eclipse

click next.

--> Create the following directory structure (please pay attention to the directory structure. It’s an important detail in the creation of Dynamic Web Project):


click next, and generate deployment descriptor, web.xml 

Press Finish.

Directory structure:


3. Convert to Maven project

Now right click on project, go to configure and click on "convert to Maven project".


  • Select “Packaging” as a WAR;
  • Click on the “Finish” button;
  • After that, pom.xml will appear in the project’s structure;
  • Move all stuff from the WebContent folder to src/main/webapp;
  • WebContent folder can be deleted after that.
4. Configuration for Eclipse
  • Go to projects root folder in cmd and run this: mvn eclipse:eclipse -Dwtpversion=2.0
  • This command will generate a configuration for eclipse (.classpath, .project, etc.).
  • Expand src > main and make a right click on the webapp folder: Build Path > Use as Source Folder (This point is actual only if src/main/webapp disappeared from the source folders).
Share this !