码迷,mamicode.com
首页 > 其他好文 > 详细

HashMap使用

时间:2015-08-08 22:40:43      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

 

 

/*
 * 测试HashMap的应用,判断
 */
import java.util.HashMap;

public class HuaWeiTest {

    private static final Integer ONE = new Integer(1);

    public static void main(String[] args) {
         HashMap<Character, Integer> m=new HashMap<Character, Integer>();
         char c[]={‘张‘,‘张‘,‘王‘,‘王‘,‘王‘,‘赵‘,‘刘‘};
         for(int i=0;i<c.length;i++){
             Integer freq=(Integer)m.get(c[i]);  //get方法是获取这个position的value;c[i]是position;取值;
             m.put(c[i], freq==null?ONE:new Integer(freq.intValue()+1));
             
         }
         System.out.println("不同姓氏有"+m.size()+"个");
         System.out.println(m);
    }
}

测试结果:

不同姓氏有4个
{张=2, 赵=1, 刘=1, 王=3}

HashMap使用

标签:

原文地址:http://www.cnblogs.com/snowwhite/p/4714032.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!