IT Community - Software Programming, Web Development and Technical Support

Overview of Hash table.

This is a discussion on Overview of Hash table. within the Other Web Programming Languages forums, part of the Web Development category; Overview of Hash table. Hashtable are very sophisticated data structures that allows accessing an element based on some key, which ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > Other Web Programming Languages

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 07-18-2007, 12:03 AM
H2o H2o is offline
D-Web Analyst
 
Join Date: Jul 2007
Posts: 246
H2o is on a distinguished road
Default Overview of Hash table.

Overview of Hash table.

Hashtable are very sophisticated data structures that allows accessing an element based on some key, which can be of any type.

1. Is member of System.Collections namespace.

2. Hashtable _table = new Hashtable() ;
Hashtable _table2 = new Hashtable(10); // capacity or size.
Hashtable _table3 = new Hashtable(10 ,06); //capacity , loadfactor

Loadfactor this factor helps for performance, The smaller the load size the more efficiently hash table works and the more memory it occupy.

1. Create a Hashtable
Hashtable _simpleTable = new Hashtable();

2. Added some data in it.

_simpleTable.Add("Key1", "Value1");
_simpleTable.Add("Key2", "Value2");

3. Get the size.
_simpleTable.Count; // an int value.

4. Fetching data

a. create an object of IDictonaryEnumerator , this is an interface that creates an enumerator and customized for Dictonary objects.

IDictionaryEnumerator _enumerator = _simpleTable.GetEnumerator();

b. while (_enumerator.MoveNext())
{
_string += _enumerator.Key + " ";
_string += _enumerator.Value + "\n";

}

c. and that all you fetched the Data !!!

d. clear hash table..
_simpleTable.Clear();











e. Search for a specific key : remember its value type when used

if (_simpleTable.ContainsKey("Key1"))
{
Console.WriteLine("Key1 is present");
}

f. Search for a specific key : remember its value type when used

if (_simpleTable.ContainsValue("Value1"))
{
Console.WriteLine("Value1 is present");
}
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 09-14-2007, 06:28 AM
leoraja8 leoraja8 is offline
D-Web Sr.Programmer
 
Join Date: May 2007
Posts: 194
leoraja8 is on a distinguished road
Default Re: Overview of Hash table.

What is a hash table?

A hash-table is a data-structure that consists of an array of linked lists.

The beauty of this arrangement is that you can have the speed of array-access,
along with the convenience of inserting objects into linked lists. What's more, inserting
and searching through a hash-table is very, very fast.

How does it work?

Hashing describes the process of taking some key value, and generating an array
index from it. Usually, this array index has seemingly little in common with
the key. Values put into a hash-table will be placed all over the array, in
no particular order.

But the trick is that for any key, there is only one hash value that is always
generated.

As an example, suppose you have an integer array of 10 locations. You want to insert a certain
value, such as 134. 134 is the key. You need to generate a hash value from 0 to 9, One way to do this is:
int hash = 134 % 10.

This guarantees a value from 0 to 9, which can be our array index.

But what if we then wanted to add the number 124 to the same array/hashtable?

134 % 10 = 4
124 % 10 = 4

The hash value is the same. This is where the linked list comes in.

Instead of having an array of mere values, the array contains objects with links
to other objects, as well as the actual values you want to store.

for example:

class MyObject extends Object {
int value;
MyObject next;
};

Now, you can have more than one value in a single array position. Because of this,
the array positions are sometimes referred to as buckets.

PHP Code:
*/
import java.io.*;

class 
HashTable {
HashTable(int size) {
numBuckets=size;
numObjects=0;
vectors=new Object[size];
InitVectorTable();
}

public 
Object AddObject(Object obj)
{
int bucket=hash(obj.GetKey());
obj.next=vectors[bucket];
vectors[bucket]=obj;
numObjects++;
return 
vectors[bucket];
}

public 
Object GetObject(String key)
{
int bucket=hash(ssn);

Object p=vectors[bucket];
if (
p==null)
return 
null;

while (
p!=null) {
if (
p.GetSSN().compareTo(ssn)==0)
return 
p;
p=p.GetNext();
}
return 
null;
}


private 
int hash(String str) {
int len=str.length();
long hash=0;
char[] buffer=str.toCharArray();

int c=0;
for (
int i=0;i
c
=buffer[i]+140;//140 is arbitrary
hash = ((hash<<3) + (hash>>28) + c);

}
return (int)(
hash%numBuckets);
}

public 
void DumpToFile(BufferedWriter bufWriterthrows IOException{
Object p;

for (
int i=0;numBucketsi++){
p=vectors[i];
while (
p!=null) {
bufWriter.write(p.GetKey() + ", "+p.GetSomeDataField());
p=p.GetNext();
}
}
}

private 
void InitVectorTable() {
for (
int i=0;numBucketsi++)
vectors[i]=null;
}


Object[] vectors;
int numBuckets;
int numObjects;


Last edited by leoraja8 : 09-14-2007 at 06:37 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP.NET Cookies Overview KiruthikaSambandam ASP and ASP.NET Programming 8 05-30-2008 01:10 AM
How do I sort a hash by the hash value? S.Vinothkumar Perl 2 11-26-2007 05:56 AM
Overview of Software Testing Certifications cool7575 Software Testing 0 09-22-2007 12:16 PM
What’s the difference between Hash Map and Hash Table? mobilegeek Java Programming 2 09-14-2007 05:37 AM
What is a hash? vadivelanvaidyanathan Perl 1 07-30-2007 06:29 AM


All times are GMT -7. The time now is 10:32 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0