-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHash.java
More file actions
109 lines (75 loc) · 1.95 KB
/
Hash.java
File metadata and controls
109 lines (75 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package Hash;
public class Hash<K ,V extends Comparable<V>> implements IHash<K, V> {
public static final int INITIAL_SIZE = 15;
private HashNode<K, V>[] list;
private int size;
@SuppressWarnings("unchecked")
public Hash() {
this.size = 0;
list = new HashNode[INITIAL_SIZE];
}
@Override
public void put(K key, V value) {
int index = hashFuntion(key);
HashNode<K, V> nodeAdd = new HashNode<K, V>(key, value);
if(list[index]!= null) {
list[index].add(nodeAdd);
}else {
list[index] = nodeAdd;
size++;
}
}
@Override
public void remove(K key) throws HashIsEmptyException, NonexistentKeyException {
int index = hashFuntion(key);
if(isEmpty() == true) {
throw new HashIsEmptyException("");
}else {
if(list[index] == null) {
throw new NonexistentKeyException("");
}else {
if(list[index].getNext() == null) {
list[index] = null;
size--;
}else {
list[index].romoveLast();
}
}
}
}
public HashNode<K, V> getObjet(K key, V value) throws NonexistentKeyException{
int index = hashFuntion(key);
if(list[index]== null) {
throw new NonexistentKeyException("the object whit the key: "+ key + " non Exist" );
}else if(list[index].getNext() == null && list[index].getValue().compareTo(value) == 0) {
return list[index];
}else if(list[index].getNext() != null && list[index].getValue().compareTo(value) == 0) {
return list[index];
}
else {
return list[index].getObjet(value);
}
}
@Override
public V get(K key) {
int index = hashFuntion(key);
return list[index].getValue();
}
@Override
public int getSize() {
return size;
}
@Override
public boolean isEmpty() {
return size == 0;
}
public int hashFuntion(K key) {
int index = key.hashCode();
if(index > INITIAL_SIZE) {
index = key.hashCode()%INITIAL_SIZE;
}else if (index < 1 ) {
index = key.hashCode()*INITIAL_SIZE;
}
return index + 1;
}
}