Java---29---Map中元素的两种取出方式
Map中是没有迭代器的,那么Map中的元素是如何取出的呢?
有两种方法:
一种是通过Set <k> keySet ()
一种是通过 Set <Map.Entry<k,v>> entrySet ()
keySet:: 将map中所有的键存入到Set集合中。在通过map的getKey ()方法即可获得 value的值
entrySet: 返回此映射中包含的映射关系的Set视图。既包含键也包含值。 这个映射关系的数据类型就是Map.Entry.
public class MapDemo { public static void main(String[] args) { // TODO Auto-generated method stub keySet_method(); entrySet_method(); } public static void entrySet_method() { Map<String, String> map = new HashMap<String, String>(); map.put("01", "a5"); map.put("02", "a1"); map.put("03", "a2"); map.put("04", "a3"); map.put("05", "a4"); Set<Map.Entry<String, String>> entryset = map.entrySet(); Iterator<Map.Entry<String, String>> it = entryset.iterator(); while (it.hasNext()) { Map.Entry<String, String> me = it.next(); String key = me.getKey(); String value = me.getValue(); System.out.println("key : " + key + " Value : " + value); } } public static void keySet_method() { Map<String, String> map = new HashMap<String, String>(); map.put("01", "a5"); map.put("02", "a1"); map.put("03", "a2"); map.put("04", "a3"); map.put("05", "a4"); Set<String> keyset = map.keySet(); Iterator<String> it = keyset.iterator(); while (it.hasNext()) { // 获取键 String key = it.next(); String value = map.get(key); System.out.println("key : " + key + " Value : " + value); } } }
存入自定义元素,通过两种方式来取出:
class Stu implements Comparable<Stu> { private String name; private int age; public Stu(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public boolean equals(Object obj) { // TODO Auto-generated method stub if (!(obj instanceof Stu)) throw new ClassCastException("不是本类的对象"); Stu s = (Stu) obj; return this.name.equals(s.name) && this.age == s.age; } @Override public int hashCode() { // TODO Auto-generated method stub return name.hashCode() + age * 27; } public int compareTo(Stu o) { // TODO Auto-generated method stub Integer num = new Integer(this.age).compareTo(new Integer(o.age)); if (num == 0) return this.name.compareTo(o.name); return num; } } public class MapDemo2 { public static void main(String[] args) { // TODO Auto-generated method stub Map<Stu, String> map = new HashMap<Stu, String>(); map.put(new Stu("01", 10), "beijing"); map.put(new Stu("02", 9), "shanghai"); map.put(new Stu("03", 8), "huangzhou"); map.put(new Stu("04", 15), "tianjin"); Set<Stu> keySet = map.keySet(); Iterator<Stu> it = keySet.iterator(); while (it.hasNext()) { Stu key = it.next(); String value = map.get(key); System.out.println("key : " + key.getName() + "---" + key.getAge() + " Value : " + value); } /*************************************************/ Set<Map.Entry<Stu, String>> entrySet = map.entrySet(); Iterator<Map.Entry<Stu, String>> iterator = entrySet.iterator(); while (iterator.hasNext()) { Map.Entry<Stu, String> me = iterator.next(); Stu key = me.getKey(); String value = me.getValue(); System.out.println("key : " + key.getName() + "---" + key.getAge() + " Value : " + value); } } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。