有一个HashMap对象,其中key是一个string对象,value是一个Integer对象,要求写一个方法,输出HashMap对象的值,形式为:key -- value
(我想要按顺序输出HashMap中的值(有序),请各位热心人在此代码上修改,最好能在各位机子上运行通过的全部代码,谢谢!)方法1:
public class HashMapTest {
public static void printPL(Map hm) {
Set s = hm.keySet();
Iterator i = s.iterator();
while(i.hasNext()) {
Object o = i.next();
System.out.println(o +" -- "+hm.get(o));
}
}
public static void main(String[] args) {
HashMap hm = new HashMap();
hm.put("ha0",new Integer(1000));
hm.put("ha1",new Integer(1001));
hm.put("ha2",new Integer(1002));
hm.put("ha3",new Integer(1003));
printPL(hm);
}
}方法2:public class HashMapTest { public static void printPL(Map hm) {
Set s = hm.entrySet();
Iterator i = s.iterator();
while(i.hasNext()) {
Map.Entry o = (Map.Entry)i.next();
System.out.println(o.getKey() +" -- "+o.getValue());
}
} public static void main(String[] args) {
HashMap hm = new HashMap();
hm.put("hao",new Integer(1000));
hm.put("ha1",new Integer(1001));
hm.put("ha2",new Integer(1002));
hm.put("ha3",new Integer(1003)); printPL(hm);
}
}
(我想要按顺序输出HashMap中的值(有序),请各位热心人在此代码上修改,最好能在各位机子上运行通过的全部代码,谢谢!)方法1:
public class HashMapTest {
public static void printPL(Map hm) {
Set s = hm.keySet();
Iterator i = s.iterator();
while(i.hasNext()) {
Object o = i.next();
System.out.println(o +" -- "+hm.get(o));
}
}
public static void main(String[] args) {
HashMap hm = new HashMap();
hm.put("ha0",new Integer(1000));
hm.put("ha1",new Integer(1001));
hm.put("ha2",new Integer(1002));
hm.put("ha3",new Integer(1003));
printPL(hm);
}
}方法2:public class HashMapTest { public static void printPL(Map hm) {
Set s = hm.entrySet();
Iterator i = s.iterator();
while(i.hasNext()) {
Map.Entry o = (Map.Entry)i.next();
System.out.println(o.getKey() +" -- "+o.getValue());
}
} public static void main(String[] args) {
HashMap hm = new HashMap();
hm.put("hao",new Integer(1000));
hm.put("ha1",new Integer(1001));
hm.put("ha2",new Integer(1002));
hm.put("ha3",new Integer(1003)); printPL(hm);
}
}
解决方案 »
- 实现弹出模态窗口
- 在html中加入Applet问题
- javad的C/S程序怎样得到网络WEB服务器上的某个目录中的文件
- 谢谢有什么方法将4个byte转化成一个int?
- 请教JTree删除截点和对截点重命名的问题
- 用javac命令编译时,如果有很多的.jar要import,那么有什么好方法包括进来吗????
- 偶是初学者,有个问题咨询一下(applet)
- 大虾版主帮忙看看!!
- 从键盘输入10个字符串 存入数组a中,然后输出这10个字符串中最大字符串和最小字符串。
- 关于jdk6版本ThreadPoolExecutor中的workerCanExit()方法的一点疑问
- 我想使用JAVA做一个聊天工具,请问需要用到JAVA的哪些技术?
- 为什么paintComponent这个函数没有调用??
public class HashMapTest { private HashMap map; HashMapTest(HashMap map){ this.map = map ; } public void printPL(){
SortedMap<String,Integer> tree =
new TreeMap<String,Integer>(new Comparator<String>()
{
public int compare(String a,String b){ return map.get(a).intValue - map.get(b).intValue;}
})
tree.addAll(map);
Set s = tree.entrySet();
Iterator i = s.iterator();
while(i.hasNext()){
Map.Entry o = (Map.Entry)i.next();
System.out.println(o.getKey()+"--"+o.getValue());
}
}
public static void main(String[] args) {
HashMap hm = new HashMap();
hm.put("ha0",new Integer(1000));
hm.put("ha1",new Integer(1001));
hm.put("ha2",new Integer(1002));
hm.put("ha3",new Integer(1003));
new HashMapTest(hm).printPL();
}
}
用的treemap 不知道有什么错误没有! 我没有运行
public static void printPL(Map hm) {
Set s = hm.keySet();
Iterator i = s.iterator();
while(i.hasNext()) {
Object o = i.next();
System.out.println(o +" -- "+hm.get(o));
}
}
public static void main(String[] args) {
TreeMap hm = new TreeMap();
hm.put("ha3",new Integer(1003));
hm.put("ha0",new Integer(1000));
hm.put("ha2",new Integer(1002));
hm.put("ha1",new Integer(1001));
printPL(hm);
}
}
public class HashMapTest { public static void printPL(Map hm) {
Set s = hm.entrySet();
Iterator i = s.iterator();
while(i.hasNext()) {
Map.Entry o = (Map.Entry)i.next();
System.out.println(o.getKey() +" -- "+o.getValue());
}
} public static void main(String[] args) {
TreeMap hm = new TreeMap();
//hm.put("hao",new Integer(1000)); //你这里写o了,没有写0,我帮你改了。
hm.put("ha0",new Integer(1000));
hm.put("ha3",new Integer(1003));
hm.put("ha1",new Integer(1001));
hm.put("ha2",new Integer(1002));
printPL(hm);
}
}
不知道为什么一定要用HashMap