Map:顶层接口,该集合存储的是键值对,而且键是唯一的,Map和Set很像,Set集合底层就是使用了Map集合。
Map集合没有迭代器,要取出元素必须先将Map集合转换成Set集合才能遍历元素
|--->HashTable(JDK1.0):
底层是哈希表数据结构;
不可以使用null键和null值;
用作键的对象必须实现hashCode和equals方法来保证键的唯一性
线程同步,效率低
|--->HashMap(JDK1.2):
底层是哈希表数据结构;
允许使用null键和null值;
线程不同步,效率高;
保证元素唯一性的:
原理:先判断元素的hashCode值是否相同,再判断两元素的equals方法是否为true
(往HashSet里面存的自定义元素要复写hashCode和equals方法,
以保证元素的唯一性!)
class Student {
private String name;
private int age;
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int hashCode(){
return name.hashCode()+age*34;
}
@Override
public boolean equals(Object obj){
if(!(obj instanceof Student))
return false;
Student stu = (Student)obj;
return this.name.equals(stu.name)&&this.age==stu.age;
}
public class HashMapDemo1 {
public static void main(String[] args) {
Map<Student , String> hmap = new HashMap<Student , String>();
hmap.put(new Student("001",20), "beijing");
hmap.put(new Student("002",25), "hebei");
hmap.put(new Student("003",50), "hainan");
hmap.put(new Student("001",20), "beijing");
System.out.println(hmap.size());
Set<Student> keySet = hmap.keySet();
Iterator<Student> it = keySet.iterator();
while(it.hasNext()){
Student stu = it.next();
String addr = hmap.get(stu);
System.out.println(stu.getName()+".."+stu.getAge()+"::"+addr);
}
}
}
|--->TreeMap(JDK1.0):
底层是二叉树结构;
允许使用null键和null值;
线程不同步;
可以给Map集合中的键进行排序.
TreeMap排序的第一种方式:让元素自身具备比较性,比如八种基本数据类型或则字符串,
实现Compareble接口,覆盖compareTo方法,
此方式是元素的自然顺序
TreeMap排序的第一种方式:当元素自身不具备比较性(比如存储学生对象时)或者具备的
比较性不是我们所需要的比较性时(比如想字符串的长度排序),
此时就需要让集合自身具备自定义的比较性。
那如何让集合自身具备比较性呢?可在集合初始化时,
就让集合具备比较方式。即定义一个类,
实现Comparator接口,覆盖compare方法。
class Student implements Comparable<Student>{
private String name;
private int age;
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int compareTo(Student stu) {
int num = new
Integer(this.age).compareTo(new Integer(stu.age));
if(num==0)
return this.name.compareTo(stu.name);
return num;
}
}
public class HashMapDemo1 {
public static void main(String[] args) {
Map<Student , String> tmap = new TreeMap<Student , String>();
tmap.put(new Student("001",20), "beijing");
tmap.put(new Student("002",25), "hebei");
tmap.put(new Student("003",50), "hainan");
tmap.put(new Student("001",20), "beijing");
System.out.println(tmap.size());
Set<Student> keySet1 = tmap.keySet();
Iterator<Student> it1 = keySet1.iterator();
while(it1.hasNext()){
Student stu = it1.next();
String addr = tmap.get(stu);
System.out.println(stu.getName()+".."+stu.getAge()+"::"+addr);
}
}
}
**Iterator:对collection进行迭代的迭代器.迭代器取代了Enumeration。
迭代器和枚举的区别:
迭代器允许调用者利用定义良好的语义在迭代期间从迭代器所指向的collection移除元素
方法名称得到了改进,简化书写
**LisIterator:系列表迭代器,允许程序员按任一方向遍历列表、迭代期间修改列表
**Comparable:此接口强行对实现它的每个类的对象进行整体自然排序。使元素具备比较性
**Comparator:强行对某个对象collection进行整体排序的比较函数,使集合具备比较性
**Collections:此类完全由在 collection 上进行操作或返回 collection 的静态方法组成。
**Arrays:此类包含用来操作数组(比如排序和搜索)的各种静态方法
------------------------------------------------------------------------------------------------------
package map;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/*2015年10月25日 15:21:05
* Map接口,是不是继承Collection接口,NO,没有关系
*
* Map集合,映射键值对,集合中不能有重复的键,每个键只能映射一个值
*
* K,存储的键的泛型
* V,存储的值的泛型
* Map接口中的方法
* Map接口 = HashMap实现类
* Key,用作键的泛型,Value,用作值的泛型
*/
public class MapDemo {
public static void main(String[] args) {
method1();
}
/*
* values方法,将Map集合中所有的值
* 存储到一个Collection集合中
* Collection<V>values()
*/
public static void method_4()
{
Map<Integer,String> map=new HashMap<Integer,String>();
/* V put(K key, V value)
将指定的值与此映射中的指定键关联(可选操作)。*/
map.put(1, "君威");
map.put(1, "君威");
map.put(2, "宝马");
map.put(3, "卡宴");
//调用集合方法values,将所有的值,存储到Collection集合
Collection <String> coll=map.values();
// System.out.println("coll=="+coll.getClass());//coll==class java.util.HashMap$Values
Iterator<String> it=coll.iterator();
while(it.hasNext())
{
System.out.println(it.next());
/* 君威
宝马
卡宴
唯一性
*/
}
}
//map覆盖
public static void method_3()
{
Map<Integer,String> map=new HashMap<Integer,String>();
map.put(1, "君威");
map.put(1, "君威");
map.put(2, "宝马");
map.put(1, "卡宴");
System.out.println(map);
// {1=卡宴, 2=宝马}后来的会覆盖前面的
}
/*
* Map集合中的移除方法
* 根据键,移除整个键值对
* Value remove(Key)
* 返回被移除之前的值
*/
public static void method_2()
{
Map<Integer,String> map=new HashMap<Integer,String>();
map.put(1, "君威");
map.put(2, "路虎");
map.put(3, "宝马");
map.put(4, "卡宴");
//调用集合方法remove进行移除键值对 3
String value=map.remove(3);
System.out.println(value);//宝马
System.out.println(map);
// {1=君威, 2=路虎, 4=卡宴}
}
/*
* Map集合判断方法
* boolean containsKey(Key)判断集合中有没有这个键
* boolean containsValue(Value)判断集合中有没有这个值
*/
public static void method_1()
{
Map<String,Integer> map=new HashMap<String,Integer>();
map.put("君威",1);
map.put("路虎",2);
map.put("宝马",3);
map.put("卡宴",4);
//调用集合方法containsKey,判断集合中没有“君威”这个键
boolean b=map.containsKey("君威");
System.out.println(b);//true
//调用集合方法containsValue,判断集合中有没有1这个值
boolean b1=map.containsValue(1);
System.out.println(b1);//true
}
/*
* Map集合中,根据键获取值的方法
* Value get(Key)
* 传递键,获取和这个键对应的值
* 如果集合中没有这个键,返回null
*/
public static void method()
{
Map<String,Integer> map=new HashMap<String,Integer>();
map.put("君威",1);
map.put("路虎",2);
map.put("宝马",3);
map.put("卡宴",4);
//调用集合方法get.传递键,获取值
Integer i=map.get("君威");
System.out.println(i);//1
}
/*
* Map集合中存储键值对对象
* V put(K,V)
* 返回值一般情况下都是null
* 如果存储了重复的键:值就被覆盖
* 返回值,返回被覆盖之前的那个值
*/
public static void method1()
{
Map<String,Integer> map=new HashMap<String,Integer>();
map.put("君威",1);
map.put("路虎",2);
map.put("宝马",3);
map.put("卡宴",4);
//调用集合的put方法存储键值对
Integer value=map.put("私人飞机", 5);
System.out.println("value=="+value);//value==null
System.out.println(map);// {卡宴=4, 君威=1, 私人飞机=5, 路虎=2, 宝马=3}
}
}