简体   繁体   中英

sorting map of the key to values with integer and strings

I am trying to return the mapping of faces to letters for this die. The faces are identified using the Integer values 1 through 6, and the returned map is sorted on its keys (the face numbers). My code is as following:

public SortedMap getValueMap() {

    SortedMap<Integer, String> sm = new TreeMap<Integer, String>();

    sm.put(new Integer(1), "A");
    sm.put(new Integer(2), "A");
    sm.put(new Integer(4), "E");
    sm.put(new Integer(3), "E");
    sm.put(new Integer(5), "G");
    sm.put(new Integer(6), "N");
    Set<Entry<Integer, String>> s = sm.entrySet();

    Iterator<Entry<Integer, String>> i = s.iterator();

    for (i=1; i.hasNext();) {
        Map.Entry m = (Map.Entry) i.next();
        int key = (Integer) m.getKey();
        String sValue = (String) m.getValue();

    }
    return "Key :" + key + "  value :" + sValue;

}

You don't need to initialize i=1 here. Change it to a while loop

while(i.hasNext())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM