`
tntxia
  • 浏览: 1485985 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

java HashMap的使用

阅读更多

Java中怎样遍历Map的所有的元素:

JDK1.4中

    view plaincopy to clipboardprint?
    <FONT color=#0000ff>Map map = new HashMap();

        Iterator it = map.entrySet().iterator();

        while (it.hasNext()) {

            Map.Entry entry = (Map.Entry) it.next();

            Object key = entry.getKey();

            Object value = entry.getValue();

    }</FONT>

    Map map = new HashMap();

        Iterator it = map.entrySet().iterator();

        while (it.hasNext()) {

            Map.Entry entry = (Map.Entry) it.next();

            Object key = entry.getKey();

            Object value = entry.getValue();

    }JDK1.5中,应用新特性For-Each循环

    view plaincopy to clipboardprint?
    Map m = new HashMap();

    for(Object o : map.keySet()){

        map.get(o);

    }

    Map m = new HashMap();

    for(Object o : map.keySet()){

        map.get(o);

    }返回的 set 中的每个元素都是一个 Map.Entry 类型。

    view plaincopy to clipboardprint?
    <FONT color=#0000ff>private Hashtable<String, String> emails = new Hashtable<String, String>();</FONT>

    private Hashtable<String, String> emails = new Hashtable<String, String>();  另外 我们可以先把hashMap 转为集合Collection,再迭代输出,不过得到的对象

    view plaincopy to clipboardprint?
     <FONT color=#0000ff>//方法一: 用entrySet()

       Iterator it = emails.entrySet().iterator();

       while(it.hasNext()){

        Map.Entry m=(Map.Entry)it.next();

        logger.info("email-" + m.getKey() + ":" + m.getValue());

       }

       // 方法二:jdk1.5支持,用entrySet()和For-Each循环()

       for (Map.Entry<String, String> m : emails.entrySet()) {

        logger.info("email-" + m.getKey() + ":" + m.getValue());

       }

       // 方法三:用keySet()

       Iterator it = emails.keySet().iterator();

       while (it.hasNext()){

        String key;

        key=(String)it.next();

        logger.info("email-" + key + ":" + emails.get(key));

       }

    // 方法五:jdk1.5支持,用keySEt()和For-Each循环

    for(Object m: emails.keySet()){

        logger.info("email-" + m+ ":" + emails.get(m));

       }
    </FONT>

     //方法一: 用entrySet()

       Iterator it = emails.entrySet().iterator();

       while(it.hasNext()){

        Map.Entry m=(Map.Entry)it.next();

        logger.info("email-" + m.getKey() + ":" + m.getValue());

       }

       // 方法二:jdk1.5支持,用entrySet()和For-Each循环()

       for (Map.Entry<String, String> m : emails.entrySet()) {

        logger.info("email-" + m.getKey() + ":" + m.getValue());

       }

       // 方法三:用keySet()

       Iterator it = emails.keySet().iterator();

       while (it.hasNext()){

        String key;

        key=(String)it.next();

        logger.info("email-" + key + ":" + emails.get(key));

       }

    // 方法五:jdk1.5支持,用keySEt()和For-Each循环

    for(Object m: emails.keySet()){

        logger.info("email-" + m+ ":" + emails.get(m));

       }

 

HashMap的插入

 

用HashMap类完成:

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

集合类这学的不是很好~尤其是HashMap类.希望大家多多帮忙~要详细过程
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);
}
}

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics