Map集合 

 特点: 

 该集合存储键值对,一对一对的往里存,并且键是唯一的。 

 特点:要保证map集合中键的唯一性。 


 1,添加。  

 put(key,value):当存储的键相同时,新的值会替换老的值,并将老值返回。如果键没有重复,返回null。 

 void putAll(Map); 

 2,删除。  

 void clear():清空  

 value remove(key) :删除指定键。  

 3,判断。  

 boolean isEmpty():  

 boolean containsKey(key):是否包含key  

 boolean containsValue(value) :是否包含value  

 4,取出。  

 int size():返回长度  

 value get(key) :通过指定键获取对应的值。如果返回null,可以判断该键不存在。 

当然有特殊情况,就是在hashmap集合中,是可以存储null键null值的。  
Collection values():获取map集合中的所有的值。
 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ 

 对于Map集合的取出原理:map中是没有迭代器的,collection具备迭代器,只要将map集合转成Set集合, 

可以使用迭代器了。之所以转成set,是因为map集合具备着键的唯一性, 

其实set集合就来自于map,set集合底层其实用的就是map的方法。 

有两种方式:1.keySet()方法:可以将map集合中的键都取出存放到set集合中。 

 对set集合进行迭代。迭代完成,再通过get方法对获取到的键进行值的获取。 

 
例如:
import java.util.*;

 
class Student implements Comparable<Student>{     // 是实现Comparable复写compareTo方法 

private String name; 

private int age; 

Student(String name,int age) 
{ //构造函数 

this.name = name; 

this.age = age; 

} 

public int compareTo(Student s) 
{//通过对像年龄的比较进行排序 ,之后再根据对象名字的比较排序 

int num = new Integer(this.age).compareTo(new Integer(s.age)); 

if(num == 0) 

return this.name.compareTo(s.name); 

return num; 

} 


public int hashCode(){//判断存入的对象hash地址是否相等 

return name.hashCode()+age*34; 

} 

public boolean equals(Object obj) 
{//判断存入的对象内容是否相等 

if(!(obj instanceof Student)) 

throw new ClassCastException("类型不匹配"); 


Student s = (Student)obj; 


return this.name.equals(s.name) && this.age==s.age; 

}//避免重复对象存入 


public String getName() 
{ 

return name; 

} 

public int getAge() 
{ 

return age; 

} 

public String toString() 
{ 

return name+":"+age; 

} 

} 


class MapTest{ 

public static void main(String[] args) 

{ 

HashMap<Student,String> hm = new HashMap<Student,String>();//创建HashMap集合存入指定的类型数据 

//添加数据 

hm.put(new Student("lisi", 21),"beijing"); 

hm.put(new Student("lisi", 22),"shanghai"); 

hm.put(new Student("lisi", 23),"nanjjing"); 

hm.put(new Student("lisi", 24),"wuhan"); 


//第一种取出方式 keySet 

Set<Student> keySet = hm.keySet();//将Map集合中的键转成Set集合并用泛型指定存储的类型 

Iterator<Student> it = keySet.iterator();//取出Set集合的迭代器 

while(it.hasNext()) 

{ 

Student stu = it.next();  //键值进行迭代 

String addr = hm.get(stu);//通过键的值取出Map集合中的键值 

System.out.println(stu+" : "+ addr); 

} 

} 

} 


2.通过entrySet()方法:将Map集合中的键值对转换成Set集合进行迭代,当然可以调用getKey()和getValue()分别取出键值对的值。 

例如:import java.util.*; 


class Student implements Comparable<Student>{ 

private String name; 

private int age; 

Student(String name,int age) 
{ 

this.name = name; 

this.age = age; 

} 


public int compareTo(Student s) 
{ 

int num = new Integer(this.age).compareTo(new Integer(s.age)); 

if(num == 0) 

return this.name.compareTo(s.name); 

return num; 

} 


public int hashCode(){//避免重复对象存入 

return name.hashCode()+age*34; 

} 

public boolean equals(Object obj) 
{ 

if(!(obj instanceof Student)) 

throw new ClassCastException("类型不匹配"); 


Student s = (Student)obj; 


return this.name.equals(s.name) && this.age==s.age; 

}//避免重复对象存入 


public String getName() 
{ 

return name; 

} 

public int getAge() 
{ 

return age; 

} 

public String toString() 
{ 

return name+":"+age; 

} 

} 


class MapTest 

{ 

public static void main(String[] args) 

{ 

HashMap<Student,String> hm = new HashMap<Student,String>(); 


hm.put(new Student("lisi", 21),"beijing"); 

hm.put(new Student("lisi", 22),"shanghai"); 

hm.put(new Student("lisi", 23),"nanjjing"); 

hm.put(new Student("lisi", 24),"wuhan"); 


//第二中种取出方式 entrySet 

/* 
 Entry就是Map接口中的内部接口; 

为什么要定义在Map内部呢?Entry是访问键值关系的入口,是Map的入口,访问的是map中的键值对。*/ 

Set<Map.Entry<Student,String>> entrySet = hm.entrySet();//调用Map的entrySet方法取出键值对,转换成Set集合 

Iterator<Map.Entry<Student,String>> iter = entrySet.iterator();  //取出Set集合的迭代器 

while(iter.hasNext()) 

{ 

Map.Entry<Student,String> me = iter.next(); 

System.out.println(me); //取出键值对 

} 


} 

}