Skip to content

Commit 4d0d02d

Browse files
authored
Merge pull request TheAlgorithms#288 from freitzzz/master
Fixed files and folders name conventions
2 parents 962720f + c850d79 commit 4d0d02d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+717
-523
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.

Data Structures/HashMap/HashMap.java

Lines changed: 283 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,283 @@
1+
<<<<<<< HEAD:Data Structures/HashMap/HashMap.java
2+
3+
4+
import java.util.ArrayList;
5+
import java.util.LinkedList;
6+
7+
public class HashMap<K,V> {
8+
public class hmnodes{ //HashMap nodes
9+
K key;
10+
V value;
11+
}
12+
13+
private int size=0; //size of hashmap
14+
private LinkedList<hmnodes> buckets[]; //array of addresses of list
15+
16+
public HashMap(){
17+
buckets=new LinkedList[4]; //initially create bucket of any size
18+
for(int i=0;i<4;i++)
19+
buckets[i]=new LinkedList<>();
20+
}
21+
22+
public void put(K key,V value) throws Exception{
23+
int bi=bucketIndex(key); //find the index,the new key will be inserted in linklist at that index
24+
int fountAt=find(bi,key); //check if key already exists or not
25+
if(fountAt==-1){
26+
hmnodes temp=new hmnodes(); //if doesn't exist create new node and insert
27+
temp.key=key;
28+
temp.value=value;
29+
buckets[bi].addLast(temp);
30+
this.size++;
31+
}else{
32+
buckets[bi].get(fountAt).value=value;//if already exist modify the value
33+
}
34+
35+
double lambda = (this.size*1.0)/this.buckets.length;
36+
if(lambda>2.0){
37+
rehash(); //rehashing function which will increase the size of bucket as soon as lambda exceeds 2.0
38+
}
39+
40+
return;
41+
}
42+
43+
44+
public V get(K key) throws Exception{
45+
int bi=bucketIndex(key);
46+
int fountAt=find(bi,key);
47+
if(fountAt==-1){
48+
return null;
49+
}else{
50+
return buckets[bi].get(fountAt).value;
51+
}
52+
}
53+
54+
public V remove(K key) throws Exception{
55+
int bi=bucketIndex(key);
56+
int fountAt=find(bi,key);
57+
if(fountAt==-1){
58+
return null;
59+
}else{
60+
this.size--;
61+
return buckets[bi].remove(fountAt).value;
62+
}
63+
}
64+
65+
public boolean containskey(K key) throws Exception{
66+
int bi=bucketIndex(key);
67+
int fountAt=find(bi,key);
68+
if(fountAt==-1){
69+
return false;
70+
}else{
71+
return true;
72+
}
73+
}
74+
75+
public int size(){
76+
return this.size;
77+
}
78+
79+
80+
public boolean isempty(){
81+
return this.size==0;
82+
}
83+
84+
public ArrayList<K> keyset() throws Exception{
85+
ArrayList<K> arr=new ArrayList<>();
86+
for(int i=0;i<buckets.length;i++){
87+
for(int j=0;j<buckets[i].size();j++){
88+
arr.add(buckets[i].get(j).key);
89+
}
90+
}
91+
return arr;
92+
}
93+
94+
public ArrayList<V> valueset() throws Exception{
95+
ArrayList<V> arr=new ArrayList<>();
96+
for(int i=0;i<buckets.length;i++){
97+
for(int j=0;j<buckets[i].size();j++){
98+
arr.add(buckets[i].get(j).value);
99+
}
100+
}
101+
return arr;
102+
}
103+
104+
public void display() throws Exception{
105+
for(int i=0;i<buckets.length;i++){
106+
System.out.print("Bucket: "+i+" ");
107+
for(int j=0;j<buckets[i].size();j++){
108+
hmnodes temp=buckets[i].get(j);
109+
System.out.print("["+temp.key+"->"+temp.value+"]");
110+
}
111+
System.out.println();
112+
}
113+
}
114+
115+
public int find(int bi,K key) throws Exception{
116+
for(int i=0;i<buckets[bi].size();i++){
117+
if(key.equals(buckets[bi].get(i).key))
118+
return i;
119+
}
120+
return -1;
121+
}
122+
123+
public int bucketIndex(K key) throws Exception{
124+
int bi=key.hashCode();
125+
return Math.abs(bi%buckets.length);
126+
}
127+
128+
private void rehash() throws Exception{
129+
LinkedList<hmnodes> ob[]= buckets;
130+
buckets=new LinkedList[ob.length*2];
131+
for(int i=0;i<ob.length*2;i++)
132+
buckets[i]=new LinkedList<>();
133+
134+
size = 0;
135+
for(int i=0;i<ob.length;i++){
136+
for(int j=0;j<ob[i].size();j++){
137+
put(ob[i].get(j).key,ob[i].get(j).value);
138+
}
139+
}
140+
141+
}
142+
}
143+
=======
144+
import java.util.ArrayList;
145+
import java.util.LinkedList;
146+
147+
public class HashMap<K,V> {
148+
public class hmnodes{ //HashMap nodes
149+
K key;
150+
V value;
151+
}
152+
153+
private int size=0; //size of hashmap
154+
private LinkedList<hmnodes> buckets[]; //array of addresses of list
155+
156+
public HashMap(){
157+
buckets=new LinkedList[4]; //initially create bucket of any size
158+
for(int i=0;i<4;i++)
159+
buckets[i]=new LinkedList<>();
160+
}
161+
162+
public void put(K key,V value) throws Exception{
163+
int bi=bucketIndex(key); //find the index,the new key will be inserted in linklist at that index
164+
int fountAt=find(bi,key); //check if key already exists or not
165+
if(fountAt==-1){
166+
hmnodes temp=new hmnodes(); //if doesn't exist create new node and insert
167+
temp.key=key;
168+
temp.value=value;
169+
buckets[bi].addLast(temp);
170+
this.size++;
171+
}else{
172+
buckets[bi].get(fountAt).value=value;//if already exist modify the value
173+
}
174+
175+
double lambda = (this.size*1.0)/this.buckets.length;
176+
if(lambda>2.0){
177+
rehash(); //rehashing function which will increase the size of bucket as soon as lambda exceeds 2.0
178+
}
179+
180+
return;
181+
}
182+
183+
184+
public V get(K key) throws Exception{
185+
int bi=bucketIndex(key);
186+
int fountAt=find(bi,key);
187+
if(fountAt==-1){
188+
return null;
189+
}else{
190+
return buckets[bi].get(fountAt).value;
191+
}
192+
}
193+
194+
public V remove(K key) throws Exception{
195+
int bi=bucketIndex(key);
196+
int fountAt=find(bi,key);
197+
if(fountAt==-1){
198+
return null;
199+
}else{
200+
this.size--;
201+
return buckets[bi].remove(fountAt).value;
202+
}
203+
}
204+
205+
public boolean containskey(K key) throws Exception{
206+
int bi=bucketIndex(key);
207+
int fountAt=find(bi,key);
208+
if(fountAt==-1){
209+
return false;
210+
}else{
211+
return true;
212+
}
213+
}
214+
215+
public int size(){
216+
return this.size;
217+
}
218+
219+
220+
public boolean isempty(){
221+
return this.size==0;
222+
}
223+
224+
public ArrayList<K> keyset() throws Exception{
225+
ArrayList<K> arr=new ArrayList<>();
226+
for(int i=0;i<buckets.length;i++){
227+
for(int j=0;j<buckets[i].size();j++){
228+
arr.add(buckets[i].get(j).key);
229+
}
230+
}
231+
return arr;
232+
}
233+
234+
public ArrayList<V> valueset() throws Exception{
235+
ArrayList<V> arr=new ArrayList<>();
236+
for(int i=0;i<buckets.length;i++){
237+
for(int j=0;j<buckets[i].size();j++){
238+
arr.add(buckets[i].get(j).value);
239+
}
240+
}
241+
return arr;
242+
}
243+
244+
public void display() throws Exception{
245+
for(int i=0;i<buckets.length;i++){
246+
System.out.print("Bucket: "+i+" ");
247+
for(int j=0;j<buckets[i].size();j++){
248+
hmnodes temp=buckets[i].get(j);
249+
System.out.print("["+temp.key+"->"+temp.value+"]");
250+
}
251+
System.out.println();
252+
}
253+
}
254+
255+
public int find(int bi,K key) throws Exception{
256+
for(int i=0;i<buckets[bi].size();i++){
257+
if(key.equals(buckets[bi].get(i).key))
258+
return i;
259+
}
260+
return -1;
261+
}
262+
263+
public int bucketIndex(K key) throws Exception{
264+
int bi=key.hashCode();
265+
return Math.abs(bi%buckets.length);
266+
}
267+
268+
private void rehash() throws Exception{
269+
LinkedList<hmnodes> ob[]= buckets;
270+
buckets=new LinkedList[ob.length*2];
271+
for(int i=0;i<ob.length*2;i++)
272+
buckets[i]=new LinkedList<>();
273+
274+
size = 0;
275+
for(int i=0;i<ob.length;i++){
276+
for(int j=0;j<ob[i].size();j++){
277+
put(ob[i].get(j).key,ob[i].get(j).value);
278+
}
279+
}
280+
281+
}
282+
}
283+
>>>>>>> 7e3a8c55c865471a33f6932a022a1059c5243fc3:data_structures/HashMap/HashMap.java
File renamed without changes.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy