getOrDefault is a useful method that we can use on map s in Java. We give two arguments here. The first is a key , if value for that key is present in the map it returns the value ortherwise it returns the second argurment we pass to this map
Here itemMap contains Strings as keys and Item objects as values
Item itemObject = itemMap.getOrDefault(item.getName(),item);
If item.getName(key) returns a value i.e an Item object well and good. If key does not return an object then return the object we passed as second argument i.e item
Now if the object for the key is present in the map then that value will be returned and not the item object argument we passed
So this will be hold true
if(itemObject!=item){
//Do something
}
i,e value returned is different from item. And since we are using == here that means it will check in memory ,so if two objects have same class variables and even if we have overriden equals method(more on equals method in another topic) ,they will still equate different
If we dont want to use getOrDefault then the traditional way of doing this would be like below
Item itemObject = itemMap.get(item.getName());
if(inStock!=null){//If value is present in the map i.e its not null
// Do something
}