zoukankan      html  css  js  c++  java
  • java集合之Map_keySet_entrySet

    keySet()的使用:该方法返回的是一个key对象的Set<E>集合,通过该set集合的对象调用iterator方法返回一个迭代器,通过该迭代器可访问到set集合里面的key

            再调用HashMap集合里面的get(Object key);方法即可得到key值

    package collection;
    
    import java.util.*;
    
    public class MyMap2 {
    
        public static void main(String[] args) {
            Map<Person2,Contact2> m1 = new HashMap<Person2,Contact2>();
            Person2 p1 = new Person2(123,"张三");
            Person2 p2 = new Person2(12,"李四");
            Contact2 c1 = new Contact2(13199898778l,"河南");
            Contact2 c2 = new Contact2(15999898778l,"北京");
            m1.put(p1, c1);
            m1.put(p2, c2);
            Set<Person2> set1  = m1.keySet(); 
            Iterator<Person2> it1 = set1.iterator();
            while(it1.hasNext()){
                Person2    key = it1.next();
                Contact2 value = m1.get(key);
                System.out.println("key键:"+key);
                System.out.println("value值:"+value);
            }
            
        }
    
    }
    class Person2 {
        int id ;
        String name;
        public Person2(int id, String name) {
            //super();
            this.id = id;
            this.name = name;
        }
        @Override
        public String toString() {
            return "Person2 [id=" + id + ", name=" + name + "]";
        }
        
    }
    class Contact2{
        long telephont;
        String address;
        public Contact2(long telephont, String address) {
            //super();
            this.telephont = telephont;
            this.address = address;
        }
        @Override
        public String toString() {
            return "Contact2 [telephont=" + telephont + ", address=" + address + "]";
        }
    }
    View Code
    key键:Person2 [id=123, name=张三]
    value值:Contact2 [telephont=13199898778, address=河南]
    key键:Person2 [id=12, name=李四]
    value值:Contact2 [telephont=15999898778, address=北京]
    View Code

    entrySet()的使用:返回此映射中包含的映射关系的set试图——jdk1.6

              理解为返回Map.Entry<K,V>类的一个对象,K,V都是Map键值对中对应的属性,在Map.Entry中K又是该Map.Entry的一个属性,V也是Map.Entry的一个

              属性.通过返回的对象调用迭代器,才能访问到Map.Entry<K,V>里面的元素。

      代码如下:

     1 package collection;
     2 
     3 import java.util.*;
     4 
     5 
     6 public class MyMap_entrySet2 {
     7 
     8     public static void main(String[] args) {
     9         Map  m1 = new TreeMap(); 
    10         Person5 p1 = new Person5(1332,"xiaoming");
    11         Person5 p2 = new Person5(8332,"ahangsan");
    12         Contact5 c1 = new Contact5(13172673363l,"广州");
    13         Contact5 c2 = new Contact5(15172673363l,"杭州");
    14         m1.put(p1, c1);
    15         m1.put(p2, c2);
    16         //System.out.println(m1);
    17         Set<Map.Entry<Person5, Contact5>> map_entry = m1.entrySet();//获取Map.Entry的对象
    18         Iterator<Map.Entry<Person5, Contact5>> it = map_entry.iterator();//获取迭代器
    19         while(it.hasNext()){
    20         Person5 p =it.next().getKey();//通过迭代器访问Map.Entry里面的元素
    21         Contact5 c =(Contact5) m1.get(p);//key得到他的value
    22             System.out.println(p);
    23             System.out.println(c);
    24         }
    25     }
    26 
    27 }
    28 class Person5 implements Comparable {
    29     int id ;
    30     String name;
    31     public Person5(int id, String name) {
    32         //super();
    33         this.id = id;
    34         this.name = name;
    35     }
    36     public int compareTo(Object obj){
    37         Person5 p4 = (Person5)obj ;
    38             return this.name.compareTo(p4.name);//根据返回值,按照二叉树结构,进行存储。
    39     }                                            //P84《数据结构与算法分析java语言描述》佛罗里达国际大学    
    40     @Override
    41     public String toString() {
    42         return "Person5 [id=" + id + ", name=" + name + "]";
    43     }
    44     
    45 }
    46 class Contact5{
    47     long telephont;
    48     String address;
    49     public Contact5(long telephont, String address) {
    50         //super();
    51         this.telephont = telephont;
    52         this.address = address;
    53     }
    54     @Override
    55     public String toString() {
    56         return "Contact5 [telephont=" + telephont + ", address=" + address + "]";
    57     }
    58     
    59 }
    View Code
  • 相关阅读:
    JS动态计算rem
    Vue数据双向绑定原理
    NOI2019 退役记
    友情链接
    算法博客总结
    总结各类错误(always online)
    学习笔记:powerful number求积性函数前缀和
    LOJ#2409. 「THUPC 2017」小 L 的计算题 / Sum(生成函数)
    多项式简单操作
    LOJ #3103. 「JSOI2019」节日庆典
  • 原文地址:https://www.cnblogs.com/huxuebing/p/5754667.html
Copyright © 2011-2022 走看看