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

最新文章

  1. Canvas 教程
  2. linux ext2 文件系统学习
  3. BZOJ 2229 最小割
  4. OC8_NSData
  5. Oracle事物基础
  6. C/C++用匿名数据结构实现时间和空间名利双收
  7. JS浏览器对象:window对象、History、Location对象、Screen对象
  8. sublime markdown编辑配色
  9. Redis之过期时间
  10. yum centos 修改镜像源
  11. bash中 2&gt;&amp;1 &amp; 的解释
  12. bootstrap 带有确定取消按钮的modal
  13. mysql 字符串函数、分组函数
  14. bzoj5127[Lydsy12月赛]数据校验
  15. JSR303验证
  16. SQL Server -&gt;&gt; SQL Server 2016新特性之 -- sp_set_session_context存储过程和SESSION_CONTEXT函数
  17. AngularJS(五)——filter(过滤器)
  18. contest0 from codechef
  19. 深入浅出:了解JavaScript的六种继承
  20. ISO 8 自适应cell

热门文章

  1. RTX数据表分析
  2. Log的相关用法
  3. MySQL -2- 体系结构
  4. rk3288 编译应用程序
  5. 设计模式之单例模式(Singleton Pattern)
  6. 【转载】jquery版的网页倒计时效果
  7. java复习(1)面向对象
  8. 2019-11-29-win10-uwp-轻量级-MVVM-框架入门-2.1.5.3199
  9. SOA架构简介
  10. docker search - 搜寻镜像