比如以下代码输出结果为
Entry collection is :
2=Chenar
1=Vida
3=Lillian
如何才能让输出为
Entry collection is :
1=Vida
2=Chenar
3=Lillian谢谢啦~!!
--------------------------------------------------------------------------
import java.util.*;
class HashMapTest
{
public static void printAllElement(Collection co
{
Iterator it=co.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
public static void main(String[] args)
{
HashMap<Integer, Object> hm=new HashMap<Integer, Object>();
hm.put(1, "Vida");
hm.put(2, "Chenar");
hm.put(3, "Lillian");
Set s2=hm.entrySet();
System.out.println("Entry collection is :");
printAllElement(s2);
}
}
Entry collection is :
2=Chenar
1=Vida
3=Lillian
如何才能让输出为
Entry collection is :
1=Vida
2=Chenar
3=Lillian谢谢啦~!!
--------------------------------------------------------------------------
import java.util.*;
class HashMapTest
{
public static void printAllElement(Collection co
{
Iterator it=co.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
public static void main(String[] args)
{
HashMap<Integer, Object> hm=new HashMap<Integer, Object>();
hm.put(1, "Vida");
hm.put(2, "Chenar");
hm.put(3, "Lillian");
Set s2=hm.entrySet();
System.out.println("Entry collection is :");
printAllElement(s2);
}
}
import java.util.HashMap;public class ShowMapWithSortedKeys { /**
* @param args
*/
public static void main(String[] args) {
HashMap map = new HashMap();
map.put("3", "Orange");
map.put("1", "Apple");
map.put("2", "Banana"); String[] keys = (String[]) map.keySet().toArray(new String[map.size()]);
Arrays.sort(keys); for (int i = 0; i < keys.length; i++) {
System.out.println("map[\"" + keys[i] + "\"] = \"" + map.get(keys[i]) + "\"");
}
}}