Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

DATA STRUCTURES - MUST USE JAVA PROGRAMMING... HashMap is a structure that stores data in the form of key and value pairs. Table 4 shows

DATA STRUCTURES - MUST USE JAVA PROGRAMMING... image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

HashMap is a structure that stores data in the form of key and value pairs. Table 4 shows the common methods utilized in an implementation of the HashMap. Jadual 4: Senarai metod-metod dan spesifikasi untuk PetaPagar Table 4: List of methods and their specifications for HashMap Pembina/Nama Metod Constructor/Method name i) Constructor for the HashMap class ii) get Spesifikasi Specification Konstruktor lalai yang menerima parameter- parameter kekunci dan nilai pasangan Default constructor which accepts Key and value pair parameters Memulangkan entry yang dipetakan kepada kekunci di dalam peta-pagar Return the entry mapped to key in the HashMap Menambah entry baru sekiranya kekunci belum lagi dipetakan di dalam peta-pagar. Sebaliknya, mengemaskini entry lama sekiranya kekunci telah wujud di dalam peta-pagar. Add new entry if key is not yet mapped into the HashMap. Otherwise, update entry mapped to Key if the HashMap already contains the Key iii) put Figure 4 shows an application of the HashMap that fixes key with student name and pair its value with the course taken at the university. /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package exam2020; /** * @author IBM */ public class MyHashMap { // for better re-sizing is taken as 2-4 private static final int SIZE = 16; private Entry table[] = new Entry[SIZE]; /** * To store the Map data in key and value pair. * Used linked list approach to avoid the collisions */ class Entry { final String key; String value; Entry next; // object of class Entry to traverse the map Entry(String k, String v) { key=k; value=v; } public String getValue() { return value; } public void setValue(String value) { this.value = value; public String getKey() { return key; } } public Entry get(String k) { int hash=k.hashCode() % SIZE; Entry e = table[hash]; // Bucket is identified by hashCode and traversed the bucket // till element is not found. while(e != null) { if(e.key.equals(k)) { return e; } e=e.next; } return null; } /** * If the map previously contained a mapping for the key, the old * value is replaced. */ public void put(String k, String v) { int hash=k.hashCode() % SIZE; Entry e = table[hash]; if(e != null) { // If we will insert duplicate key-value pair, // Old value will be replaced by new one. if(e.key.equals(k)) { e.value = v; } else { // Collision: insert new element at the end of list // in the same bucket while(e.next != null) { e=e.next; } Entry entryInOldBucket = new Entry(k, v); e.next = entry InOldBucket; } } else { // create new bucket for new element in the map. Entry entryInNewBucket = new Entry(k, v); table[hash] = entryInNewBucket; } } public static void main(String[] args) { MyHashMap myHashMap = new MyHashMap(); // New entry myHashMap.put("BruceW", "SE"); myHashMap.put("DeanW", "AI"); myHashMap.put("TonyS", "IS"); myHashMap.put("LaraC", "CST"); System.out.println(""); System.out.println("Retrieving student data: "); Entry el = myHashMap.get("Dean W"); System.out.println("'"' + el.getKey()+":"+el.getValue()); Entry e2 = myHashMap.get("BruceW"); System.out.println("" + e2.getKey() + ":" + e2.getValue()); System.out.println(""); // Updating record myHashMap.put("Bruce W", "CST"); myHashMap.put("JeanG", "SE"); System.out.println("Retrieving student data: "); Entry e3 = myHashMap.get("BruceW"); System.out.println("" + e3.getKey()+":"+e3.getValue()); Entry e4 = myHashMap.get("JeanG"); System.out.println(" + e4.getKey()+":"+e4.getValue()); } Update the methods for the HashMap application in Figure 4 so the application can accept the student's matrix number or email address as key. Please make sure the pairing value for each key can store any other student data such as phone number and year of registration. Your HashMap application should compile and runs sucessfully even though an unknown key is being searched, for a full mark. Figure 5 shows an example output for the updated application. run: Retrieving student data: DeanW: AI, 017-449 2272, 2020 bruce@gmail.com : SE, 011-559 8900, 2018 17110022: CST, 013-4509 722, 2018 Retrieving student data: bruce@gmail.com: CST, 011-559 8900, 2018 JeanG: SE, 014-234 9906, 2020 BUILD SUCCESSFUL (total time: 0 seconds) Rajah 5: Contoh paparan untuk aplikasi yang telah dikemaskini Figure 5: Example output for the updated application No marks will be given for any implementation using the existing HashMap class or any other Collection classes available from the Java API Library. HashMap is a structure that stores data in the form of key and value pairs. Table 4 shows the common methods utilized in an implementation of the HashMap. Jadual 4: Senarai metod-metod dan spesifikasi untuk PetaPagar Table 4: List of methods and their specifications for HashMap Pembina/Nama Metod Constructor/Method name i) Constructor for the HashMap class ii) get Spesifikasi Specification Konstruktor lalai yang menerima parameter- parameter kekunci dan nilai pasangan Default constructor which accepts Key and value pair parameters Memulangkan entry yang dipetakan kepada kekunci di dalam peta-pagar Return the entry mapped to key in the HashMap Menambah entry baru sekiranya kekunci belum lagi dipetakan di dalam peta-pagar. Sebaliknya, mengemaskini entry lama sekiranya kekunci telah wujud di dalam peta-pagar. Add new entry if key is not yet mapped into the HashMap. Otherwise, update entry mapped to Key if the HashMap already contains the Key iii) put Figure 4 shows an application of the HashMap that fixes key with student name and pair its value with the course taken at the university. /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package exam2020; /** * @author IBM */ public class MyHashMap { // for better re-sizing is taken as 2-4 private static final int SIZE = 16; private Entry table[] = new Entry[SIZE]; /** * To store the Map data in key and value pair. * Used linked list approach to avoid the collisions */ class Entry { final String key; String value; Entry next; // object of class Entry to traverse the map Entry(String k, String v) { key=k; value=v; } public String getValue() { return value; } public void setValue(String value) { this.value = value; public String getKey() { return key; } } public Entry get(String k) { int hash=k.hashCode() % SIZE; Entry e = table[hash]; // Bucket is identified by hashCode and traversed the bucket // till element is not found. while(e != null) { if(e.key.equals(k)) { return e; } e=e.next; } return null; } /** * If the map previously contained a mapping for the key, the old * value is replaced. */ public void put(String k, String v) { int hash=k.hashCode() % SIZE; Entry e = table[hash]; if(e != null) { // If we will insert duplicate key-value pair, // Old value will be replaced by new one. if(e.key.equals(k)) { e.value = v; } else { // Collision: insert new element at the end of list // in the same bucket while(e.next != null) { e=e.next; } Entry entryInOldBucket = new Entry(k, v); e.next = entry InOldBucket; } } else { // create new bucket for new element in the map. Entry entryInNewBucket = new Entry(k, v); table[hash] = entryInNewBucket; } } public static void main(String[] args) { MyHashMap myHashMap = new MyHashMap(); // New entry myHashMap.put("BruceW", "SE"); myHashMap.put("DeanW", "AI"); myHashMap.put("TonyS", "IS"); myHashMap.put("LaraC", "CST"); System.out.println(""); System.out.println("Retrieving student data: "); Entry el = myHashMap.get("Dean W"); System.out.println("'"' + el.getKey()+":"+el.getValue()); Entry e2 = myHashMap.get("BruceW"); System.out.println("" + e2.getKey() + ":" + e2.getValue()); System.out.println(""); // Updating record myHashMap.put("Bruce W", "CST"); myHashMap.put("JeanG", "SE"); System.out.println("Retrieving student data: "); Entry e3 = myHashMap.get("BruceW"); System.out.println("" + e3.getKey()+":"+e3.getValue()); Entry e4 = myHashMap.get("JeanG"); System.out.println(" + e4.getKey()+":"+e4.getValue()); } Update the methods for the HashMap application in Figure 4 so the application can accept the student's matrix number or email address as key. Please make sure the pairing value for each key can store any other student data such as phone number and year of registration. Your HashMap application should compile and runs sucessfully even though an unknown key is being searched, for a full mark. Figure 5 shows an example output for the updated application. run: Retrieving student data: DeanW: AI, 017-449 2272, 2020 bruce@gmail.com : SE, 011-559 8900, 2018 17110022: CST, 013-4509 722, 2018 Retrieving student data: bruce@gmail.com: CST, 011-559 8900, 2018 JeanG: SE, 014-234 9906, 2020 BUILD SUCCESSFUL (total time: 0 seconds) Rajah 5: Contoh paparan untuk aplikasi yang telah dikemaskini Figure 5: Example output for the updated application No marks will be given for any implementation using the existing HashMap class or any other Collection classes available from the Java API Library

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Current Trends In Database Technology Edbt 2006 Edbt 2006 Workshops Phd Datax Iidb Iiha Icsnw Qlqp Pim Parma And Reactivity On The Web Munich Germany March 2006 Revised Selected Papers Lncs 4254

Authors: Torsten Grust ,Hagen Hopfner ,Arantza Illarramendi ,Stefan Jablonski ,Marco Mesiti ,Sascha Muller ,Paula-Lavinia Patranjan ,Kai-Uwe Sattler ,Myra Spiliopoulou ,Jef Wijsen

2006th Edition

3540467882, 978-3540467885

More Books

Students also viewed these Databases questions

Question

=+What is the nature of the unions in the particular country?

Answered: 1 week ago