------- android培训、java培训、期待与您交流! ----------
主要内容:一、Map集合概述;二、map集合的两种取出方式:
一、Map集合概述:Map<K,V>
该集合存储键值对。一对一对往里存。而且要保证键的唯一性。
1,添加。
put(K key,V value)
putAll(Map<? extends K,? extends V> m)
2,删除。
clear();
remove(Object key)
3,判断。
containsValue(Object value)
containsKey(Object key)
isEmpty()
4,获取。
get(Object key)
size()
values()
entrySet()
keySet()
Map
|--Hashtable:底层是哈希表数据结构,不可以存入null键null值。该集合是线程同步的。jdk1.0 效率低
|--HashMap:底层是哈希表数据结构,运行使用null键和null值,该集合是不同步的。 jdk1.2 效率高
|--TreeMap:底层是二叉树数据结构,线程不同步,可以用于给map集合中的建进行排序。
和Set很像
其实,Set底层就是使用了Map集合。
例:基本练习
import java.util.*;
class MapDemo
{
public static void main(String[] args)
{
Map<String,String> map = new HashMap<String,String>();
//添加元素
//如果出现添加相同的建,那么后添加的值会覆盖原有建对应值。
//并put方法会返回被覆盖的值。
map.put("01","zhangsan1");
map.put("01","wangwu");
map.put("02","zhangsan2");
map.put("03","zhangsan3");
System.out.println("containsKey:"+map.containsKey("02"));
//System.out.println("remove:"+map.remove("02"));
System.out.println("get:"+map.get("02"));
map.put("04",null);
System.out.println("get:"+map.get("04"));
//可以通过get方法的返回值来判断一个键是否存在。通过返回null来判断。
//获取map集合中所有的值。
Collection<String> coll = map.values();
System.out.println(coll);
System.out.println(map);
}
}
二、map集合的两种取出方式:
1,Set<k> keySet:将map中所有的建存入到Set集合。因为set具备迭代器。
所以可以迭代方式取出所有的建,在根据get方法,获取每一个建对应的值。
Map集合的取出原理:将map集合转成set集合。在通过迭代器取出。
2,Set<Map.Entry<k,v>> entrySet:将map集合中的映射关系存入到了set集合中,
而这个关系的数据类型就是:Map.Entry
Map.Entry 其实Entry也是一个接口,它是Map即可中的一个内部接口。
练习例一:
/*
每一个学生都有对应的归属地。
学生Student,地址String。
学生属性:姓名,年龄。
注意:姓名和年龄相同的视为同一个学生。
保证学生的唯一性。
1,描述学生。
2,定义map容器。将学生作为建,地址作为值,存入。
3,获取map集合中的元素。
*/
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;
}
//覆盖hashCode和equals方法
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("lisi1",21),"beijing");
hm.put(new Student("lisi1",21),"tianjin");
hm.put(new Student("lisi2",22),"shanghai");
hm.put(new Student("lisi3",23),"nanjing");
hm.put(new Student("lisi4",24),"wuhan");
//第一种取出方式 keySet
Set<Student> keySet = hm.keySet();
Iterator<Student> it = keySet.iterator();
while (it.hasNext())
{
Student stu = it.next();
String addr = hm.get(stu);
System.out.println(stu+".."+addr);
}
//第二种取出方式 entrySet
Set<Map.Entry<Student,String>> entrySet = hm.entrySet();
Iterator<Map.Entry<Student,String>> iter = entrySet.iterator();
while(iter.hasNext())
{
Map.Entry<Student,String> me = iter.next();
Student stu = me.getKey();
String addr = me.getValue();
System.out.println(stu+"......."+addr);
}
}
}
练习例二:
/*
需求:对学生对象的年龄进行升序排序。
因为数据是以键值对形式存在的。
所以要使用可以排序的Map集合。TreeMap。
*/
import java.util.*;
//自定义比较器
class StuNameComparator implements Comparator<Student>
{
public int compare(Student s1,Student s2)
{
int num = s1.getName().compareTo(s2.getName());
if(num==0)
return new Integer(s1.getAge()).compareTo(new Integer (s2.getAge()));
return num;
}
}
class MapTest2
{
public static void main(String[] args)
{
//把自定义比较器传进去
TreeMap<Student,String> tm = new TreeMap<Student,String>(new StuNameComparator());
tm.put(new Student("lisi1",21),"beijing");
tm.put(new Student("lisi1",21),"tianjin");
tm.put(new Student("alisi2",22),"shanghai");
tm.put(new Student("lisi3",23),"nanjing");
tm.put(new Student("lisi4",24),"wuhan");
Set<Map.Entry<Student,String>> entrySet = tm.entrySet();
Iterator<Map.Entry<Student,String>> it = entrySet.iterator();
while(it.hasNext())
{
Map.Entry<Student,String> me = it.next();
Student stu = me.getKey();
String addr = me.getValue();
System.out.println(stu+":"+addr);
}
}
}
例:
/*
map扩展知识。
map集合被使用是因为具备映射关系。
"yureban" Student("01" "zhangsan");
"yureban" "02" "lisi";
"jiuyeban" "01" "wangwu";
"jiuyeban" "02" "wangwu";
*/
import java.util.*;
class Student
{
private String id;
private String name;
Student(String id,String name)
{
this.id = id;
this.name = name;
}
public String toString()
{
return id+":::"+name;
}
}
class MapDemo3
{
public static void demo()
{
HashMap<String,List<Student>> czbk = new HashMap<String,List<Student>>();
List<Student> yure = new ArrayList<Student>();
List<Student> jiuye = new ArrayList<Student>();
czbk.put("yurenban",yure);
czbk.put("jiuyeban",jiuye);
yure.add(new Student("01","zhangsan"));
yure.add(new Student("02","lisi"));
jiuye.add(new Student("01","wangwu"));
jiuye.add(new Student("02","zhaoliu"));
Iterator<String> it = czbk.keySet().iterator();
while(it.hasNext())
{
String roomName = it.next();
List<Student> room = czbk.get(roomName);
System.out.println(roomName);
getInfos(room);
}
}
public static void getInfos(List<Student> list)
{
Iterator<Student> it = list.iterator();
while(it.hasNext())
{
Student s = it.next();
System.out.println(s);
}
}
public static void main(String[] args)
{
demo();
/*
HashMap<String,HashMap<String,String>> czbk = new HashMap<String,HashMap<String,String>>();
HashMap<String,String> yure = new HashMap<String,String>();
HashMap<String,String> jiuye = new HashMap<String,String>();
czbk.put("yureban",yure);
czbk.put("jiuyeban",jiuye);
yure.put("01","zhangsan");
yure.put("02","lisi");
jiuye.put("01","wangwu");
jiuye.put("02","zhaoliu");
//遍历czbk集合,获取所有的教师。
Iterator<String> it = czbk.keySet().iterator();
while(it.hasNext())
{
String roomName = it.next();
HashMap<String,String> room = czbk.get(roomName);
System.out.println(roomName);
getStudentInfo(room);
}
//getStudentInfo(yure);
//getStudentInfo(jiuye);
*/
}
public static void getStudentInfo(HashMap<String,String> roomMap)
{
Iterator<String> it = roomMap.keySet().iterator();
while(it.hasNext())
{
String id = it.next();
String name = roomMap.get(id);
System.out.println(id+":"+name);
}
}
}