Creating a Hash Table in java
Hello, I am learning java programming language and while learning it, I want to create a Hash Table in java. I have tried it, but I am not having any knowledge about it, So, if anyone is having knowledge about it, then please provide me solution for it. If you are having program for getting solution on this, then please reply me with that program.
Re: Creating a Hash Table in java
Hello, if you want to create a hash table in java then you must need to make use of the different method below:
- clear()
- clone()
- contains(Object value)
- containsKey(Object key)
- containsValue(Object value)
- elements()
- entrySet()
- equals(Object o)
- get(Object key)
- hashCode()
- isEmpty()
- keys()
- keySet()
- put(Object key, Object value)
- putAll(Map t)
- rehash()
- remove(Object key)
- size()
- toString()
- values()
Re: Creating a Hash Table in java
Hello, for creating a hash table in java you must need to make use of the code below. Just create a class and add all of the above code in that class.
Code:
Map M = new HashMap();
M = new TreeMap();
M.put("x", new Integer(100));
M.put("y", new Integer(200));
M.put("z", new Integer(300));
int S = M.S();
Object obj = M.put("x", new Integer(9));
obj = M.remove("c");
Iterator iterator = M.keySet().iteratorerator();
while (iterator.hasNext()) {
Object key = iterator.next();
}
iterator = M.values().iteratorerator();
while (iterator.hasNext()) {
Object value = iterator.next();
}
Re: Creating a Hash Table in java
If you want to create a structure in java which contains the key value pair then you must need to make use of the Hashashtableable in java. For creating a hash table in java you must need to make use of the constructor as below:
Code:
HashTable hashtable = new HashTable();
After creating hash table you need to add the data in the hash table. So, for adding the data in the hash table you must need to make use of the code below:
Code:
hashtable.put("Number", new Integer(1000));
Integer num = (Integer)numbers.get("Number one");
if (num != null)
{
System.out.println("Number one = " + num);
}
Re: Creating a Hash Table in java
Hello, I have knowledge of creating a custom hash table. If you want to create a custom hash table then it is necessary to make use of the code below. So, just make use of it and get your problem solved:
Code:
import java.util.Hashtable;
public class CustomHashtable<H, T> extends Hashtable
{
@Override public synchronized T get(Object key) {
if(key == null) return new Object();
Object obj = super.get(key);
if(obj == null)
{
return new Object();
}
}
}
Re: Creating a Hash Table in java
If many entries are to be made into a Hashtable, creating it with a sufficiently large capacity may allow the entries to be inserted more efficiently than letting it perform automatic rehashing as needed to grow the table. This example creates a hashtable of Htable. It uses the names of the Htable as keys:
Code:
Hashtable Htable = new Hashtable();
Htable.put("Eleven", new Integer(11));
Htable.put("Tweleve", new Integer(12));
Htable.put("Thirteen", new Integer(13));
Now if you want to retrieve the number then it is necessary to make use of the code below:
Code:
Integer num = (Integer)Htable.get("twelve");
if (num != null)
{
System.out.println("twelve = " + num);
}