Map

来源:互联网 发布:矩阵分析与计算 pdf 编辑:程序博客网 时间:2024/06/14 02:09
/*在现实生活中有些数据是以映射关系存在的,也就是成对存在的,比如:     民政局 :        键                    值        老公                老婆        身份证            人        一把要锁       锁 双列集合:-------------| Map  如果是实现了Map接口的集合类,具备的特点: 存储的数据都是以键值对的形式存在的,键不可重复,值可以重复。----------------| HashMap ----------------| TreeMap----------------| Hashtable  Map接口的方法:    添加:        put(K key, V value)         putAll(Map<? extends K,? extends V> m)     删除        remove(Object key)         clear()     获取:        get(Object key)         size()     判断:        containsKey(Object key)         containsValue(Object value)         isEmpty()  */public class Demo2 {    public static void main(String[] args) {        Map<String,String> map = new HashMap<String, String>();        //添加方法        map.put("汪峰", "章子怡");        map.put("文章", "马伊琍");        map.put("谢霆锋","张柏芝");        /*        添加        System.out.println("返回值:"+map.put("谢霆锋","黄菲"));  // 如果之前没有存在该键,那么返回的是null,如果之前就已经存在该键了,那么就返回该键之前对应 的值。        Map<String,String> map2 = new HashMap<String, String>();        map2.put("杨振宁", "翁帆");        map2.put("习总", "彭丽媛");        map.putAll(map2); // 把map2的元素添加到map集合中。        */        /*        删除        System.out.println("删除的数据是:"+map.remove("汪峰")) ;  //根据键删除一条map中的数据,返回的是该键对应 的值。        map.clear(); //清空集合中的所有数据。        */        /* 获取        System.out.println("根据指定 的键获取对应的值:"+ map.get("文章"));        System.out.println("获取map集合键值对个数:"+map.size());        判断        System.out.println("判断map集合是否包含指定的键:"+ map.containsKey("文章"));        System.out.println("判断map集合中是否包含指定 的值:"+ map.containsValue("张柏芝"));        map.clear();        System.out.println("判断map集合是否为空元素:"+ map.isEmpty());        */        System.out.println("集合的元素:"+ map);    }}
/*    迭代:        keySet()         values()         entrySet() */public class Demo3 {    public static void main(String[] args) {        Map<String,String> map = new HashMap<String, String>();        //添加方法        map.put("汪峰", "章子怡");        map.put("文章", "马伊琍");        map.put("谢霆锋","张柏芝");        map.put("成龙", "林凤娇");        /*        //map集合中遍历方式一: 使用keySet方法进行遍历       缺点: keySet方法只是返回了所有的键,没有值。         Set<String> keys = map.keySet();  //keySet() 把Map集合中的所有键都保存到一个Set类型 的集合对象中返回。        Iterator<String> it = keys.iterator();        while(it.hasNext()){            String key = it.next();            System.out.println("键:"+ key+" 值:"+ map.get(key));        }        //map集合的遍历方式二: 使用values方法进行 遍历。    缺点: values方法只能返回所有 的值,没有键。        Collection<String>  c = map.values(); //values() 把所有的值存储到一个Collection集合中返回。        Iterator<String> it = c.iterator();        while(it.hasNext()){            System.out.println("值:"+ it.next());        }        */        //map集合的遍历方式三: entrySet方法遍历。        Set<Map.Entry<String,String>>  entrys = map.entrySet();         Iterator<Map.Entry<String,String>> it = entrys.iterator();        while(it.hasNext()){            Map.Entry<String,String> entry = it.next();            System.out.println("键:"+ entry.getKey()+" 值:"+ entry.getValue());        }    }}
/*双列集合:-------------| Map  如果是实现了Map接口的集合类,具备的特点: 存储的数据都是以键值对的形式存在的,键不可重复,值可以重复。----------------| HashMap  底层也是基于哈希表实现 的。HashMap的存储原理:    往HashMap添加元素的时候,首先会调用键的hashCode方法得到元素 的哈希码值,然后经过运算就可以算出该    元素在哈希表中的存储位置。     情况1: 如果算出的位置目前没有任何元素存储,那么该元素可以直接添加到哈希表中。    情况2:如果算出 的位置目前已经存在其他的元素,那么还会调用该元素的equals方法与这个位置上的元素进行比较    ,如果equals方法返回 的是false,那么该元素允许被存储,如果equals方法返回的是true,那么该元素被视为    重复元素,不允存储。----------------| TreeMap----------------| Hashtable */class Person{    int id;    String name;    public Person(int id, String name) {        super();        this.id = id;        this.name = name;    }    @Override    public String toString() {        return  "[编号:"+this.id+" 姓名:"+ this.name+"]";    }       @Override    public int hashCode() {        return this.id;    }    @Override    public boolean equals(Object obj) {        Person p = (Person) obj;        return this.id== p.id;    }}public class Demo5 {    public static void main(String[] args) {        HashMap<Person, String> map = new HashMap<Person, String>();        map.put(new Person(110,"狗娃"), "001");        map.put(new Person(220,"狗剩"), "002");        map.put(new Person(330,"铁蛋"), "003");        map.put(new Person(110,"狗娃"), "007");  //如果出现了相同键,那么后添加的数据的值会取代之前 的值。        System.out.println("集合的元素:"+ map);    }}
/*双列集合:-------------| Map  如果是实现了Map接口的集合类,具备的特点: 存储的数据都是以键值对的形式存在的,键不可重复,值可以重复。----------------| HashMap  底层也是基于哈希表实现 的。HashMap的存储原理:    往HashMap添加元素的时候,首先会调用键的hashCode方法得到元素 的哈希码值,然后经过运算就可以算出该    元素在哈希表中的存储位置。     情况1: 如果算出的位置目前没有任何元素存储,那么该元素可以直接添加到哈希表中。    情况2:如果算出 的位置目前已经存在其他的元素,那么还会调用该元素的equals方法与这个位置上的元素进行比较    ,如果equals方法返回 的是false,那么该元素允许被存储,如果equals方法返回的是true,那么该元素被视为    重复元素,不允存储。----------------| TreeMap   TreeMap也是基于红黑树(二叉树)数据结构实现 的, 特点:会对元素的键进行排序存储。TreeMap 要注意的事项:    1.  往TreeMap添加元素的时候,如果元素的键具备自然顺序,那么就会按照键的自然顺序特性进行排序存储。    2.  往TreeMap添加元素的时候,如果元素的键不具备自然顺序特性, 那么键所属的类必须要实现Comparable接口,把键    的比较规则定义在CompareTo方法上。    3. 往TreeMap添加元素的时候,如果元素的键不具备自然顺序特性,而且键所属的类也没有实现Comparable接口,那么就必须    在创建TreeMap对象的时候传入比较器。----------------| Hashtable */class Emp {//implements Comparable<Emp>{    String name;    int salary;    public Emp(String name, int salary) {        super();        this.name = name;        this.salary = salary;    }    @Override    public String toString() {        return "[姓名:"+this.name+" 薪水:"+ this.salary+"]";    }/*    @Override    public int compareTo(Emp o) {        return this.salary - o.salary;    }*/}//自定义一个比较器class MyComparator implements Comparator<Emp>{    @Override    public int compare(Emp o1, Emp o2) {        return o1.salary - o2.salary;    }}public class Demo6 {    public static void main(String[] args) {    /*  TreeMap<Character, Integer> tree = new TreeMap<Character, Integer>();        tree.put('c',10);        tree.put('b',2);        tree.put('a',5);        tree.put('h',12);        System.out.println(tree);*/        //创建一个自定义比较器        MyComparator comparator = new MyComparator();        TreeMap<Emp, String> tree = new TreeMap<Emp, String>(comparator);        tree.put(new Emp("冰冰", 2000),"001");        tree.put(new Emp("家宝", 1000),"002");        tree.put(new Emp("习总", 3000),"003");        tree.put(new Emp("克强", 5000),"005");        tree.put(new Emp("财厚", 5000),"008");        System.out.println(tree);    }}
0 0
原创粉丝点击