java之HashMap的遍历Iterator
package com.ql_2;
/*
* 功能:HashMap 的使用
*/
import java.util.*;
public class Test_2 {
public static void main(String[] args) {
// TODO 自动生成的方法存根
HashMap hm=new HashMap();
Emp emp1=new Emp("s001","aaa",3.4f);
Emp emp2=new Emp("s002","花荣",3.5f);
Emp emp3=new Emp("s003","李奎",3.6f);
hm.put("s001", emp1);
hm.put("s002", emp2);
hm.put("s002", emp3);
//查找编号s001
if(hm.containsKey("s001"))//containsKey()返回boolean
{
System.out.println("找到该员工");
Emp emp=(Emp)hm.get("s001");
System.out.println("名字:"+emp.getName());
}
if(hm.containsKey("s002"))
{
System.out.println("找到该员工");
Emp emp=(Emp)hm.get("s002");
System.out.println("名字:"+emp.getName());
}
//遍历HashMap 中所有的Key和Values
//Iterator迭代
Iterator it=hm.keySet().iterator();
while(it.hasNext())
{
//取出key
String key=it.next().toString();
//通过key取出values
Emp emp=(Emp)hm.get(key);
System.out.println("姓名:"+emp.getName());
System.out.println("工资:"+emp.getSal());
}
}
}
//雇员类
class Emp
{
private String empno;
private String name;
private float sal;
//构造函数
Emp(String empno,String name,float sal)
{
this.empno=empno;
this.name=name;
this.sal=sal;
}
public String getEmpno()
{
return empno;
}
public void setEmpno(String empno)
{
this.empno = empno;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public float getSal()
{
return sal;
}
public void setSal(float sal)
{
this.sal = sal;
}
}
java之HashMap的遍历Iterator的更多相关文章
- Java中HashMap遍历的两种方式
Java中HashMap遍历的两种方式 转]Java中HashMap遍历的两种方式原文地址: http://www.javaweb.cc/language/java/032291.shtml 第一种: ...
- Java中五种遍历HashMap的方式
import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class Java8Templat ...
- java 中 HashMap 遍历与删除
HashMap的遍历 方法一.这是最常见的并且在大多数情况下也是最可取的遍历方式 /** * 在键值都需要时使用 */ Map<Integer, Integer> map = new Ha ...
- Java中HashMap(泛型嵌套)的遍历
//Studnet package yzhou.gen03; public class Student<T> { private T score; public T getScore() ...
- Java中HashMap遍历的两种方法(转)
第一种: Map map = new HashMap(); Iterator iter = map.entrySet().iterator(); while (iter.hasNext()) { Ma ...
- Java学习——HashMap
遍历 Map map = new HashMap(); Iterator iter = map.entrySet().iterator(); while (iter.hasNext()) { Map. ...
- Java 集合系列18之 Iterator和Enumeration比较
概要 这一章,我们对Iterator和Enumeration进行比较学习.内容包括:第1部分 Iterator和Enumeration区别第2部分 Iterator和Enumeration实例 转载请 ...
- HashMap循环遍历方式及其性能对比(zhuan)
http://www.trinea.cn/android/hashmap-loop-performance/ ********************************************* ...
- 简单分析Java的HashMap.entrySet()的实现
关于Java的HashMap.entrySet(),文档是这样描述的:这个方法返回一个Set,这个Set是HashMap的视图,对Map的操作会在Set上反映出来,反过来也是.原文是 Returns ...
随机推荐
- Change visual studio 2015 enterprise installation path(转)
I would like to install VS2015 in a drive different than C:. The problem is that when I run the inst ...
- 转 Django+Bootstrap练习--我的类博客系统开发
转自: http://blog.sina.com.cn/s/blog_7e050dc80102w312.html 本文记录了一个类博客网站从无到有的搭建过程,同时也是我入门django以及再次入门前端 ...
- 反汇编角度->C++ const
#include<iostream> #include<stdlib.h> using namespace std; const int &add( const int ...
- Group by 两表联查取另一表与之关联数据的总数
使用group by 需要查询的字段 必须要放在group by 后面 SELECT U.UserLotterySn,count(W.Userlotterysn) as WinCount,U.Acti ...
- vue v-show与v-for同时配合v-bind使用并在href中传递多个参数的使用方法
最近在项目中,因为还没使用前端构建工具,还在使用vue+jquery方法渲染页面 碰到几个小问题,在此记录下作为vue学习之路上的一个小知识点 需求:1.数据列表存在与否状态,没有数据显示默认提示,有 ...
- 2017 ACM-ICPC 沈阳区域赛记录
出发日 中午坐大巴前往萧山机场. 哇开心又可以坐飞机了 飞机延误了.在候机大厅里十分无聊,先用机场的电脑玩了会小游戏 然后偷偷切了2个水题 (什么编译器IDE都没有,只能记事本了) 飞机上什么东西都没 ...
- char *argv[] 与 char **argv
#include<stdio.h> #include<string.h> int main(int argc,char *argv[])//同int main(int argc ...
- 邁向IT專家成功之路的三十則鐵律 鐵律五:IT人穩定發展之道-去除惡習
在我們努力邁向IT專家成功之路的過程當中,實際上會遭遇到許多障礙來影響我們前進,然而在這諸多障礙之中,最難克服的並非是旁人對我們所造成的影響,而是無形之間自己對自己所造的阻礙,如果沒有隨時隨地加以自反 ...
- 深入理解Activity启动流程(三)–Activity启动的详细流程2
本文原创作者:Cloud Chou. 欢迎转载,请注明出处和本文链接 本系列博客将详细阐述Activity的启动流程,这些博客基于Cm 10.1源码研究. 深入理解Activity启动流程(一)--A ...
- Java并发学习 & Executor学习 & 异常逃逸 & 同步互斥Best Practice & wait/notify, conditon#await/signal
看了这篇文章:http://www.ciaoshen.com/2016/10/28/tij4-21/ 有一些Java并发的内容,另外查了一些资料. 朴素的Thread 首先,Java中关于线程Thre ...