黑马程序员——Map、HashMap、TreeMap

来源:互联网 发布:淘宝乐高霍比特人玩具 编辑:程序博客网 时间:2024/05/17 01:48



    Map:一次添加一对元素,Collection一次添加一个元素。
    Map也称为双列集合,Collection集合称为单列集合。
    其实Map集合中存储的就是键值对。
    map集合中必须保证键的唯一性。


    常用方法:
    1、添加
    value put(key,value):返回前一个和key关联的值,如果没有返回null

    2、删除
    void clear():清空map集合。
    value remove(Object key):根据指定的key删除这个键值对。

    3、判断
    boolean containsKey(key);
    boolean containsValue(value);
    boolean isEmpty();

    4、获取
    value get(key):通过键获取值,如果没有该键返回null
                            当然可以通过返回null,来判断是否包含指定键。
    int size():获取键值对个数。

    示例1

import java.util.HashMap;

import java.util.Map;

 

public class MapDemo{

       public static void main(String[] args){

            Map<Integer,String> map = new HashMap<Integer,String>();

             method(map);

      }

 

       public static void method(Map<Integer,String> map){ //学号和姓名

             //添加元素

            System. out.println(map.put(8,"旺财" ));

            System. out.println(map.put(8,"小强" ));

            System. out.println(map);

 

            map.put(2, "张三");

            map.put(7, "赵六");

            System. out.println(map);

 

             //删除

            System. out.println("remove:" + map.remove(2));

 

             //判断

            System. out.println("containsKey:" + map.containsKey(7));

 

             //获取

            System. out.println("get:" + map.get(7));

      }

}

 

复制代码

   运行结果:


    获取Map集合元素并打印方式一:

    示例2

import java.util.HashMap;

import java.util.Iterator;

import java.util.Map;

import java.util.Set;

 

public class MapDemo{

       public static void main(String[] args){

            Map<Integer,String> map = new HashMap<Integer,String>();

            method(map);

      }

 

       public static void method(Map<Integer,String> map){

            map.put(8, "王五");

            map.put(2, "赵六");

            map.put(7, "小强");

            map.put(6, "旺财");

 

             //取出map中的所有元素。

             //原理,通过keySet方法获取map中所有的键所在的set集合,在通过set的迭代器获取到每一个键。

             //再对每一个键通过map集合的get方法获取其对应的值即可。

 

            Set<Integer> keySet = map.keySet();

            Iterator<Integer> it = keySet.iterator();

 

             while(it.hasNext()){

                  Integer key = it.next();

                  String value = map.get(key);

                  System.out.println(key + ":" + value);

            }

      }

}

 

复制代码

    运行结果:


    获取Map集合元素并打印方式二:

    示例3

import java.util.HashMap;

import java.util.Iterator;

import java.util.Map;

import java.util.Set;

 

public class MapDemo{

       public static void main(String[] args){

            Map<Integer,String> map = new HashMap<Integer,String>();

             method(map);

      }

 

       public static void method(Map<Integer,String> map){

            map.put(8, "王五");

            map.put(2, "赵六");

            map.put(7, "小强");

            map.put(6, "旺财");

 

             /*

            通过Map转成Set就可以迭代。

            找到了另一个方法,entrySet

            该方法将键和值的映射关系作为对象存储到了Set集合中,而这个映射关系的类型就是Map.Entry类型

            */

            Set<Map.Entry<Integer,String>> entrySet = map.entrySet();

 

            Iterator<Map.Entry<Integer,String>> it = entrySet.iterator();

 

             while(it.hasNext()){

                  Map.Entry<Integer,String> me = it.next();

                  Integer key = me.getKey();

                  String value = me.getValue();

                  System. out.println(key + ":" + value);

            }

      }

}

 

复制代码

   运行结果:


    获取Map集合元素并打印方式三:

    示例4

import java.util.Collection;

import java.util.HashMap;

import java.util.Iterator;

import java.util.Map;

 

public class MapDemo{

       public static void main(String[] args){

            Map<Integer,String> map = new HashMap<Integer,String>();

             method(map);

      }

 

       public static void method(Map<Integer,String> map){

            map.put(8, "王五");

            map.put(2, "赵六");

            map.put(7, "小强");

            map.put(6, "旺财");

 

            Collection<String> values = map.values();

 

            Iterator<String> it = values.iterator();

             while(it.hasNext()){

                  System. out.println(it.next());

            }

      }

}

 

复制代码

    运行结果:


    Map常用的子类:
          |--Hashtable:内部结构是哈希表,是同步的。不允许null作为键,null作为值。
               |--Properties:用来存储键值对型的配置文件的信息,可以和IO技术相结合。
          |--HashMap:内部结构式哈希表,不是同步的。允许null作为键,null作为值。
          |--TreeMap:内部结构式二叉树,不是同步的。可以对Map结合中的键进行排序。

    hashSet实现Set接口,由哈希表(实际上是一个HashMap实例)支持。

    示例5

import java.util.HashMap;

import java.util.Iterator;

 

class Student {

       private String name;

       private int age;

 

       public Student(){

      }

 

       public Student(String name,int age){

             this.name = name;

             this.age = age;

      }

 

       public void setName(String name){

             this.name = name;

      }

 

       public String getName(){

             return this .name;

      }

 

       public void setAge(int age){

             this.age = age;

      }

 

       public int getAge(){

             return this .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 ;

            Student other = (Student) 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 ;

      }

}

 

public class HashMapDemo{

       public static void main(String[] args){

             //将学生对象和学生的归属地通过键与值存储到map集合中

            HashMap<Student,String> hm = new HashMap<Student,String>();

 

            hm.put( new Student("lisi" ,38),"北京");

            hm.put( new Student("zhaoliu" ,24),"上海");

            hm.put( new Student("xiaoqiang" ,31),"沈阳");

            hm.put( new Student("wangcai" ,28),"大连");

            hm.put( new Student("zhaoliu" ,24),"铁岭");

            

            Iterator<Student> it = hm.keySet().iterator();

 

             while(it.hasNext()){

                  Student key = it.next();

                  String value = hm.get(key);

                  System.out.println(key.getName() + ":" + key.getAge() + "---" + value);

            }

      }

}

 

复制代码

   运行结果:


    P.S.
    键有了判断依据,HashMap中的值就被覆盖。

    示例6

import java.util.Comparator;

import java.util.Iterator;

import java.util.Map;

import java.util.TreeMap;

 

class ComparatorByName implements Comparator<Student>{

       public int compare(Student s1,Student s2){

             int temp = s1.getName().compareTo(s2.getName());

             return temp == 0?s1.getAge() - s2.getAge():temp;

      }

}

 

public class HashMapDemo{

       public static void main(String[] args){

             //将学生对象和学生的归属地通过键与值存储到map集合中

            TreeMap<Student,String> tm = new TreeMap<Student,String>(new ComparatorByName());

 

            tm.put( new Student("lisi" ,38),"北京");

            tm.put( new Student("zhaoliu" ,24),"上海");

            tm.put( new Student("xiaoqiang" ,31),"沈阳");

            tm.put( new Student("wangcai" ,28),"大连");

            tm.put( new Student("zhaoliu" ,24),"铁岭");

            

            Iterator<Map.Entry<Student,String>> it = tm.entrySet().iterator();

 

             while(it.hasNext()){

                  Map.Entry<Student,String> me = it.next();

                  Student key = me.getKey();

                  String value = me.getValue();

                  System.out.println(key.getName() + ":" + key.getAge() + "---" + value);

            }

      }

}

 

复制代码

   运行结果:

 

0 0
原创粉丝点击