You are on page 1of 4

package hashtable;

class hashNode{

String key;

int data;

class hashTable{

hashNode[] table;

int capacity = 5;

hashTable(){

table = new hashNode [capacity];

int hash(String key){

int i;

int hasil = 0;

for(i = 0; i < key.length(); i++) {

hasil = hasil + key.charAt(i);

hasil = hasil%capacity;

return hasil;

void put(String key, int data){


int idx = hash(key);

hashNode h = new hashNode();

h.data = data;

h.key = key;

table[idx] = h;

checkLoad();

void display(){

for (int i = 0; i < table.length; i++) {

if (table[i] != null) {

System.out.println(table[i].key + " " +table[i].data);

void checkLoad(){

int load = 0;

for (int i = 0; i < table.length; i++) {

if (table[i] != null) {

load++;

}
if (load == capacity) {

hashNode[] temp = new hashNode[capacity];

for (int i = 0; i < table.length; i++) {

temp[i] = table[i];

table = new hashNode[capacity*2];

capacity = capacity*2;

for (int i = 0; i < temp.length; i++) {

table[i] = temp[i];

public class HashTable {

public static void main(String[] args) {

hashTable h = new hashTable();

h.put("Judi", 100);

h.put("roma", 85);

h.put("ani", 75);

h.display();

}
}

You might also like