8000 Hashmap · Ismail0290/DSA-Bootcamp-Java@380f8b0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 380f8b0

Browse files
Hashmap
1 parent 93ce6c8 commit 380f8b0

File tree

3 files changed

+207
-0
lines changed

3 files changed

+207
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import java.util.*;
2+
3+
public class HashMapFinal<K, V> {
4+
ArrayList<LinkedList<Entity>> list;
5+
6+
private int size = 0;
7+
8+
private float lf = 0.5f;
9+
10+
public HashMapFinal() {
11+
list = new ArrayList<>();
12+
for(int i=0; i < 10; i++) {
13+
list.add(new LinkedList<>());
14+
}
15+
}
16+
17+
public void put(K key, V value) {
18+
int hash = Math.abs(key.hashCode() % list.size());
19+
20+
LinkedList<Entity> entities = list.get(hash);
21+
22+
for (Entity entity : entities) {
23+
if(entity.key.equals(key)) {
24+
entity.value = value;
25+
return;
26+
}
27+
}
28+
29+
if((float)(size) / list.size() > lf) {
30+
reHash();
31+
}
32+
33+
entities.add(new Entity(key, value));
34+
35+
size++;
36+
}
37+
38+
private void reHash() {
39+
System.out.println("We are now rehashing!");
40+
41+
ArrayList<LinkedList<Entity>> old = list;
42+
list = new ArrayList<>();
43+
44+
size = 0;
45+
46+
for(int i=0; i<old.size() * 2; i++) {
47+
list.add(new LinkedList<>());
48+
}
49+
50+
for(LinkedList<Entity> entries :old) {
51+
for(Entity entry : entries) {
52+
put(entry.key, entry.value);
53+
}
54+
}
55+
}
56+
57+
public V get(K key) {
58+
int hash = Math.abs(key.hashCode() % list.size());
59+
LinkedList<Entity> entities = list.get(hash);
60+
for(Entity entity : entities) {
61+
if(entity.key.equals(key)) {
62+
return entity.value;
63+
}
64+
}
65+
return null;
66+
}
67+
68+
public void remove(K key) {
69+
int hash = Math.abs(key.hashCode() % list.size());
70+
LinkedList<Entity> entities = list.get(hash);
71+
72+
Entity target = null;
73+
74+
for(Entity entity : entities) {
75+
if(entity.key.equals(key)) {
76+
target = entity;
77+
break;
78+
}
79+
}
80+
81+
entities.remove(target);
82+
size--;
83+
}
84+
85+
public boolean containsKey(K key) {
86+
return get(key) != null;
87+
}
88+
89+
@Override
90+
public String toString() {
91+
StringBuilder builder = new StringBuilder();
92+
builder.append("{");
93+
for(LinkedList<Entity> entities : list) {
94+
for(Entity entity : entities) {
95+
builder.append(entity.key);
96+
builder.append(" = ");
97+
builder.append(entity.value);
98+
builder.append(" , ");
99+
}
100+
}
101+
builder.append("}");
102+
103+
return builder.toString();
104+
}
105+
106+
private class Entity {
107+
K key;
108+
V value;
109+
110+
public Entity(K key, V value) {
111+
this.key = key;
112+
this.value = value;
113+
}
114+
}
115+
116+
117+
}
118+
119+

lectures/25-hashmaps/code/Main.java

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import java.util.*;
2+
3+
class MapUsingHash {
4+
5+
private Entity[] entities;
6+
7+
public MapUsingHash() {
8+
entities = new Entity[100];
9+
}
10+
11+
public void put(String key, String value) {
12+
int hash = Math.abs(key.hashCode() % entities.length);
13+
entities[hash] = new Entity(key, value); // overriding
14+
}
15+
16+
public String get(String key) {
17+
int hash = Math.abs(key.hashCode() % entities.length);
18+
if(entities[hash] != null && entities[hash].key.equals(key)) {
19+
return entities[hash].value;
20+
}
21+
return null;
22+
}
23+
24+
public void remove(String key) {
25+
int hash = Math.abs(key.hashCode() % entities.length);
26+
if(entities[hash] != null && entities[hash].key.equals(key)) {
27+
entities[hash] = null;
28+
}
29+
}
30+
31+
private class Entity {
32+
String key;
33+
String value;
34+
35+
public Entity(String key, String value) {
36+
this.key = key;
37+
this.value = value;
38+
}
39+
}
40+
}
41+
42+
43+
44+
class Main {
45+
public static void main(String[] args) {
46+
// hashDemo();
47+
HashMapFinal<String, String> map = new HashMapFinal<>();
48+
49+
map.put("Mango", "King of fruits");
50+
map.put("Apple", "A sweet red fruit");
51+
map.put("Litchi", "Kunal's fav fruit");
52+
53+
System.out.println(map);
54+
55+
56+
}
57+
58+
public static void hashDemo() {
59+
// String name = "Rahul";
60+
61+
// Integer a = 4235678;
62+
63+
// int code = a.hashCode();
64+
65+
// System.out.println(code);
66+
67+
HashMap<String, Integer> map = new HashMap<>();
68+
69+
map.put("Kunal", 89);
70+
map.put("Karan", 99);
71+
map.put("Rahul", 94);
72+
73+
// System.out.println(map.get("Karan"));
74+
// System.out.println(map.getOrDefault("Apoorv", 78));
75+
System.out.println(map.containsKey("Karan"));
76+
77+
HashSet<Integer> set = new HashSet<>();
78+
set.add(56);
79+
set.add(9);
80+
set.add(12);
81+
set.add(43);
82+
set.add(56);
83+
set.add(2);
84+
85+
System.out.println(set);
86+
}
87+
88+
}
2.3 MB
Binary file not shown.

0 commit comments

Comments
 (0)
0