hashmap集合类用法

来源:互联网 发布:k60单片机特点 编辑:程序博客网 时间:2024/05/18 03:46

hashmap的增加

public static void main(String[] args) {
// TODO Auto-generated method stub
    HashMap ha=new HashMap();
    food f1=new food("1", "苹果", 222);
    food f2=new food("2","西瓜",111);
    ha.put("1", f1);   ---集合元素增加
    ha.put("2", f2);   
    //hashmap的历遍
    Iterator it=ha.keySet().iterator();  ---激活所有键值
    while (it.hasNext()) {
    System.out.println(((food) ha.get(it.next())).getName());

}
   
    
}

原创粉丝点击