概述
package exercise;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class MapTest {
public static void main(String[] args) {
Map map = new HashMap();
Student s1 = new Student(1,"zhangsan",88);
Student s2 = new Student(2,"lisi",87);
Student s3 = new Student(3,"wangwu",99);
Student s4 = new Student(4,"zhaoliu",77);
map.put(s1, 1);
map.put(s2, 1);
map.put(s3, 1);
map.put(s4, 1);
System.out.println("map中元素个数是:"+map.size());
System.out.println("key为10的value值:"+map.get(10));
System.out.println(map.containsKey(true));
map.remove("hello");
map.remove(true,false);
System.out.println("map中元素个数:"+map.size());
//遍历map(1)
Set set = map.keySet();
Iterator site = set.iterator();
while (site.hasNext()) {
Object key = site.next();
Object value = map.get(key);
System.out.println(key+" = "+value);
}
System.out.println("----------------------------");
//遍历map(2)
Set kvs =map.entrySet();
Iterator kvite = kvs.iterator();
while (kvite.hasNext()) {
Map.Entry kv = (Map.Entry) kvite.next();
Object key = kv.getKey();
Object value = kv.getValue();
System.out.println(key+" = "+value);
}
System.out.println("--------------------------");
//遍历map(3)
Collection values = map.values();
for(Object value:values){
System.out.println("value:"+value);
}
}
}
最后
以上就是高兴夏天为你收集整理的Map集合的三种遍历方法的全部内容,希望文章能够帮你解决Map集合的三种遍历方法所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复