How Java Hashtable Works ?

         Hashtable is an implementation of a key-value pair data structure in java. You can store and retrieve a ‘value’ using a ‘key’ and it is an identifier of the value stored. It is obvious that the ‘key’ should be unique.

     java.util.Hashtable extends Dictionary and implements Map. Objects with non-null value can be used as a key or value. Key of the Hashtable must implement hashcode() and equals() methods. By the end of this article you will find out the reason behind this condition.
 

Generally a Hashtable in java is created using the empty constructor Hashtable().
Which is a poor decision and an often repeated mistake.

Hashtable has two other constructors.
 
and  
Initial capacity is number of buckets created at the time of Hashtable instantiation. Bucket is a logical space of storage for Hashtable.

Hashing and Hashtable

       Before seeing java’s Hashtable in detail you should understand hashing in general. Assume that, v is a value to be stored and k is the key used for storage / retrieval, then h is a hash function where v is stored at h(k) of table. To retrieve a value compute h(k) so that you can directly get the position of v. So in a key-value pair table, you need not sequentially scan through the keys to identify a value.
 
h(k) is the hashing function and it is used to find the location to store the corresponding value v. h(k) cannot compute to a indefinite space. Storage allocated for a Hashtable is limited within a program. So, the hasing function h(k) should return a number within that allocated spectrum (logical address space).

Hashing in Java

Java’s hashing uses uses hashCode() method from the key and value objects to compute. Following is the core code from Hashtable where the hashCode ‘h’ is computed. You can see that both key’s and value’s hashCode() method is called.
 
It is better to have your hashCode() method in your custom objects. String has its own hashCode methode and it computes the hashcode value as below:  
 
If you don’t have a hashCode() method, then it is derived from Object class.
Following is javadoc comment of hashCode() method from Object class: 
  • Returns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable.  
If you are going to write a custom hashCode(), then follow the following contract:
  • The general contract of hashCode is: Whenever it is invoked on the same object more than once during  an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified.
The following is to improve performance of the Hashtable.
  • If two objects are equal according to the equals(Object)method, then calling the hashCode method on each of the two objects must produce the same integer result

hashCode() guarantees distinct integers by using the internal address of the object.

Collision in Hashtable

 

posted under | 0 Comments

How to install Ant on Windows System

How to install Ant on windows System

1. Download ANT http://ant.apache.org/
2. Unzip it and rename it to ant

3. Set environmental variables JAVA_HOME to your Java environment, ANT_HOME to the directory you uncompressed, to do it on command prompt type this (Assume Ant is installed in E:\ant\.)

set ANT_HOME=E:\ant
set JAVA_HOME=D:\Program Files\Java\jdk1.6.0_03
set PATH=%PATH%;%ANT_HOME%\bin


In windows 7 you can go to Control Panel\All Control Panel Items\System or right click on Computer and then to “Advance system setting”


-Choose Advanced Tab
-Press Environtmen Variables Button
-In the System Variables, click New Button 


Give the Variable Name:ANT_HOME
Give the Value: E:\ant
Click OK

Then,we’ll add new ANT_HOME path,
And Click again on New Button if you do not have ‘path’ Variable in there, if so select it and edit as

Give the Variable Name:path
Give the Value D:\Program Files\Java\jdk1.6.0_03\bin;%ANT_HOME%\bin
Click OK

4. Check wheter ANT works correctly or not.
In the command prompt, type:
ant -version

I got Apache Ant version 1.9.2 compiled on October 17 2013
You may be get different version based on the version you have downloaded from the apache. In my case it will be 1.9.2 .

posted under | 0 Comments

How To Set Up Master Slave Replication in MySQL

How To Set Up Master Slave Replication in MySQL

1. Create new user for the purpose of replication which will be used by client to connect master.

mysql > CREATE USER 'repl'@'%.mydomain.com' IDENTIFIED BY 'slavepass';
 
Grant replication privileges to newly created user

mysql >  GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%.mydomain.com';
2. Configure Master Server. On master server open my.ini file available in MySql program files folder.
        Add following variables to that file. (For this you may need administrator      privileges)

Variable name
Value
Comment
tmpdir
Temp folder path
Value should be placed in double quotes
server_id
1
Value should be unique among the master and client db server.
log-bin
Log file path
File should have extension “.log”. Place in double quotes
log-error
Error log file path
File should have extension “.log”. Place in double quotes
log-bin-index
Index file path
File should have extension “.index”. Place in double quotes

3. Configure Slave (client) server.On client server open my.ini file available in Mysql program files folder.

Add following variables to that file. (For this you may need administrator privileges)
 

Variable name

Value

Comment

server_id

2

Value should be unique among the master and client db server.

master-host

IP address of master db sever

 

master-port

Port no of master db server.

 

master-user

Username of master db server

 

master-password

Password of master db server

 

log-bin

Log file path

Same as in master.

log-bin-index

Index file path

Same as in master.

log-error

Error log file path

Same as in master.

relay-log

Relay log file path

Same as in master.

relay-log-info-file

Info file path

Same as in master.

relay-log-index

Index file path

Same as in master.
 

4. Export / Import existing MySql database.
This step is not required if you are setting up replication master-client for new database.

Restart master and client server services respectively.
Create a database dump from master server of your choice database.
On client MySql server, enter command
mysql > STOP SLAVE
 
Restore same dump in the client server where you want to replicate existing database.
Note: For the above process, stop all the writable queries on the master server so that no further updates are possible. This is to ensure that both client and master have same database with same table structure and with same data.
If database is particularly very large then just create a zip file of the database you want from data folder in MySql server and place it in same data folder of client server and unzip it.

 
5. Start Replication
  • On the master database server restart the MySQL service.
  • On the slave database server restart the MySQL service.
  • On the client MySQL server, enter command :
  • mysql > START SLAVE
  • On the slave database server issue the following  command from the MySQL command line client or regular command Window :
You should get :
    Slave_IO_Running  : Yes
    Slave_SQL_Running : Yes

If this is the result in above window, your replication is succsessfull. Enjoy it..

posted under | 2 Comments

How to resize an array in java

 
In General terms, As we know that we cannot resize an array once we initialized because array defination says that an array is an fixed size,sequence and collection of similar data type. But It is possible to resize an array, resizing an array is nothing but copy the content of old array to new index array as per our requirement. Following code snippets is usefull for resize an array. Java.lang.System.arraycopy() Method is responsible for resizing an array.
File Name : ResizeArray.java
 
What is Java.lang.System.arraycopy() Method :
The java.lang.System.arraycopy() method copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array. A subsequence of array components are copied from the source array referenced by src to the destination array referenced by dest.The number of components copied is equal to the length argument.
The components at positions srcPos through srcPos + length - 1 in the source array are copied into positions destPos through destPos + length - 1, respectively, of the destination array.
Syntax :
 
Parameters:
  • src       -- This is the source array.
  • srcPos -- This is the starting position in the source array.
  • dest -- This is the destination array.
  • destPos -- This is the starting position in the destination data.
  • length -- This is the number of array elements to be copied.
Return Value: Method does not return any value.
Exception Details:
  • IndexOutOfBoundsException  --if copying would cause access of data outside array bounds.
  • ArrayStoreException -- if an element in the src array could not be stored into the dest array because of a type mismatch.
  • NullPointerException -- if either src or dest is null.
Another Example : File Name :TestSystemDemo.java
output : array2 = 0 10 20 30 40

posted under | 0 Comments

How to read XML file in java

How To Read/Parse XML File In Java

Here you will find the program which read/parse xml file. It will provide you a basic guide to parse your XML document, Your codding may be different as per your requirement. Here you will find mathod called getAllStudentNames(String fileName). Please provide fileName as full path of your XML file source.
File Name : XMLParser.java
 
File Name : student.xml
 

posted under | 0 Comments

How to take screen shots in Java

How to take screen shots in Java

Following piece of code in Java that takes the screen shot of your desktop and save it in a PNG file. This example uses java.awt.Robot class to capture the screen pixels and returns a BufferedImage. Java.awt.Robot class is used to take the control of mouse and keyboard. Once you get the control, you can do any type of operation related to mouse and keyboard through your java code. This class is used generally for test automation. Copy and paste following code in your Java class and invoke the method captureScreen() with file name as argument. The screen shot will be stored in the file that you specified in argument. Please enter the full file path.
 

posted under | 0 Comments

Manipulate ISO Dates in Javascript

Following code helps you to find total number of weeks in javascript as well as getISOYear, getISOWeek.

Manipulate ISO Dates in Javascript

posted under , | 0 Comments

Date Manipulation In Java

 
This Class will become very usefull when you are dealing with Dates in Java. This class provides method which you can call based on your needs.

File Name : DateManipulation.java

 

posted under , | 0 Comments
Newer Posts Older Posts Home

Followers

Powered by Blogger.

Populares

Custom Search