Help Java hash mapping

Can someone explain what happened in the following code and how it ended with 36?

thank you

Edited by Amir rachum

public class HashMap2009 {
    public static void main (String[] args) {
        Map<String,Integer> myMap2009 = 
            new HashMap<String,Integer>();
        myMap2009.put("one",new Integer(1));
        myMap2009.put("three",new Integer(3));
        myMap2009.put("five",new Integer(5));
        myMap2009.put("seven",new Integer(7));
        myMap2009.put("nine",new Integer(9));
        System.out.println(oddOne(myMap2009));
    }
    private static int oddOne(Map<String,Integer> myMap2009) {
        if (myMap2009.isEmpty())
            return 11;
        else {
            Set<String> st = myMap2009.keySet();
            String key = st.iterator().next();
            int num = myMap2009.get(key);
            myMap2009.remove(key);
            return num + oddOne(myMap2009);
        }
    }
}

Solution

This is a simple example of recursion, which causes all keys in the map to be added one by one. When the map is empty, it will add 11 more keys The total is 36 at most

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>