java HashMap的使用

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); 
    }
}
 
原文地址:https://www.cnblogs.com/tc310/p/4970831.html