一、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、

JAVA笔记20-容器之四Map接口、自动打包、泛型(重要)的更多相关文章

  1. 4.Java集合总结系列:Map接口及其实现

    一.Map接口 Map集合的特点是:通过key值找到对应的value值,key值是唯一的,value可以重复.Map中的元素是无序的,但是也有实现了排序的Map实现类,如:TreeMap. 上面Map ...

  2. Java笔记20:迭代器模式

    迭代器模式 所谓Iterator模式,即是Iterator为不同的容器提供一个统一的访问方式.本文以Java中的容器为例,模拟Iterator的原理. 1 定义一个容器Collection接口 pub ...

  3. Java学习--list,set,Map接口使用

    list接口: 泛型:规定list中的元素的类型 /*     *      * 泛型不能使用基本数据类型(可以使用基本类型的包装类)     *      */    public void tes ...

  4. 菜鸡的Java笔记 第三十五 接口定义增强

    接口定义增强        在java从一开始到现在接口之中的核心组成部分:抽象方法与全局常量,但是随着技术的不断发展,用户在使用过程之中发现了有一些问题        如果说现在有一个接口经过了长年 ...

  5. JAVA笔记19-容器之三 Set接口、List接口、Collections类、Comparable接口(重要)

    一.Set接口 //HashSet综合举例 import java.util.*; public class Test{ public static void main(String[] args){ ...

  6. Java笔记(十四)……抽象类与接口

    抽象类概念 抽象定义: 抽象就是从多个事物中将共性的,本质的内容抽取出来. 例如:狼和狗共性都是犬科,犬科就是抽象出来的概念. 抽象类: Java中可以定义没有方法体的方法,该方法的具体实现由子类完成 ...

  7. Thinking in Java——笔记(20)

    Annotations They provide information that you need to fully describe your program, but that cannot b ...

  8. JAVA中Collection接口和Map接口的主要实现类

    Collection接口 Collection是最基本的集合接口,一个Collection代表一组Object,即Collection的元素(Elements).一些Collection允许相同的元素 ...

  9. Java集合框架之Map接口浅析

    Java集合框架之Map接口浅析 一.Map接口综述: 1.1java.util.Map<k, v>简介 位于java.util包下的Map接口,是Java集合框架的重要成员,它是和Col ...

随机推荐

  1. 自己用canvas写的贪吃蛇代码

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. C#学习笔记一(概念,对象与类型,继承)

    一.基础 1.CLR为公共语言运行库,类似于JVM 2..NET Framwork是一个独立发布的程序包,其包含了CLR,类库及相关的语言编辑器等工具,类似于JDK,除了C#,还有其他几种语言在CLR ...

  3. SpringBoot项目集成cas单点登录

    添加依赖 添加cas client依赖 <dependency> <groupId>net.unicon.cas</groupId> <artifactId& ...

  4. python 三元运算、列表推倒式、字典推倒式、生成器生成式

    1.三元运算 name=input('姓名>>: ') res='SB' if name == 'alex' else 'NB' print(res) 2.列表推倒式 #1.示例 egg_ ...

  5. HDU 1069 Monkey and Banana (动态规划、上升子序列最大和)

    Monkey and Banana Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  6. Codeforces Round #587 (Div. 3)

    https://codeforces.com/contest/1216/problem/A A. Prefixes 题意大概就是每个偶数位置前面的ab数目要相等,很水,被自己坑了 1是没看见要输出修改 ...

  7. python爬虫常用第三方库

    这个列表包含与网页抓取和数据处理的Python库 网络 通用 urllib -网络库(stdlib). requests -网络库. grab – 网络库(基于pycurl). pycurl – 网络 ...

  8. Python中字典合并的四种方法

    字典是Python语言中唯一的映射类型.映射类型对象里哈希值(键,key)和指向的对象(值,value)是一对多的的关系,通常被认为是可变的哈希表.字典对象是可变的,它是一个容器类型,能存储任意个数的 ...

  9. 用SQL存储过程生成唯一单据号

    用SQL存储过程生成唯一单据号     在一些系统中,经理要生成单据号,为了不使多台客户端生成的单据号重复,一般要在服务端生成这种流水号,本文是在数据库中生成流水号,并且可以生成多种类型的单据号(比如 ...

  10. 34、Scrapy 知识总结

      Scrapy 知识总结   1.安装   pip install wheel pip install https://download.lfd.uci.edu/pythonlibs/q5gtlas ...