请尊重原创,转载地址: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 plain copy print ?
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 plain copy print ?
//根据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;
}
本文分享 CSDN - wangyonglin1123。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。
来源:oschina
链接:https://my.oschina.net/u/4670056/blog/4745417