Java的map键值对的用法,map的遍历,Entry对象的使用
思路:
1、定义集合
2、存储数据
3、添加元素
4、遍历
4.1将需要遍历的集合的键封装到set集合中(这用到了entrySet方法,和Entry对象)
4.2声明迭代器或者用for增强循环
4.3通过set集合的getKey和getValue方法拿到值和键
5、打印输出就好
代码呈现如下:
package com.aaa.demo; import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set; public class MaoMapDemo {
public static void main(String[] args) {
//定义Java班的集合
HashMap<String, String> javas=new HashMap<String,String>();
//定义Hdoop班的集合
HashMap<String, String> Hdoop=new HashMap<String,String>();
//向班级存储学生
javas.put("001", "大冰");
javas.put("002", "北岛"); Hdoop.put("001", "舒婷");
Hdoop.put("002", "食指");
//定义aaa容器,键是班级的名字 值是两个班级的容器
HashMap<String, HashMap<String, String>> aaa=new HashMap<String,HashMap<String,String>>();
aaa.put("现代", javas);
aaa.put("近代", Hdoop);
entrySet1(aaa);
}
public static void entrySet(HashMap<String,HashMap<String,String>> aaa){
//调用集合aaa的方法 entrySet将aaa集合的键封装到Set集合中
Set<Entry<String,HashMap<String,String>>> classNameSet=aaa.entrySet();
//迭代Set集合
Iterator<Entry<String,HashMap<String,String>>> it=classNameSet.iterator();
while(it.hasNext()){
Entry<String,HashMap<String,String>> next=it.next();
//getKey getValue 得到键和值
String classNameKey = next.getKey();
System.out.println(classNameKey);
//aaa容器的键 班级姓名classMap
HashMap<String,String> classMap=next.getValue();
//entrySet将classMap集合的键封装到set集合中
Set<Entry<String,String>> studentSet=classMap.entrySet();
//迭代
Iterator<Entry<String, String>> studentIt = studentSet.iterator();
while(studentIt.hasNext()){
//遍历集合
Entry<String,String> studentEntry=studentIt.next();
String numKey = studentEntry.getKey();
String nameValue = studentEntry.getValue();
System.out.println(numKey+": "+nameValue);
}
}
}
public static void entrySet1(HashMap<String,HashMap<String,String>> aaa){
//调用集合aaa的方法 entrySet将aaa集合的键封装到Set集合中
Set<Entry<String,HashMap<String,String>>> cNS=aaa.entrySet();
//for循环遍历得到班级
for(Entry<String,HashMap<String,String>> next:cNS){
//getKey getValue 得到键和值
String classKey = next.getKey();
//aaa容器的键 班级姓名classMap
HashMap<String, String> classValue = next.getValue();
//entrySet将classMap集合的键封装到set集合中
Set<Entry<String, String>> studentSet = classValue.entrySet();
System.out.println(classKey);
//for循环遍历
//Set<Entry<String, String>> entrySet = classValue.entrySet();
for(Entry<String, String> studentEntry:studentSet){
String numKey = studentEntry.getKey();
String nameValue = studentEntry.getValue();
System.out.println(numKey+": "+nameValue);
}
}
}
}
Java的map键值对的用法,map的遍历,Entry对象的使用的更多相关文章
- java 把json对象中转成map键值对
相关:Json对象与Json字符串的转化.JSON字符串与Java对象的转换 本文的目的是把json串转成map键值对存储,而且只存储叶节点的数据 比如json数据如下: {responseHeade ...
- 枚举类返回Map键值对,绑定到下拉框
有时候,页面的下拉框要显示键值对,但是不想从数据库取,此时我们可以写一个枚举类, Java后台代码 1.枚举类 import java.util.HashMap; import java.util.M ...
- 用字典给Model赋值并支持map键值替换
用字典给Model赋值并支持map键值替换 这个是昨天教程的升级版本,支持键值的map替换. 源码如下: NSObject+Properties.h 与 NSObject+Properties.m / ...
- 如何将Map键值的下划线转为驼峰
本文不再更新,可能存在内容过时的情况,实时更新请移步我的新博客:如何将Map键值的下划线转为驼峰: 例,将HashMap实例extMap键值下划线转为驼峰: 代码: HashMap<String ...
- Java Map 键值对排序 按key排序和按Value排序
一.理论准备 Map是键值对的集合接口,它的实现类主要包括:HashMap,TreeMap,Hashtable以及LinkedHashMap等. TreeMap:基于红黑树(Red-Black tre ...
- 通过反射,获取linkedHashMap的最后一个键值对。对map按照值进行排序。
1:通过反射,获取linkedHashMap的最后一个键值对. Map<Integer, Integer> map = new LinkedHashMap<>(); Field ...
- 根据map键值对,生成update与select语句,单条执行语句
方法 constructUpdateSQL private static String constructUpdateSQL(String tableName, List<Map<Stri ...
- Java的HashMap键值对存储结构解析
容器总体结构 Map存储键值对的数据结构是“数组+链表”的结构,结合了数组查询数据快和链表增删数据快的优点:用Entry[]存储键值对,Entry为类类型,类里面有四个属性:hash.K.V.next ...
- a=av###b=bv###c=cv map键值对 (a,av) (b,bv) (c,cv)
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; impo ...
随机推荐
- Unity3D Update() 和 FixedUpdate()区别
Unity3D中 Update()与FixedUpdate()的区别是什么呢?从字面上理解,它们都是在更新时会被调用,并且会循环的调用.但是Update会在每次渲 染新的一帧时,被调用.而FixedU ...
- iOS保存gif动态图
- (void)saveImageToPhotos:(NSData*)gifData { /***注意先倒入库 #import <AssetsLibrary/AssetsLibrary.h> ...
- php图片上传base64数据编码。
/** * base64图片上传 */ function IdImg($base64_img = ''){ $up_dir = 'upload/';//存放在当前目录的upload文件夹下 if(!f ...
- delphi android 自动升级
用IdHTTP1下载文件到手机完成. 调用自动安装不行. First chance exception at $DFC22519. Exception class EJNIException with ...
- sql 2014 安装失败
SQL Server setup failed to modify security permissions on 原因是 上述目录中没有权限,浏览此文件夹试试,有 错误,删除文件夹,无权删除,通过右 ...
- IIS asp 401.1错误
asp程序使用非匿名帐户运行时因用户名前带了计算机名会导致出现401.1错误,只要直接输入用户名即可,不要带计算机名.
- ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql/mysql.sock' (2)
[root@XXX ~]# mysql -h localhost -uroot -p Enter password: ERROR (HY000): Can't connect to local MyS ...
- C++11并发之std::thread<转>
最近技术上没什么大的收获,也是悲催的路过~ 搞一点新东西压压惊吧! C++11并发之std::thread 知识链接: C++11 并发之std::mutex C++11 并发之std::atomic ...
- Java 配置环境变量教程
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/3 ...
- ORA-00600: internal error code, arguments: [4193]问题解决
操作环境 SuSE+Oracle11gR2 问题现象 单板宕机自动重启后,ORACLE运行不正常,主要表现如下: 1.执行shutdown immedate停止数据库时,提示ORA-00600: in ...