——Java培训、Android培训、iOS培训、.Net培训、期待与您交流! ——-
Map集合:该集合存储键值对,一对一对往里存,而且要保证键的唯一性。
它的常用方法有:
1、添加:put(E key , V value)、putAll ( Map m)
2、删除:clear( ) 、remove ( key )
3、判断:containsKey ( key ) 、isEmpty ( ) 、containsValue ( value )
4、获取:get ( Object key ) 、size ( ) 、value ( ) 、entrySet ( ) 、keySet ( )
子类:
Hashtable :底层是哈希表数据结构,不可以存入null键null值,该集合线程是同步的。(它的效率比较低)
HashMap:底层是哈希表数据结构,允许使用null键null值,线程不同步的(效率比较高)
TreeMap:底层是二叉树数据结构,线程不同步,可以用于给Map集合中的键进行排序。
HashMap:
可以通过get方法的返回值来判断一个键是否存在,通过返回null来判断。
添加元素时,如果出现添加相同的键,那么后添加的值会覆盖原有键对应值,并且put方法会返回被覆盖的值。
它有两种取值方法:
第一种:Set keySet( )
将Map中所有的键存入到Set集合,因为Set具备迭代器,所有可以迭代方法取出所有的值,再根据get方法,获取每一键对应的值。
例子如下:
import java.util.*;
public class MapTest {
public static void main(String[] args){
HashMap<String,String> hm=new HashMap<String,String>();
hm.put("01", "bca");
hm.put("aa", "uio");
hm.put("bb", "ddd");
hm.put("02", "z");
Set<String> s=hm.keySet();
Iterator<String> it=s.iterator();
while(it.hasNext()){
String index=it.next();
System.out.println(index+"......"+hm.get(index));
}
}
}
第二种:Set entrySet( )
将Map集合中的映射关系存入到Set集合中,而这个关系的数据类型就是Map. Entry ,再通过它的getKey( ) 和 getValue( ) 方法取值。
其实Entry也是一个接口,它是Map接口中的一个内部接口。
例子如下:
import java.util.*;
public class MapTest2 {
public static void main(String[] args){
HashMap<Integer,String> hm=new HashMap<Integer,String>();
hm.put(1, "bca");
hm.put(25, "uio");
hm.put(7, "ddd");
hm.put(6, "z");
Set<Map.Entry<Integer, String>> entrySet=hm.entrySet();
Iterator<Map.Entry<Integer, String>> it=entrySet.iterator();
while(it.hasNext()){
Map.Entry<Integer, String> me=it.next();
int key=me.getKey();
String value=me.getValue();
System.out.println(key+"------"+value);
}
}
}
TreeMap: 要对Map集合进行排序时,可以用TreeMap,它的原理跟TreeSet差不多。
例子如下:
import java.util.*;
class MapTest3{
public static void main(String[] args){
TreeMap<Doctor,String> tm=new TreeMap<Doctor,String>(new Lencomp());
tm.put(new Doctor("陈医生aa",30),"北京" );
tm.put(new Doctor("王医师",23), "深圳");
tm.put(new Doctor("方护士bbbbb",25), "恩平");
tm.put(new Doctor("陈医生aa",50),"北京" );
//第一种取值方式:
// Set<Doctor> set=hm.keySet();
// Iterator<Doctor> it=set.iterator();
// while(it.hasNext()){
// Doctor d=it.next();
// String address=hm.get(d);
// System.out.println(d+"----"+address);
// }
//第二种方式:
Set<Map.Entry<Doctor, String>> entrySet=tm.entrySet();
Iterator<Map.Entry<Doctor, String>> it=entrySet.iterator();
while(it.hasNext()){
Map.Entry<Doctor, String> me=it.next();
Doctor d=me.getKey();
String address=me.getValue();
System.out.println(d+"====="+address);
}
}
}
class Doctor implements Comparable<Doctor>{
private String name;
private int age;
public String getName() {
return name;
}
public int getAge() {
return age;
}
Doctor(String name,int age){
this.name=name;
this.age=age;
}
public int hashCode(){
return this.name.hashCode()+this.age*17;
}
public boolean equals(Object obj){
if(!(obj instanceof Doctor)){
throw new ClassCastException("类型不匹配");
}
Doctor d=(Doctor)obj;
return this.name.equals(d.name)&&this.age==d.age;
}
public String toString(){
return this.name+"...."+this.age;
}
public int compareTo(Doctor obj){
int num=new Integer(this.getAge()).compareTo(new Integer(obj.getAge()));
if(num==0){
return this.name.compareTo(obj.name);
}
return num;
}
}
class Lencomp implements Comparator<Doctor>{
public int compare(Doctor d1,Doctor d2){
if(d1.getName().length()>d2.getName().length())
return 1;
if(d1.getName().length()==d2.getName().length()){
new Integer(d1.getAge()).compareTo(new Integer(d2.getAge()));
}
return -1;
}
}