JAVA笔记20-容器之四Map接口、自动打包、泛型(重要)

一、Map接口

  Map提供的是key到value的映射。key不能重复,每个key只能映射一个value

注:重复是指equals,但用equals()方法效率低,所以此处用hashCode()方法比较

  key还决定了存储对象在映射中的存储位置,不是由key本身决定,而是通过一种散列技术处理产生的散列码,该散列码作为偏移量,对应分配给映射的内存区域的起始位置。Map集合包括Map接口和Map接口的所有实现类。

  Map接口中提供了集合的常用方法:

举例1:Map接口中方法的使用

import java.util.*;  //导入java.util包
public class Test{
    public static void main(String args[]){
        Map m1 = new HashMap();
        Map m2 = new TreeMap();
        m1.put("one",new Integer(1));
        m1.put("two",new Integer(2));
        m1.put("three",new Integer(3));
        m2.put("one",new Integer(2));
        m2.put("B",new Integer(2));
        System.out.println(m1.size());
        System.out.println(m1.containsKey("one"));
        System.out.println(m2.containsValue(new Integer(1)));
        if(m1.containsKey("two")){
            int i = ((Integer)m1.get("two")).intValue();
            System.out.println(i);
        }
        Map m3 = new HashMap(m1);
        m3.putAll(m2);
        System.out.println(m3);
    }
}

输出:

3
true
false
2
{two=2, B=2, one=2, three=3}

举例2:

import java.util.*;  //导入java.util包
public class Test{
    public static void main(String args[]){
        Map m1 = new HashMap();
        Map m2 = new TreeMap();
        m1.put("one",new Integer(1));
        m1.put("two",new Integer(2));
        m1.put("three",new Integer(3));
        m1.put("four",null);
        Set s = m1.keySet();
        Iterator t = s.iterator();
        System.out.println("key集合中的元素");
        while(t.hasNext()){
            System.out.println(t.next());
        }
        Collection c = m1.values();
        t = c.iterator();
        System.out.println("value集合中的元素");
        while(t.hasNext()){
            System.out.println(t.next());
        }
    }
}

输出:

key集合中的元素
two
one
three
four
value集合中的元素
2
1
3
null

  Map接口的实现类有HashMap(哈希表)和TreeMap(二叉树)等。建议使用HashMap实现类实现Map集合,因为由HashMap类实现的Map集合对于添加和删除映射关系效率更高。如果希望Map集合中的对象存在一定的顺序,应使用TreeMap。

1、HashMap类:基于哈希表的Map接口的实现。提供所有可选的映射操作,并允许使用null值和null键,但必须保证键的唯一性。不保证映射顺序,特别是不保证映射顺序恒久不变。

2、TreeMap类:不仅实现了Map接口,还实现了java.util.SortedMap接口。集合中的映射关系存在一定的顺序。在添加、删除和定位映射关系上,比HashMap类性能差一些。不允许键对象为null

补充:可以通过HashMap类创建Map集合,当需要顺序输出时,再创建一个完成相同映射关系的TreeMap类实例。

举例:

二、自动打包、解包(Auto-boxing/unboxing)(JDK1.5之后支持)

  在合适的时机自动打包、解包:自动将基础类型转换为对象,自动将对象转换为基础类型。

举例1:

import java.util.*;  //导入java.util包
public class Test{
    public static void main(String args[]){
        Map m1 = new HashMap();
        Map m2 = new TreeMap();
        //m1.put("one",new Integer(1));
        //m1.put("two",new Integer(2));
        //m1.put("three",new Integer(3));
        m1.put("one",1);
        m1.put("two",2);
        m1.put("three",3);
        //m2.put("one",new Integer(1));
        //m2.put("B",new Integer(2));
        m2.put("one",2);
        m2.put("B",2);
        System.out.println(m1.size());
        System.out.println(m1.containsKey("one"));
        //System.out.println(m2.containsValue(new Integer(1)));
        System.out.println(m2.containsValue(1));
        if(m1.containsKey("two")){
            //int i = ((Integer)m1.get("two")).intValue();
            int i = (Integer)m1.get("two");
            System.out.println(i);
        }
        Map m3 = new HashMap(m1);
        m3.putAll(m2);
        System.out.println(m3);
    }
}

 

输出:

3
true
false
2
{two=2, B=2, one=2, three=3}

举例2:

自动打包的写法:

三、JDK1.5泛型(Generic)

1、起因:JDK1.4以前的类型不明确:装入集合的类型都被当做Object对待,从而失去自己的实际类型;从集合中取出时往往需要转型,效率低,容易出错。

2、解决办法:在定义集合的时候同时定义集合中对象的类型(凡是遇到集合的时候尽量使用泛型)。可以在定义Collection的时候指定,也可以在循环时用Iterator指定。

3、好处:增强程序的可读性和稳定性。

举例:第5行指定只能存String。什么时候能在类后指定<>中的类型呢?查看API文档中如果类定义为如下形式则可以:Class ArrayList<E>,Interface Comparable<T> 它的方法 int compareTo(T o)。

4、List<? extends T>和List<? super T>的区别:前者T和T的子类;后者T和T的父类;

 

自动打包与泛型结合,修改前面的:

1、Interface Map<K,V>

import java.util.*;  //导入java.util包
public class Test{
    public static void main(String args[]){
        //Map m1 = new HashMap();
        //Map m2 = new TreeMap();
        Map<String,Integer> m1 = new HashMap<String,Integer>();
        Map<String,Integer> m2 = new TreeMap<String,Integer>();
        //m1.put("one",new Integer(1));
        //m1.put("two",new Integer(2));
        //m1.put("three",new Integer(3));
        m1.put("one",1);
        m1.put("two",2);
        m1.put("three",3);
        //m2.put("one",new Integer(1));
        //m2.put("B",new Integer(2));
        m2.put("one",2);
        m2.put("B",2);
        System.out.println(m1.size());
        System.out.println(m1.containsKey("one"));
        //System.out.println(m2.containsValue(new Integer(1)));
        System.out.println(m2.containsValue(1));
        if(m1.containsKey("two")){
            //int i = ((Integer)m1.get("two")).intValue();
            //int i = (Integer)m1.get("two");
            int i = m1.get("two");//使用泛型后不需要强制转型了!
            System.out.println(i);
        }
        Map m3 = new HashMap(m1);
        m3.putAll(m2);
        System.out.println(m3);
    }
}

2、

 

posted @ 2014-04-18 10:56  seven7seven  阅读(849)  评论(1编辑  收藏  举报