一、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. C#使用NPOI读写excel

    本帖内容来自网络+自己稍作整理,已找不到原贴,侵删 个人比较习惯用NPOI操作excel,方便易理解.在宇宙第一IDE(笑)——VS2017中插入NPOI就很方便: 首先安装NPOI: 然后在.cs文 ...

  2. IOS CocoaPods详细使用方法

    自从有了CocoaPods以后,这些繁杂的工作就不再需要我们亲力亲为了,只需要我们做好少量的配置工作,CocoaPods会为我们做好一切   一.什么是CocoaPods 1.为什么需要CocoaPo ...

  3. spring5的基本组成(6个模块)

    1:数据访问及集成(Data Access/Integeration):jdbc,orm,oxm,jms,transactions ——由 spring-jdbc.spring-tx.spring-o ...

  4. heartbeat高可用

    一.基本了解 1.Hearbeat和keepalived区别Keepalived使用的vrrp协议方式,虚拟路由冗余协议 (Virtual Router Redundancy Protocol,简称V ...

  5. Nginx配置反向代理与负载均衡

    Nginx的upstream目前支持的分配算法: 1.round-robin 轮询1:1轮流处理请求(默认) 每个请求按时间顺序逐一分配到不同的应用服务器,如果应用服务器down掉,自动剔除,剩下的继 ...

  6. 简述Vue中使用Vuex

    1.为什么要用vuex 在vue组件通信的过程中,我们通信的目的往往就是在组件之间传递数据或组件的状态(这里将数据和状态统称为状态),进而更改状态.但可以看到如果我们通过最基本的方式来进行通信,一旦需 ...

  7. Excel透视表进阶之计算字段、计算项、切片器、页面布局

    计算字段 在透视表的字段列表中通过函数.公式等方式构建一个新的字段 又称虚拟字段,因为计算字段不会出现在数据源中,对于普通字段的操作,都可以对计算字段进行操作 计算字段只能出现在值区域,不能出现在筛选 ...

  8. <form:select>

    <form:select path="classification" class="input-medium"> <form:option v ...

  9. 解决Asp.net Core中chtml文档中文乱码的问题

    原因 由于Visual Studio 2017在保存chtml时,文本格式非utf-8,所以导致中文会出现乱码情况. 解决办法 在工具->扩展与更新中添加插件"ForceUTF8 (w ...

  10. django Paginator 让分页变得完美

    参考大佬地址:https://www.zmrenwu.com/courses/django-blog-tutorial/materials/21/ 类视图 from django.contrib.au ...