• Map接口


    Map接口中的常用方法
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Set;
    //Map接口
    public class Demo01 {
        public static void main(String[] args) {
            Map<String,String> map=new HashMap<String,String>();
            //添加元素
            map.put("a", "1");
            map.put("b", "2");
            map.put("a", "3");//key键不能重复   如果重复会覆盖值            值可以重复
            map.put("c", "1");
            /*String v=map.remove("b");
            //查询元素
            System.out.println(map.get("a"));
            System.out.println(map.get("b"));
            System.out.println(map.get("c"));*/
    Map集合遍历键找值方式
            //1.通过keyset遍历map
            //增强for
            Set<String> keys=map.keySet();
            for(String key:keys){
                System.out.print(key);//取key
                System.out.println(map.get(key));//取值
            }
            //Interator迭代器迭代
            Set<String> key1=map.keySet();
            Iterator<String> it=key1.iterator();
            while(it.hasNext()){
                String key=it.next();
                String value=map.get(key);
                System.out.println(key+value);
            }
    Map集合遍历键值对方式
            //2.entrySet方法遍历
            //增强for
            Set<Map.Entry<String, String>> entrys=map.entrySet();
            for(Map.Entry<String, String> entry:entrys){
                String key=entry.getKey();
                String value=entry.getValue();
                System.out.println(key+value);
            }
            //Interator迭代器迭代
            Set<Map.Entry<String, String>> entry1=map.entrySet();
            Iterator<Map.Entry<String, String>> it1=entry1.iterator();
            while(it1.hasNext()){
                //获取每一张结婚证
                Map.Entry<String, String> entry=it1.next();
                String key=entry.getKey();//从结婚证上获取key
                String value=entry.getValue();//从结婚证上获取值
                System.out.println(key+value);
            }
        }
    }
    HashMap存储自定义类型键值
    先建一个Person类
    public class Person {
        private String name;
        private int 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;
        }
        public Person(){}
        public Person(String name, int age) {
            super();
            this.name = name;
            this.age = age;
        }
        public String toString() {
            return "Person [name=" + name + ", age=" + age + "]";
        }
        
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + age;
            result = prime * result + ((name == null) ? 0 : name.hashCode());
            return result;
        }
        
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            Person other = (Person) obj;
            if (age != other.age)
                return false;
            if (name == null) {
                if (other.name != null)
                    return false;
            } else if (!name.equals(other.name))
                return false;
            return true;
        }
        
    }
    
    import java.util.HashMap;
    import java.util.LinkedHashMap;
    import java.util.Map;
    import java.util.Set;
    
    public class Demo02 {
        public static void main(String[] args) {
            method1();
            method2();
            method3();
        }
        public static void method1(){
            Map<String,Person> map=new HashMap<String,Person>();
            map.put("2", new Person("帅涛",18));
            map.put("1", new Person("狗炜",19));
            map.put("3", new Person("小菜",19));
            map.put("4", new Person("小明",19));
            map.put("4", new Person("小明",19));
            //遍历
            Set<String> set=map.keySet();
            for(String s:set){                     
                System.out.println(s+map.get(s));
            }                                     
        }
    //自定义类型当key键时需要重写hashCode和equals方法
        public static void method2(){
            Map<Person,String> map=new HashMap<Person,String>();
            map.put(new Person("帅涛",18),"1" );
            map.put(new Person("狗炜",19),"2" );
            map.put(new Person("小菜",19),"3");
            map.put(new Person("小明",19),"4" );
            map.put(new Person("小明",19),"4" );
            //遍历
            Set<Person> set=map.keySet();
            for(Person s:set){                    
                System.out.println(s+map.get(s));
            }                                     
        }
    //有序遍历 
        public static void method3(){
            Map<Person,String> map=new LinkedHashMap<Person,String>();
            map.put(new Person("帅涛",18),"2" );
            map.put(new Person("狗炜",19),"1" );
            map.put(new Person("小菜",19),"3");
            map.put(new Person("小明",19),"4" );
            map.put(new Person("小明",19),"4" );
            //遍历
            Set<Person> set=map.keySet();
            for(Person s:set){                     
                System.out.println(s+map.get(s));
            }                                     
        }                                        
    }
     
  • 相关阅读:
    全国各地DNS(电信,移动,联通,教育网)
    全国各地电信DNS服务器地址
    免费公共 DNS 大全
    免费公共DNS服务器 全球DNS服务器 国内DNS服务器
    AT指令发送短信
    OpenWrt 定期检测进程是否存在并重启
    OpenWrt 计划任务使用方法
    openwrt-mt7688 修改默认的WIFI SSID
    在MT7628实现wan、wwan与4G动态切换
    Unix常用命令之修改密码
  • 原文地址:https://www.cnblogs.com/zhaotao11/p/10237512.html
Copyright © 2020-2023  润新知