首页 > 编程语言 > 详细

java HashMap的使用

时间:2015-11-17 10:53:39      阅读:277      评论:0      收藏:0      [点我收藏+]

java HashMap的使用 

import java.util.HashMap;
import java.util.Iterator;
public class WpsklHashMap
{
public static void main(String[] args)
{
   System.out.println("************HashMap**********");
   //在HashMap中的对象是无序的
   HashMap hm=new HashMap();
   hm.put("a","wpskl 1");
   hm.put("b","wpskl 3");
   hm.put("c","wpskl 5");
   //测试是否包含关键字"a"
   System.out.println(hm.containsKey("a"));
   System.out.println(hm.containsKey("d"));
   System.out.println(hm.get("a"));
   System.out.println(hm.entrySet());
   Iterator it=hm.entrySet().iterator();
   while(it.hasNext())
   {
    System.out.println(it.next());
   }
   //Set keySet()返回关键字的集合
   it=hm.keySet().iterator();
   while(it.hasNext())
   {
    System.out.println(it.next());
   }
   //Collection values()返回值的集合
   it=hm.values().iterator();
   while(it.hasNext())
   {
    System.out.println(it.next());
   }
}
}

****************************************************************

 

AVA题.HashMap类

用HashMap类完成: 
考号组成:学号+课程编号+系统日期 例如:s19303C2007723 
分数:在0-10之间 
考号作为键,分数作为值. 
要求分别录入5名学员信息,统计5名学员中参加JAVA考试的平均成绩. 

最佳答案

import java.util.HashMap;
import java.util.Iterator;
public class Test {
    public static void main(String[] args) {
        HashMap<String, Integer> map = new HashMap<String, Integer>(); 
        map.put("s19303C2007723", 8); 
        map.put("s19303C2007724", 6); 
        map.put("s19303C2007725", 4); 
        map.put("s19303C2007726", 8); 
        map.put("s19303C2007727", 3); 

        int sum = 0; 

        for (Iterator<String> i = map.keySet().iterator(); i.hasNext();) { 
        String key = i.next(); 
        int value = map.get(key); 
        sum += value; 
        } 
        System.out.println(sum / (double) 5); 
    }
}

 

 

 

java HashMap的使用

原文:http://www.cnblogs.com/tc310/p/4970831.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!