分享

Java中如何取出Map集合中的元素

 Mis林桑 2017-03-23

Java的Map集合中没有迭代器,需要将其转换为set集合

/*map集合的两种取出方式:1,Set keySet:将map中所有的键存入到Set集合。因为set具备迭代器。 所有可以迭代方式取出所有的键,在根据get方法。获取每一个键对应的值。 Map集合的取出原理:将map集合转成set集合。在通过迭代器取出。2,Set> entrySet:将map集合中的映射关系存入到了set集合中, 而这个关系的数据类型就是:Map.Entry Entry其实就是Map中的一个static内部接口。 为什么要定义在内部呢? 因为只有有了Map集合,有了键值对,才会有键值的映射关系。 关系属于Map集合中的一个内部事物。 而且该事物在直接访问Map集合中的元素。*/import java.util.*;class MapDemo2 { public static void main(String[] args) { Map map = new HashMap(); map.put('02','zhangsan2'); map.put('03','zhangsan3'); map.put('01','zhangsan1'); map.put('04','zhangsan4'); //将Map集合中的映射关系取出。存入到Set集合中。 Set> entrySet = map.entrySet(); Iterator> it = entrySet.iterator(); while(it.hasNext()) { Map.Entry me = it.next(); String key = me.getKey(); String value = me.getValue(); System.out.println(key+':'+value); } /* //先获取map集合的所有键的Set集合,keySet(); Set keySet = map.keySet(); //有了Set集合。就可以获取其迭代器。 Iterator it = keySet.iterator(); while(it.hasNext()) { String key = it.next(); //有了键可以通过map集合的get方法获取其对应的值。 String value = map.get(key); System.out.println('key:'+key+',value:'+value); } */ }}/*Map.Entry 其实Entry也是一个接口,它是Map接口中的一个内部接口。interface Map{ public static interface Entry { public abstract Object getKey(); public abstract Object getValue(); }}class HashMap implements Map{ class Hahs implements Map.Entry { public Object getKey(){} public Object getValue(){} } }*/

练习题:

/*每一个学生都有对应的归属地。学生Student,地址String。学生属性:姓名,年龄。注意:姓名和年龄相同的视为同一个学生。保证学生的唯一性。1,描述学生。2,定义map容器。将学生作为键,地址作为值。存入。3,获取map集合中的元素。*/import java.util.*;class Student implements Comparable{ private String name; private int age; Student(String name,int age) { this.name = name; this.age = age; } //为了确保使用TreeSet集合存储元素,使得Student类实现Comparable接口和实现compareTo方法 public int compareTo(Student s) { int num = new Integer(this.age).compareTo(new Integer(s.age)); if(num==0) return this.name.compareTo(s.name); return num; }
	//为了确保使用HashSet集合存储元素,实现hashCode和equals方法
public int hashCode(){return name.hashCode()+age*34;}public boolean equals(Object obj){if(!(obj instanceof Student))throw new ClassCastException('类型不匹配');Student s = (Student)obj;return this.name.equals(s.name) && this.age==s.age;}public String getName(){return name;}public int getAge(){return age;}public String toString(){return name+':'+age;}}class MapTest{public static void main(String[] args) {HashMap hm = new HashMap();hm.put(new Student('lisi1',21),'beijing');hm.put(new Student('lisi1',21),'tianjin');hm.put(new Student('lisi2',22),'shanghai');hm.put(new Student('lisi3',23),'nanjing');hm.put(new Student('lisi4',24),'wuhan');//第一种取出方式 keySetSet keySet = hm.keySet();Iterator it = keySet.iterator();while(it.hasNext()){Student stu = it.next();String addr = hm.get(stu);System.out.println(stu+'..'+addr);}//第二种取出方式 entrySetSet> entrySet = hm.entrySet();Iterator> iter = entrySet.iterator();while(iter.hasNext()){Map.Entry me = iter.next();Student stu = me.getKey();String addr = me.getValue();System.out.println(stu+'.........'+addr);}}}


练习2 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多