Map遍历的几种方法
来源:互联网 发布:虚拟机运行mac os x慢 编辑:程序博客网 时间:2024/04/30 15:13
请尊重原创,转载地址:http://blog.csdn.net/zhaokuo719/article/details/7445770
public static void main(String[] args) {
// Map<String, Object> map = new HashMap<String, Object>();
Map<String, Object> map = new LinkedHashMap<String, Object>();
map.put("username","zhaokuo");
map.put("password", "123456");
map.put("email", "zhaokuo719@163.com");
map.put("sex", "男");
//第一种 用for循环的方式
for (Map.Entry<String, Object> m :map.entrySet()) {
System.out.println(m.getKey()+"\t"+m.getValue());
}
//利用迭代 (Iterator)
Set set=map.entrySet();
Iterator iterator=set.iterator();
while(iterator.hasNext()){
Map.Entry<String, Object> enter=(Entry<String, Object>) iterator.next();
System.out.println(enter.getKey()+"\t"+enter.getValue());
}
//利用KeySet 迭代
Iterator it = map.keySet().iterator();
while(it.hasNext()){
String key;
String value;
key=it.next().toString();
value=(String) map.get(key);
System.out.println(key+"--"+value);
}
//利用EnterySet迭代
Iterator i=map.entrySet().iterator();
System.out.println( map.entrySet().size());
String key;
String value;
while(i.hasNext()){
Map.Entry entry = (Map.Entry)i.next();
key=entry.getKey().toString();
value=entry.getValue().toString();
System.out.println(key+"===="+value);
}
System.out.println(getKeyByValue(map, "zhaokuo"));
}
[java]view plaincopyprint?
public static void main(String[] args) {
// Map<String, Object> map = new HashMap<String, Object>();
Map<String, Object> map = new LinkedHashMap<String, Object>();
map.put("username","zhaokuo");
map.put("password", "123456");
map.put("email", "zhaokuo719@163.com");
map.put("sex", "男");
//第一种 用for循环的方式
for (Map.Entry<String, Object> m :map.entrySet()) {
System.out.println(m.getKey()+"\t"+m.getValue());
}
//利用迭代 (Iterator)
Set set=map.entrySet();
Iterator iterator=set.iterator();
while(iterator.hasNext()){
Map.Entry<String, Object> enter=(Entry<String, Object>) iterator.next();
System.out.println(enter.getKey()+"\t"+enter.getValue());
}
//利用KeySet 迭代
Iterator it = map.keySet().iterator();
while(it.hasNext()){
String key;
String value;
key=it.next().toString();
value=(String) map.get(key);
System.out.println(key+"--"+value);
}
//利用EnterySet迭代
Iterator i=map.entrySet().iterator();
System.out.println( map.entrySet().size());
String key;
String value;
while(i.hasNext()){
Map.Entry entry = (Map.Entry)i.next();
key=entry.getKey().toString();
value=entry.getValue().toString();
System.out.println(key+"===="+value);
}
System.out.println(getKeyByValue(map, "zhaokuo"));
}
[java]view plaincopyprint?
//根据Value取Key
public static String getKeyByValue(Map map, Object value) {
String keys="";
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Entry) it.next();
Object obj = entry.getValue();
if (obj != null && obj.equals(value)) {
keys=(String) entry.getKey();
}
}
return keys;
}
public static String getKeyByValue(Map map, Object value) {
String keys="";
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Entry) it.next();
Object obj = entry.getValue();
if (obj != null && obj.equals(value)) {
keys=(String) entry.getKey();
}
}
return keys;
}
0 0
- 遍历Map的几种方法
- Java 遍历Map的几种方法
- 遍历Map的几种方法
- 遍历Map的几种常用方法
- 遍历Map的几种方法《转载》
- 遍历Map的几种方法
- java Map 遍历的几种方法
- 遍历map的几种方法
- Map的几种遍历方法
- 遍历Map的几种方法
- Map遍历的几种方法
- Map遍历的几种方法
- Map遍历的几种方法
- 遍历map的几种方法
- 几种遍历map的方法
- java 几种遍历map的方法
- 遍历map的几种方法
- Map遍历的几种方法
- 曾经的自己 现在的我
- android轮询最佳实践service+AlarmManager+Thread
- IntelliJ IDEA 常用插件收集
- cocos2dx 中 ProgressTimer的使用
- 利用 SGA 共享池,避开 parse 阶段
- Map遍历的几种方法
- 如何使用ueditor
- 将日期控件绑定指定的EditText中,自然显示给定的时间
- cron表达式详解
- Android获取系统当前日期及将日期转换成对应的星期
- 主元素问题--蒙特卡罗
- encodeURIComponent
- IIC协议的知识总结与积累
- 字符间距,行间距