Equals and hashCode mystery resolved

Equals and hashCode mystery resolved

Hi,

Try executing the code in the attached files first by commenting the hashcode() method in Test1.java and then by uncommenting.

The output differs.

In first case the objects are equal based in the criteria defined in the overridden equals method but when u try to put these two “equal” objects in a hashtable it allows you inspite of the fact the keys (objects) are equal.

Here: both objects are equal but hashtable consider them different so allows two entries.

In second case when we have overridden the hashcode() method for objects of class Test1 uncommented then the hashtable doesn’t allow to put the equal objects twice.

Here: both objects are equal and hashtable consider them equal because of overridden hashtable method so allow only one entry

import java.util.Hashtable;
public class RunTest
{
public static void main (String args[])
{
Test1 test1= new Test1("start", 10);
Test1 test2= new Test1("start", 10);
Hashtable ht = new Hashtable();
System.out.println("test1.hashCode=" + test1.hashCode());
System.out.println("test2.hashCode=" + test2.hashCode());
System.out.println("test1.equals(test2) = " + test1.equals(test2));
if(test1.hashCode()!=test2.hashCode())
{
System.out.println("hashCodes are different --> means Objects are unequal");
if(test1.equals(test2))
{
System.out.println("Please note test1.equals(test2) returns " + test1.equals(test2));
System.out.println("Still it considers the objects to be unequal");
}
System.out.println("and adds it to hashTable");
}
else
{
System.out.println("hashCodes are same");
System.out.println("means");
System.out.println("Objects can be equal or unequal");
System.out.println("Now it'll check equals method");
System.out.println("test1.equals(test2) = " + test1.equals(test2));
if(test1.equals(test2))
{
System.out.println("Since equals method returns true, it'll not allow 2 equal elements to be added");
}
}
ht.put(test1,"first");
ht.put(test2,"second");
System.out.println("the size of ht is :" + ht.size());
}
}
public class Test1
{
String s;
int t ;
public Test1(String s, int t)
{
this.s= s;
this.t=t;
}

public boolean equals(Object obj)
{
if(this == obj)
{
return true;
}
if(!(obj instanceof Test1))
{
return false;
}
Test1 test = (Test1)obj;

return (s.equalsIgnoreCase(test.s) && t==test.t);
}

public int hashCode()
{
return (this.s.hashCode() + this.t);
}
}


How to wrap longer lines in SQL file…

A post by Aashu Aggarwal…

More i learn UNIX, more i feel its miraculous 🙂

1. There is a direct way to split the lines in unix. Command is “fold” It can directly split the huge lines into multiple

Syntax>> fold -200 input_file > output_file

This will split all the lines in input_file to lines less than equal to 200 characters. Like say if there is aline with 500 characters, that will be split into 3 lines with 200 character s in 2 lines and 100 in 1 line.

2. If i need to split a line around some character or word then below is the command Syntax >> perl -pe ‘s/(.{1,199}[,])/1n/g’ input_file > output_file

In this command will insert newline character after last comma in next 200 characters .{1,199} is telling to see after 200 character [,] is the character to split around. Multiple character can be given like [,:]. It will use both comma and colon to split.
1n will copy the result of first part is (.{1,199}[,]) and insert n after this s/input_pattern/output_pattern/g will replace all such entries in file

I was in love with JAVA, now i love UNIX also.. 🙂


Sendkeys

A VB Script to run outlook and send some specified keys to it.

To send Ctrl+n, use “^{n}” as given below, for Alt use %

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "Outlook"
WScript.Sleep 2500 ' Give Notepad some time to load
WshShell.SendKeys "^{n}"
WScript.Sleep 1000
WshShell.SendKeys "Yogesh_Gandhi@infosys.com"
WshShell.SendKeys "{TAB}"
WshShell.SendKeys "{TAB}"
WshShell.SendKeys "{TAB}"
WshShell.SendKeys "{TAB}"
WshShell.SendKeys "{TAB}"
WshShell.SendKeys " Hi Yogesh, I accept that I am a FOOL ...and you made a fool out of me today ... I don’t think God planted brains in my head!!!!...otherwise I would have checked out the code first! you are great. hats off for you. With Regards"
WshShell.SendKeys "%{s}"


Quick batch file to check OS version…

How to check the Operation System version
if OS is 32 bit or 64 bit

Quick batch file to check OS version…


@echo off

Set RegQry=HKLMHardwareDescriptionSystemCentralProcessor

REG.exe Query %RegQry% > checkOS.txt

Find /i “x86” StringCheck.txt

If %ERRORLEVEL% == 0 (

Echo “This is 32 Bit Operating system”

) ELSE (

Echo “This is 64 Bit Operating System”

)

pause


use OnClick with caution

JSP Code :

<a href="abc.jsp" onclick='’>LOGOUT

The above statement gets converted to Servlet CODE :


out.write("<a href="abc.jsp" onclick='");

session.invalidate();

out.write("'>LOGOUT");


Conclusion:

What ever scriplet you write in onclick, will get executed every time the JSP is called. Not only on onclick as can be wrongly assumed.


BUG in struts2

Actually I used

Now, I have specified type as button, so I expected that it will not go to action on click on the button.

But what happens is

when I click on button, it asks if I want to close the window,
Now, even I click NO, it still goes to action specified in form.

Solution : This is a BUG in struts2.

the HTML tag that is generated is something like:

<button type="submit” id=”test_0″ value=”Submit” class=”button positive save”>Submit

Please note that we have given type=button in s:submit tag, and the HTML generated still contains type=submit.

Can get some information from http://www.ericmmartin.com/struts-2-bug-submit-button-tag-rendering/


Internationalization tips for JSP

In order to display the complex script characters like Japanese, Chinese, etc in the browser window, the following changes need to be made at the beginning of each JSP file:

<%@ page language="java" 
contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

Wherever input controls are present, the following additional steps need to be taken:
Add the following code before reading any parameters in the previous JSP file:

request.setCharacterEncoding("UTF-8"); // Set the request encoding

While sending output to a browser:

response.setContentType ("charset=UTF-8");


Color of Error Messages in struts 2

Problem:

I am working on struts 2.
I have one text field and have put validation (with xml)on the text field if its blank, throw an error.
But the field error I am getting is in black colour.
Does anyone have idea how to display the field error in red colour.

Solution:

U can the use the following tag in your .css Stylesheet file

.errorMessage {
color: red;
font-size: 0.8em;
}

And include this Stylesheet in the file where u have the text field.

Source : http://www.roseindia.net/struts/struts2/validations/Struts2-annotations.shtml