-1

I wonder why the logic if (!map.containsKey("Africa")) neither go to if block (XX) nor else block (YY)

public class HashMapWithListTest {

public static void main (String args[]) {
    HashMap<String,List<String>> map=new HashMap<String,List<String>>();

    //to put data firs time
    String country="USA";
    //create list for cities
    List<String> cityList=new ArrayList<String>();
    //then fill list
    cityList.add("New York");
    cityList.add("Los Angeles ");
    cityList.add("Chicago");

    //lets put this data to map
    map.put(country, cityList);

    //same thind with other data
    country="Pakistan";
    cityList=new ArrayList<String>();
    cityList.add("Lahore");
    cityList.add("Karachi");
    map.put(country, cityList);

    country="Malaysia";
    cityList=new ArrayList<String>();
    cityList.add("Kuala Lumpur");
    cityList.add("Johor Baru");
    map.put(country, cityList);

    country="Indonesia";
    cityList=new ArrayList<String>();
    cityList.add("Jakarta");
    cityList.add("Bandung");
    map.put(country, cityList);

    //now lets check what is in map
    System.out.println(map);

    //to add city in USA
    //you need to get List of cities and add new one 
    map.get("USA").add("Washington");

    //to get all values from USA
    System.out.println("city in USA:");
    List<String> tmp=map.get("USA");
    for (String city:tmp)
        System.out.println(city);

    System.out.println("x------------------x");
    map.remove("USA");
    System.out.println(map);
    System.out.println("x------------------x");
    map.get("Indonesia").add("Lembang");
    System.out.println(map.get("Indonesia"));
    System.out.println("x------------------x");
    country="Indonesia";
    cityList=new ArrayList<String>();
    cityList.add("Semarang");
    cityList.add("Bali");
    map.putIfAbsent(country, cityList);

    if (!map.containsKey("Africa")) {
        System.out.println(map.get("XX"));
    } else {
        System.out.println(map.get("YY"));
    }
}

}

lagunaloire
  • 81
  • 3
  • 9

3 Answers3

0

map.containsKey("Africa") returns false in your code (because there is no such key in your HashMap) and then map.get("XX") produces null value (there is no value associated with "XX" key in your HashMap), which is printed by System.out.println.

Code is correct, possibly there is a semantics issue

Cootri
  • 3,806
  • 20
  • 30
  • 1
    no problem, mate. You're welcome :) I recommend you to use debugger - it is very easy to find any mistakes with its help – Cootri Mar 23 '16 at 10:12
0

Best way to check existence of a key:

Myobject value = map.get(key);
if (value != null) {
...
} else {
if (map.containsKey(key)) {
   // key exists but it's null
} else {
   // No such key
}
}

Because this is the way HashMap.containsKey works:

@Override
public boolean containsKey(Object key) {
Entry<K, V> m = getEntry(key);
return m != null;
}
Stef_ro89
  • 81
  • 1
  • 10
0

it is going in if loop..if you want to check, repace System.out.println(map.get("XX")); with System.out.println("XX");

NikhilP
  • 1,508
  • 14
  • 23