一、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. python3.5 append使用

    1.从元组中添加 friends=[] tup1=("Jon",35) friends.append(tup1[0]) print(friends[0]) ssh://root@1 ...

  2. js获取当天时间,7天前后时间,时间格式化

    格式化时间年月日时分秒 //时间戳转换方法 date:时间戳数字 formatDate(date) { var date = new Date(date); var YY = date.getFull ...

  3. Egret入门学习日记 --- 第七篇(书中 3.9节 内容)

    第七篇(书中 3.9节 内容) 好,今天就来看下 3.9节 的内容. 第一点: 昨天就已经搞定了. 第二点: 也包括在昨天的内容了. 第三点: 如果在构造函数里直接引用组件,就会挂掉. 但是把位置变化 ...

  4. Python学习之数据库

    9.6 表的查询 [结构]select distinct 字段1,字段2 from 表名 where 条件 group by 字段 having 筛选 order by 字段 limit 限制条数 [ ...

  5. idea运行时 Process finished with exit code -1073741819 (0xC0000005)

    问题描述:        idea中启动项目报   Process finished with exit code -1073741819 (0xC0000005) ,如图所示: 问题解决:      ...

  6. Redis 3主-3从集群的搭建(CentOS 7)

    注意ip地址为: 虚拟机ip设置 TYPE="Ethernet"BOOTPROTO="static"NAME="enp0s3"DEVICE= ...

  7. Git介绍、安装、命令和实战

    一.Git介绍 Git是一个开源的分布式版本控制系统,可以有效.高速地处理从很小到非常大的项目版本管理. 二.Git安装(Mac系统) 在Git官网下载安装包双击直接安装 在终端输入git来检测Git ...

  8. HDU-4507-吉哥系列故事-恨7不成妻

    题目描述 单身! 依然单身! 吉哥依然单身! DS级码农吉哥依然单身! 所以,他生平最恨情人节,不管是214还是77,他都讨厌! 吉哥观察了214和77这两个数,发现: 2+1+4=7 7+7=7*2 ...

  9. PyQt5_主要的类库

    1.PyQt5包括的主要模块如下. QtCore模块——涵盖了包的核心的非GUI功能,此模块被用于处理程序中涉及的时间.文件.目录.数据类型.文本流.链接.QMimeData.线程或进程等对象. Qt ...

  10. C - 卿学姐与诡异村庄(并查集+One face meng bi)

    卿学姐与诡异村庄 Time Limit: 4500/1500MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit  ...