Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

topping2 java challenge issue

Hi,

public Map<String, String> topping2(Map<String, String> map) {
  if(map.get("ice cream")!=null){
    map.put("yogurt", map.get("ice cream"));
  }
  if(map.get("spinach")!=null){
    map.put("nuts", map.get("spinach"));
  }
  else{
    map=map;
  }
  return map;
}

Open in new window

how above code different from below code

public Map<String, String> topping2(Map<String, String> map) {
//public Map < String, String > topping1(Map < String, String > map) {
   // map.put("spinach", "nuts");
    if (map.containsKey("spinach")) {
        map.put("spinach", "nuts");
   }
      if (map.containsKey("ice cream")) {
        map.put("yogurt", map.get("ice cream"));
       // map.put("ice cream", map.get("ice cream"));

   }
    return map;
}

Open in new window


my above first code fails below test
http://codingbat.com/prob/p196458

Expected      Run            
topping2({"ice cream": "cherry"}) → {"yogurt": "cherry", "ice cream": "cherry"}      {"yogurt": "cherry", "ice cream": "cherry"}      OK      
topping2({"spinach": "dirt", "ice cream": "cherry"}) → {"yogurt": "cherry", "spinach": "nuts", "ice cream": "cherry"}      {"yogurt": "cherry", "spinach": "dirt", "ice cream": "cherry", "nuts": "dirt"}      X      
topping2({"yogurt": "salt"}) → {"yogurt": "salt"}      {"yogurt": "salt"}      OK      
topping2({"yogurt": "salt", "bread": "butter"}) → {"yogurt": "salt", "bread": "butter"}      {"yogurt": "salt", "bread": "butter"}      OK      
topping2({}) → {}      {}      OK      
topping2({"ice cream": "air", "salad": "oil"}) → {"yogurt": "air", "ice cream": "air", "salad": "oil"}      {"yogurt": "air", "ice cream": "air", "salad": "oil"}      OK      
ASKER CERTIFIED SOLUTION
Avatar of rrz
rrz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

if(map.get("ice cream")!=null){

is above approach better or below approach
  if (map.containsKey("spinach")) {

there are multiple ways of doing same thign right how to decide best way?
please advise
Avatar of gudii9

ASKER

also when else loop is mandatory and when it is optional?
Avatar of gudii9

ASKER

If the key "spinach" has a value, change that value to "nuts".
what is meaning of above?

if(map.get("spinach")!=null){
    map.put("nuts", map.get("spinach"));
  }

Open in new window

how above different from below
if(map.get("spinach")!=null){
    map.put("spinach", "nuts");

Open in new window

there are multiple ways of doing same thign right how to decide best way?
Personally, I would choose the way that is more readable. If I do, then, when I come back to it in the future, I will quickly know what the code is doing.  
In this case,
if (map.containsKey("spinach"))

Open in new window

is more readable than
if(map.get("ice cream")!=null)

Open in new window

because I have to remember that Map's get method returns null when the key is not there.  
also when else loop is mandatory and when it is optional?
In this case, it doesn't make sense to use it.  The challenge gave us a map and requested that if this then do that and if another thing then do something. That is exactly what our code should do.
if(map.get("spinach")!=null){
    map.put("nuts", map.get("spinach"));
  }

Open in new window

That would result in a Map entry  of  {"nuts": "whatever-the-original-value-was"}  
The challenge told us to change the value not the key.
Avatar of gudii9

ASKER

If the key "spinach" has a value, change that value to "nuts".
what is meaning of above?

if(map.get("spinach")!=null){
    map.put("nuts", map.get("spinach"));
  }


how above different from below


if(map.get("spinach")!=null){
    map.put("spinach", "nuts");
Avatar of gudii9

ASKER

if(map.get("spinach")!=null){
    map.put("nuts", map.get("spinach"));
  }

Select all
 
Open in new window
That would result in a Map entry  of  {"nuts": "whatever-the-original-value-was"}  
The challenge told us to change the value not the key.




as per map api


Java Map API

// Make a new empty map
Map<String, String> map = new HashMap<String, String>();
map.get(key) -- retrieves the stored value for a key, or null if that key is not present in the map.

map.put(key, value) -- stores a new key/value pair in the map. Overwrites any existing value for that key.

map.containsKey(key) -- returns true if the key is in the map, false otherwise.

map.remove(key) -- removes the key/value pair for this key if present. Does nothing if the key is not present.





i thought i was changing value only not there key?
That is the reasone i wrote like



if(map.get("spinach")!=null){
    map.put("nuts", map.get("spinach"));
  }

Select all
 
Open in new window
That would result in a Map entry  of  {"nuts": "whatever-the-original-value-was"}  


not able to visualize what below line doing?

 map.put("spinach", "nuts");
Avatar of gudii9

ASKER

If the key "spinach" has a value, change that value to "nuts".
when challenge says  "that value" does the author mean that as key or value  ?
Avatar of gudii9

ASKER

i think i got it now.
if key spinach has any value like xyz
change that xyz to nuts

so that spinach key is with value nuts


what happens if spinach key do not have any value?
In the API, we have  map.put(key, value)  
The first parameter is the key.
If you use
map.put("nuts", map.get("spinach"));

Open in new window

 
then you will have a key that is a String with the value of "nuts". That is not what they asked us to do. They wanted us to keep the "spinach" key and change it's value.
what happens if spinach key do not have any value?
If the map does not have a spinach key then do nothing.
Avatar of gudii9

ASKER

topping2({"ice cream": " "})

what happens as above if "ice cream" key has  value which is " " or "" or null

please advise

especially in the map.containsKy() approach not map.get approach like



public Map<String, String> topping2(Map<String, String> map) {
  //if(map.get("ice cream")!=null){
      if (map.containsKey("ice cream")) {
        map.put("yogurt", map.get("ice cream"));
        }  
        //if(map.get("spinach")!=null){
    if (map.containsKey("spinach")) {
        map.put("spinach", "nuts");
   }

    return map;
}
And, don't forget, you can always go back and re-read the answer to this question the last time you asked it :

https://www.experts-exchange.com/questions/28973402/topping2-challenge.html