#Question about Collision Handling with HashMaps

1 messages · Page 1 of 1 (latest)

wintry sinewBOT
#

Detected code, here are some useful tools:

Formatted code
A grocery store wants to track products that are past their expiration date(no longer fresh).The store’s software engineer tackles the problem by writing the following code, but it has some problems that you’re going to fix : class NotFreshRecordHashMap {
  HashMap<String, GroceryItem> notFreshMap;
  public NotFreshRecordHashMap() {
    notFreshMap = new HashMap<String, GroceryItem>();
  }
  void addGroceryItem(String name, GroceryItem item) {
    notFreshMap.put(name, item);
  }
  GroceryItem getGroceryItem(String name) {
    if (!notFreshMap.containsKey(name)) return null ;
    return notFreshMap.get(name);
  }
}
class ItemNotFoundException extends Exception {
  String err;
  public ItemNotFoundException(String err) {
    this .err = err;
  }
  String getErr() {
    return this .err;
  }
}
a.Modify the above code so that it uses appropriate access modifiers on all fields and methods.b.Modify getGroceryItem so that instead of returningnull if the item is not in the HashMap, it implements exception handling using ItemNotFoundException.Youdo not need to modify any other methodsfor this step.c.Modify the above code to handle collisions(using a list structure).This may require modifying the HashMap itself and / or some method parameter andreturn types.Use getGroceryItem in other methodsif you need to get something from your hash map to modify.If getGroceryItemthrows an exception, respond by instantiating something.NOTE : The GroceryItemclass is not providedfor you, but you don't need it to solvethis problem.If you solve the problem using an IDE, you can stub out a GroceryItemclass to get your code to compile.
#

<@&987246399047479336> please have a look, thanks.

#

Detected code, here are some useful tools:

severe socket
#

is what i have so far

#

what am I meant to do for part c? i thought java already handles collisions with chaining

#

shoul di make ti a HashMap of <String, List<GroceryItem>?

#

so if they're equal i just don't do anything?

#

or smth?

#

im confused

#

and in a time crunch :soB:

#

😭

cosmic stream
#

are u sure ur supposed to use javas hashmap and not ur own selfmade?

#

javas hashmap handles collisions fully by itself

severe socket
#

yeah that's what i thought

#

but the instructions never say to make my own hashmap as far as i can tell

wintry sinewBOT
#

Detected code, here are some useful tools:

cosmic stream
#

that's just... so unnecessary

#

hashmap already maintains lists by itself in each bucket to handle collisions

#

even better, it maintains them as either lists or as red-black tree dynamically, yielding superior performance

severe socket
#

yeah that's what i was thinking

wintry sinewBOT
#

Detected code, here are some useful tools:

severe socket
#

how does this look@cosmic stream?

#

would it work functionally

cosmic stream
#

u have to talk to ur prof or someone, not us

#

hashmap handles collisions by itself

#

doing it "again on top" is stupid

#

and makes things much worse

#

or wait, does he want u to handle collisions on the values?

#

hashmap does it for keys, not values (obviously)

severe socket
#

yeah

#

im not too sure

#

this is the only example hes given us

#

i tried to emulate that

cosmic stream
#

then u should first work towards understanding the assignment

#

u probably have to ask them for details

#

we can't tell u what they wanted u to do

severe socket
#

hm ok

#

ill ask but they might not answer

#

sorry about nagging but do you think my code emulates the example he gave

dire hill
#

Do they just mean that two of the same key could be added and that the values should be a List<GroceryItem>? That's an application-data collision (same name, different data), not a hash-collision.

wintry sinewBOT
#

@severe socket

Your question has been closed due to inactivity.

If it was not resolved yet, feel free to just post a message below
to reopen it, or create a new thread.

Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.

When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.

Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.

With enough info, someone knows the answer for sure 👍

severe socket
wintry sinewBOT
#

@severe socket

Your question has been closed due to inactivity.

If it was not resolved yet, feel free to just post a message below
to reopen it, or create a new thread.

Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.

When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.

Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.

With enough info, someone knows the answer for sure 👍