SlideShare a Scribd company logo
1 of 6
Download to read offline
public class LinkedHashEntry {
private int key;
private int value;
private LinkedHashEntry next;
LinkedHashEntry(int key, int value) {
this.key = key;
this.value = value;
this.next = null;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public int getKey() {
return key;
}
public LinkedHashEntry getNext() {
return next;
}
public void setNext(LinkedHashEntry next) {
this.next = next;
}
}
public class HashMap {
private final static int TABLE_SIZE = 128;
LinkedHashEntry[] table;
HashMap() {
table = new LinkedHashEntry[TABLE_SIZE];
for (int i = 0; i < TABLE_SIZE; i++)
table[i] = null;
}
public int get(int key) {
int hash = (key % TABLE_SIZE);
if (table[hash] == null)
return -1;
else {
LinkedHashEntry entry = table[hash];
while (entry != null && entry.getKey() != key)
entry = entry.getNext();
if (entry == null)
return -1;
else
return entry.getValue();
}
}
public void put(int key, int value) {
int hash = (key % TABLE_SIZE);
if (table[hash] == null)
table[hash] = new LinkedHashEntry(key, value);
else {
LinkedHashEntry entry = table[hash];
while (entry.getNext() != null && entry.getKey() != key)
entry = entry.getNext();
if (entry.getKey() == key)
entry.setValue(value);
else
entry.setNext(new LinkedHashEntry(key, value));
}
}
public void remove(int key) {
int hash = (key % TABLE_SIZE);
if (table[hash] != null) {
LinkedHashEntry prevEntry = null;
LinkedHashEntry entry = table[hash];
while (entry.getNext() != null && entry.getKey() != key) {
prevEntry = entry;
entry = entry.getNext();
}
if (entry.getKey() == key) {
if (prevEntry == null)
table[hash] = entry.getNext();
else
prevEntry.setNext(entry.getNext());
}
}
}
}
Solution
public class LinkedHashEntry {
private int key;
private int value;
private LinkedHashEntry next;
LinkedHashEntry(int key, int value) {
this.key = key;
this.value = value;
this.next = null;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public int getKey() {
return key;
}
public LinkedHashEntry getNext() {
return next;
}
public void setNext(LinkedHashEntry next) {
this.next = next;
}
}
public class HashMap {
private final static int TABLE_SIZE = 128;
LinkedHashEntry[] table;
HashMap() {
table = new LinkedHashEntry[TABLE_SIZE];
for (int i = 0; i < TABLE_SIZE; i++)
table[i] = null;
}
public int get(int key) {
int hash = (key % TABLE_SIZE);
if (table[hash] == null)
return -1;
else {
LinkedHashEntry entry = table[hash];
while (entry != null && entry.getKey() != key)
entry = entry.getNext();
if (entry == null)
return -1;
else
return entry.getValue();
}
}
public void put(int key, int value) {
int hash = (key % TABLE_SIZE);
if (table[hash] == null)
table[hash] = new LinkedHashEntry(key, value);
else {
LinkedHashEntry entry = table[hash];
while (entry.getNext() != null && entry.getKey() != key)
entry = entry.getNext();
if (entry.getKey() == key)
entry.setValue(value);
else
entry.setNext(new LinkedHashEntry(key, value));
}
}
public void remove(int key) {
int hash = (key % TABLE_SIZE);
if (table[hash] != null) {
LinkedHashEntry prevEntry = null;
LinkedHashEntry entry = table[hash];
while (entry.getNext() != null && entry.getKey() != key) {
prevEntry = entry;
entry = entry.getNext();
}
if (entry.getKey() == key) {
if (prevEntry == null)
table[hash] = entry.getNext();
else
prevEntry.setNext(entry.getNext());
}
}
}
}

More Related Content

Similar to public class LinkedHashEntry { private int key; private int valu.pdf

How do you update an address according to this code- Currently- I fig.pdf
How do you update an address according to this code-  Currently- I fig.pdfHow do you update an address according to this code-  Currently- I fig.pdf
How do you update an address according to this code- Currently- I fig.pdf
ThomasXUMParsonsx
 
Given below is the completed implementation of MyLinkedList class. O.pdf
Given below is the completed implementation of MyLinkedList class. O.pdfGiven below is the completed implementation of MyLinkedList class. O.pdf
Given below is the completed implementation of MyLinkedList class. O.pdf
info430661
 
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdfHere is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
arrowmobile
 
How do I fix this error - Exception in thread -main- java-lang-NullPoi.pdf
How do I fix this error - Exception in thread -main- java-lang-NullPoi.pdfHow do I fix this error - Exception in thread -main- java-lang-NullPoi.pdf
How do I fix this error - Exception in thread -main- java-lang-NullPoi.pdf
pnaran46
 
can you add a delete button and a add button to the below program. j.pdf
can you add a delete button and a add button to the below program. j.pdfcan you add a delete button and a add button to the below program. j.pdf
can you add a delete button and a add button to the below program. j.pdf
sales88
 

Similar to public class LinkedHashEntry { private int key; private int valu.pdf (7)

This is to test a balanced tree. I need help testing an unbalanced t.pdf
This is to test a balanced tree. I need help testing an unbalanced t.pdfThis is to test a balanced tree. I need help testing an unbalanced t.pdf
This is to test a balanced tree. I need help testing an unbalanced t.pdf
 
i am looking for help on the method AddSorted and the method Copy only.pdf
i am looking for help on the method AddSorted and the method Copy only.pdfi am looking for help on the method AddSorted and the method Copy only.pdf
i am looking for help on the method AddSorted and the method Copy only.pdf
 
How do you update an address according to this code- Currently- I fig.pdf
How do you update an address according to this code-  Currently- I fig.pdfHow do you update an address according to this code-  Currently- I fig.pdf
How do you update an address according to this code- Currently- I fig.pdf
 
Given below is the completed implementation of MyLinkedList class. O.pdf
Given below is the completed implementation of MyLinkedList class. O.pdfGiven below is the completed implementation of MyLinkedList class. O.pdf
Given below is the completed implementation of MyLinkedList class. O.pdf
 
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdfHere is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
 
How do I fix this error - Exception in thread -main- java-lang-NullPoi.pdf
How do I fix this error - Exception in thread -main- java-lang-NullPoi.pdfHow do I fix this error - Exception in thread -main- java-lang-NullPoi.pdf
How do I fix this error - Exception in thread -main- java-lang-NullPoi.pdf
 
can you add a delete button and a add button to the below program. j.pdf
can you add a delete button and a add button to the below program. j.pdfcan you add a delete button and a add button to the below program. j.pdf
can you add a delete button and a add button to the below program. j.pdf
 

More from anjanaarts2014

A robust whistleblowing regime is now an integral part of governance.pdf
A robust whistleblowing regime is now an integral part of governance.pdfA robust whistleblowing regime is now an integral part of governance.pdf
A robust whistleblowing regime is now an integral part of governance.pdf
anjanaarts2014
 
1. In higher parts of the reef, where the wave energy is high, the c.pdf
1. In higher parts of the reef, where the wave energy is high, the c.pdf1. In higher parts of the reef, where the wave energy is high, the c.pdf
1. In higher parts of the reef, where the wave energy is high, the c.pdf
anjanaarts2014
 
1)The viruse which is the most dangerous threat.In DTI survey,72 of.pdf
1)The viruse which is the most dangerous threat.In DTI survey,72 of.pdf1)The viruse which is the most dangerous threat.In DTI survey,72 of.pdf
1)The viruse which is the most dangerous threat.In DTI survey,72 of.pdf
anjanaarts2014
 
HClO3 + NaOH NaClO3 +H2O n(HClO3) = 0.10.04 = 4.pdf
                     HClO3 + NaOH  NaClO3 +H2O n(HClO3) = 0.10.04 = 4.pdf                     HClO3 + NaOH  NaClO3 +H2O n(HClO3) = 0.10.04 = 4.pdf
HClO3 + NaOH NaClO3 +H2O n(HClO3) = 0.10.04 = 4.pdf
anjanaarts2014
 
NaCl Its the coulombic interaction between Na.pdf
                     NaCl  Its the coulombic interaction between Na.pdf                     NaCl  Its the coulombic interaction between Na.pdf
NaCl Its the coulombic interaction between Na.pdf
anjanaarts2014
 
Here Jupiter is the fifth planet from the Sun a.pdf
                     Here  Jupiter is the fifth planet from the Sun a.pdf                     Here  Jupiter is the fifth planet from the Sun a.pdf
Here Jupiter is the fifth planet from the Sun a.pdf
anjanaarts2014
 
a.Times Interest Earned Ratio = EBIT interest ExpenseFor 2014.pdf
a.Times Interest Earned Ratio = EBIT  interest ExpenseFor 2014.pdfa.Times Interest Earned Ratio = EBIT  interest ExpenseFor 2014.pdf
a.Times Interest Earned Ratio = EBIT interest ExpenseFor 2014.pdf
anjanaarts2014
 
the bacteria isolated from the dead mouse is smooth (S) S. pneumonia.pdf
the bacteria isolated from the dead mouse is smooth (S) S. pneumonia.pdfthe bacteria isolated from the dead mouse is smooth (S) S. pneumonia.pdf
the bacteria isolated from the dead mouse is smooth (S) S. pneumonia.pdf
anjanaarts2014
 

More from anjanaarts2014 (20)

A robust whistleblowing regime is now an integral part of governance.pdf
A robust whistleblowing regime is now an integral part of governance.pdfA robust whistleblowing regime is now an integral part of governance.pdf
A robust whistleblowing regime is now an integral part of governance.pdf
 
1. In higher parts of the reef, where the wave energy is high, the c.pdf
1. In higher parts of the reef, where the wave energy is high, the c.pdf1. In higher parts of the reef, where the wave energy is high, the c.pdf
1. In higher parts of the reef, where the wave energy is high, the c.pdf
 
a)dydx + 2yx = xI.F. = x^2solution iy .pdf
a)dydx + 2yx = xI.F. = x^2solution iy .pdfa)dydx + 2yx = xI.F. = x^2solution iy .pdf
a)dydx + 2yx = xI.F. = x^2solution iy .pdf
 
23.C. 1 in 424. B. Homozygous25. B. 5026.All females will be .pdf
23.C. 1 in 424. B. Homozygous25. B. 5026.All females will be .pdf23.C. 1 in 424. B. Homozygous25. B. 5026.All females will be .pdf
23.C. 1 in 424. B. Homozygous25. B. 5026.All females will be .pdf
 
A is correct. The ntp server 10.0.0.5 command is entered in Global C.pdf
A is correct. The ntp server 10.0.0.5 command is entered in Global C.pdfA is correct. The ntp server 10.0.0.5 command is entered in Global C.pdf
A is correct. The ntp server 10.0.0.5 command is entered in Global C.pdf
 
A. Gene expression.B. Transcription factor.C. Gene expression..pdf
A. Gene expression.B. Transcription factor.C. Gene expression..pdfA. Gene expression.B. Transcription factor.C. Gene expression..pdf
A. Gene expression.B. Transcription factor.C. Gene expression..pdf
 
All are capable to grow in lactose medium . In presence of lactose t.pdf
All are capable to grow in lactose medium . In presence of lactose t.pdfAll are capable to grow in lactose medium . In presence of lactose t.pdf
All are capable to grow in lactose medium . In presence of lactose t.pdf
 
1)The viruse which is the most dangerous threat.In DTI survey,72 of.pdf
1)The viruse which is the most dangerous threat.In DTI survey,72 of.pdf1)The viruse which is the most dangerous threat.In DTI survey,72 of.pdf
1)The viruse which is the most dangerous threat.In DTI survey,72 of.pdf
 
HClO3 + NaOH NaClO3 +H2O n(HClO3) = 0.10.04 = 4.pdf
                     HClO3 + NaOH  NaClO3 +H2O n(HClO3) = 0.10.04 = 4.pdf                     HClO3 + NaOH  NaClO3 +H2O n(HClO3) = 0.10.04 = 4.pdf
HClO3 + NaOH NaClO3 +H2O n(HClO3) = 0.10.04 = 4.pdf
 
The -OCl ion. .pdf
                     The -OCl ion.                                    .pdf                     The -OCl ion.                                    .pdf
The -OCl ion. .pdf
 
I have already answered it. Check. .pdf
                     I have already answered it. Check.               .pdf                     I have already answered it. Check.               .pdf
I have already answered it. Check. .pdf
 
Iodide is a strong nucleophile but a weak base, s.pdf
                     Iodide is a strong nucleophile but a weak base, s.pdf                     Iodide is a strong nucleophile but a weak base, s.pdf
Iodide is a strong nucleophile but a weak base, s.pdf
 
NaCl Its the coulombic interaction between Na.pdf
                     NaCl  Its the coulombic interaction between Na.pdf                     NaCl  Its the coulombic interaction between Na.pdf
NaCl Its the coulombic interaction between Na.pdf
 
Here Jupiter is the fifth planet from the Sun a.pdf
                     Here  Jupiter is the fifth planet from the Sun a.pdf                     Here  Jupiter is the fifth planet from the Sun a.pdf
Here Jupiter is the fifth planet from the Sun a.pdf
 
moles of H+ = 42.0 x 0.120 millimoles = 5.04 mill.pdf
                     moles of H+ = 42.0 x 0.120 millimoles = 5.04 mill.pdf                     moles of H+ = 42.0 x 0.120 millimoles = 5.04 mill.pdf
moles of H+ = 42.0 x 0.120 millimoles = 5.04 mill.pdf
 
Lithium is group 1 metal, while nitrogen is group.pdf
                     Lithium is group 1 metal, while nitrogen is group.pdf                     Lithium is group 1 metal, while nitrogen is group.pdf
Lithium is group 1 metal, while nitrogen is group.pdf
 
there is some special character in ur question li.pdf
                     there is some special character in ur question li.pdf                     there is some special character in ur question li.pdf
there is some special character in ur question li.pdf
 
a.Times Interest Earned Ratio = EBIT interest ExpenseFor 2014.pdf
a.Times Interest Earned Ratio = EBIT  interest ExpenseFor 2014.pdfa.Times Interest Earned Ratio = EBIT  interest ExpenseFor 2014.pdf
a.Times Interest Earned Ratio = EBIT interest ExpenseFor 2014.pdf
 
45 yrs.Solution45 yrs..pdf
45 yrs.Solution45 yrs..pdf45 yrs.Solution45 yrs..pdf
45 yrs.Solution45 yrs..pdf
 
the bacteria isolated from the dead mouse is smooth (S) S. pneumonia.pdf
the bacteria isolated from the dead mouse is smooth (S) S. pneumonia.pdfthe bacteria isolated from the dead mouse is smooth (S) S. pneumonia.pdf
the bacteria isolated from the dead mouse is smooth (S) S. pneumonia.pdf
 

Recently uploaded

SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
CaitlinCummins3
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
中 央社
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
中 央社
 

Recently uploaded (20)

When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
demyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptxdemyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptx
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 
Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
Supporting Newcomer Multilingual Learners
Supporting Newcomer  Multilingual LearnersSupporting Newcomer  Multilingual Learners
Supporting Newcomer Multilingual Learners
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 

public class LinkedHashEntry { private int key; private int valu.pdf

  • 1. public class LinkedHashEntry { private int key; private int value; private LinkedHashEntry next; LinkedHashEntry(int key, int value) { this.key = key; this.value = value; this.next = null; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } public int getKey() { return key; } public LinkedHashEntry getNext() { return next; } public void setNext(LinkedHashEntry next) { this.next = next; } } public class HashMap { private final static int TABLE_SIZE = 128; LinkedHashEntry[] table;
  • 2. HashMap() { table = new LinkedHashEntry[TABLE_SIZE]; for (int i = 0; i < TABLE_SIZE; i++) table[i] = null; } public int get(int key) { int hash = (key % TABLE_SIZE); if (table[hash] == null) return -1; else { LinkedHashEntry entry = table[hash]; while (entry != null && entry.getKey() != key) entry = entry.getNext(); if (entry == null) return -1; else return entry.getValue(); } } public void put(int key, int value) { int hash = (key % TABLE_SIZE); if (table[hash] == null) table[hash] = new LinkedHashEntry(key, value); else { LinkedHashEntry entry = table[hash]; while (entry.getNext() != null && entry.getKey() != key) entry = entry.getNext(); if (entry.getKey() == key) entry.setValue(value); else entry.setNext(new LinkedHashEntry(key, value)); } }
  • 3. public void remove(int key) { int hash = (key % TABLE_SIZE); if (table[hash] != null) { LinkedHashEntry prevEntry = null; LinkedHashEntry entry = table[hash]; while (entry.getNext() != null && entry.getKey() != key) { prevEntry = entry; entry = entry.getNext(); } if (entry.getKey() == key) { if (prevEntry == null) table[hash] = entry.getNext(); else prevEntry.setNext(entry.getNext()); } } } } Solution public class LinkedHashEntry { private int key; private int value; private LinkedHashEntry next; LinkedHashEntry(int key, int value) { this.key = key; this.value = value; this.next = null; } public int getValue() { return value; }
  • 4. public void setValue(int value) { this.value = value; } public int getKey() { return key; } public LinkedHashEntry getNext() { return next; } public void setNext(LinkedHashEntry next) { this.next = next; } } public class HashMap { private final static int TABLE_SIZE = 128; LinkedHashEntry[] table; HashMap() { table = new LinkedHashEntry[TABLE_SIZE]; for (int i = 0; i < TABLE_SIZE; i++) table[i] = null; } public int get(int key) { int hash = (key % TABLE_SIZE); if (table[hash] == null) return -1; else { LinkedHashEntry entry = table[hash]; while (entry != null && entry.getKey() != key) entry = entry.getNext();
  • 5. if (entry == null) return -1; else return entry.getValue(); } } public void put(int key, int value) { int hash = (key % TABLE_SIZE); if (table[hash] == null) table[hash] = new LinkedHashEntry(key, value); else { LinkedHashEntry entry = table[hash]; while (entry.getNext() != null && entry.getKey() != key) entry = entry.getNext(); if (entry.getKey() == key) entry.setValue(value); else entry.setNext(new LinkedHashEntry(key, value)); } } public void remove(int key) { int hash = (key % TABLE_SIZE); if (table[hash] != null) { LinkedHashEntry prevEntry = null; LinkedHashEntry entry = table[hash]; while (entry.getNext() != null && entry.getKey() != key) { prevEntry = entry; entry = entry.getNext(); } if (entry.getKey() == key) { if (prevEntry == null) table[hash] = entry.getNext(); else prevEntry.setNext(entry.getNext());