---------------------- ASP.Net+Android+IOS开发、.NET培训、期待与您交流! ----------------------
Map集合
该集合存储键值对,而且要保证键的唯一性1.添加
put(K key,V value);
putAll();
2.删除
clear();
remove(Object key);
3.判断
containsKey(Object key);
containsValue(Object value);
4.获取
get(Object key);
size();
values();
entrySet();
Map
|---Hashtable:底层是哈希表数据结构,不能存入null键值对,该集合是线程同步得,效率低
|---HashMap:底层是哈希表数据结构,允许使用null键值对,该集合是线程不同步得,效率高
|---TreeMap:底层是二叉树数据机构,该集合线程不同步,可以用于给Map集合中的键进行排序
例子1
import java.util.*;
class MapDemo1
{
Map
map = new HashMap
();
//添加元素,如果出现添加相同的值,然后添加的值会覆盖原有的值
map.put("01","张三");
map.put("02","李四");
map.put("03","王二");
map.put("04","马五");
//HashMap可以存入null键值对
map.put("null","null");
//判断map中是否存在键为02
System.out.println("containsKey:"+map.containsKey("02"));
System.out.println("remove:"+map.remove("002"));
System.out.println("remove:"+map.remove("02"));
//打印map
System.out.println(map);
System.out.println("get:"+map.get("04"));
System.out.println("get:"+map.get("null"));
System.out.println(map);
//获取map中所有的值
Collection
values = map.values();
System.out.println(values);
}
Map集合的两种取出方式:
1.Set<T> KeySet:将map中所有的键存入set集合中,因为set具备迭代器
所有可以迭代方式取出所有的值,再根据get方法,获取每一个键对应的值
例子:
import java.util.*;
public class MapDemo2
{
public static void main(String args[]){
Map
map = new HashMap
();
//添加元素
map.put("01","张三");
map.put("03","李四");
map.put("02","王二");
map.put("04","马五");
//先获取map集合的所有键的set集合,keySet()
Set
keySet = map.keySet();
//迭代
Iterator
it = keySet.iterator();
while(it.hasNext()){
String key = it.next();
//有了键就可以通过map集合的get方法获取其键对应的值
String value = map.get(key);
System.out.println(key+"="+value);
}
}
}
2.Set<T> entrySet:将map集合中的映射关系存入到了set集合中,而这个关系的数据类型就是: Map.Entry
例子:
import java.util.*;
public class MapDemo3
{
public static void main(String args[]){
Map
map = new HashMap
();
//添加元素
map.put("01","张三");
map.put("03","李四");
map.put("02","王二");
map.put("04","马五");
//先获取map集合的所有键的set集合
Set
> entrySet = map.entrySet();
//迭代
Iterator
> itor = entrySet.iterator(); while(itor.hasNext()){ Map.Entry
me = itor.next(); String key = me.getKey(); String value = me.getValue(); System.out.println(key+"="+value); } } }
Map练习
/**
每一个学生都有对应的归属地
学生Student,地址:String
学生属性:name,age
注意:姓名和年龄相同的视为同一个学生
保证学生的唯一性
步骤:
1.描述学生
2.定义map容器,将学生作为键,地址作为值存入
3.获取map集合中的元素
**/
import java.util.*;
public class MapDemo1
{
public static void main(String args[]){
Map
hm = new HashMap
();
hm.put(new Student("a",20),"深圳");
hm.put(new Student("b",21),"北京");
hm.put(new Student("c",22),"武汉");
hm.put(new Student("d",23),"天津");
hm.put(new Student("e",24),"广州");
hm.put(new Student("f",25),"南京");
// Set
keySet = hm.keySet();
//
// Iterator
it = keySet.iterator();
//
// while(it.hasNext()){
// Student s = it.next();
// System.out.println(s+"地址:"+hm.get(s));
// }
Set
> me = hm.entrySet(); Iterator
> it = me.iterator(); while(it.hasNext()){ Map.Entry
m = it.next(); Student key = m.getKey(); String value = m.getValue(); System.out.println(key+value); } } } class Student implements Comparable
{ private String name; private int age; public Student(String name,int age){ this.name = name; this.age = age; } public String getName(){ return name; } public int getAge(){ return age; } public String toString(){ return "姓名:"+name+"年龄"+age; } public int hashCode(){ return name.hashCode()+age*34; } public boolean equals(Object obj){ //如果传入的对象不是Student,则抛出异常 if(!(obj instanceof Student)){ throw new ClassCastException("类型不匹配"); } //强制把obj转为Student Student s = (Student)obj; return this.name.equals(s.name)&&this.age == s.age; } //重写compareTo() public int compareTo(Student s){ //age相比较 int num = new Integer(this.age).compareTo(new Integer(s.age)); //如果年龄相等 if(num == 0){ return this.name.compareTo(s.name); } return num; } }
TreeMap
/**
按照学生姓名排序
**/
例子:
import java.util.*;
public class MapDemo2
{
public static void main(String args[]){
Map
hm = new TreeMap
();
hm.put(new Student("d",23),"深圳");
hm.put(new Student("g",21),"北京");
hm.put(new Student("a",25),"武汉");
hm.put(new Student("e",24),"天津");
hm.put(new Student("e",27),"广州");
hm.put(new Student("c",26),"南京");
hm.put(new Student("b",22),"四川");
//添加一个key相同,value不同的元素
hm.put(new Student("b",22),"南昌");
// Set
keySet = hm.keySet();
//
// Iterator
it = keySet.iterator();
//
// while(it.hasNext()){
// Student s = it.next();
// System.out.println(s+"地址:"+hm.get(s));
// }
Set
> me = hm.entrySet(); Iterator
> it = me.iterator(); while(it.hasNext()){ Map.Entry
m = it.next(); Student key = m.getKey(); String value = m.getValue(); System.out.println(key+value); } } } class Student implements Comparable
{ private String name; private int age; public Student(String name,int age){ this.name = name; this.age = age; } public String getName(){ return name; } public int getAge(){ return age; } public String toString(){ return "姓名:"+name+"年龄"+age; } public int hashCode(){ return name.hashCode()+age*34; } public boolean equals(Object obj){ //如果传入的对象不是Student,则抛出异常 if(!(obj instanceof Student)){ throw new ClassCastException("类型不匹配"); } //强制把obj转为Student Student s = (Student)obj; return this.name.equals(s.name)&&this.age == s.age; } //重写compareTo() 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); // } //按照学生姓名排序 int num = this.name.compareTo(s.name); if(num ==0){ return new Integer(this.age).compareTo(new Integer(s.age)); } return num; } }
Map扩展
Map集合被使用是因为具备映射关系
假设有这样的需求,itheima机构有两个班级,一个yure班级,班级里面有学生(ID,姓名),另外一个jiuye班级,班级里面有学生(ID,姓名)。利用Map实现获取itheima中的学生
import java.util.*;
public class MapDemo3
{
public static void main(String[] args){
//传智播客集合(班级,学生)
HashMap
> czbk = new HashMap
>();
//预热班级(学号,姓名)
HashMap
yure = new HashMap
(); //就业班级(学号,姓名) HashMap
jiuye = new HashMap
(); //把两个班级添加到传智播客 czbk.put("预热班",yure); czbk.put("就业班",jiuye); //把学生添加到班级 yure.put("001","wangheng"); yure.put("002","zhangshan"); jiuye.put("101","lisi"); jiuye.put("102","wangwu"); //取出传智播客的键 Iterator
it = czbk.keySet().iterator(); while(it.hasNext()){ String roomName = it.next(); HashMap
room = czbk.get(roomName); System.out.println(roomName); getStudentInfo(room); } } //获取班级里面的学生 public static void getStudentInfo(HashMap
roomMap){ Iterator
it = roomMap.keySet().iterator(); while(it.hasNext()){ String stuId = it.next(); String stuName = roomMap.get(stuId); System.out.println(stuId+"..."+stuName); } } }
---------------------- ASP.Net+Android+IOS开发 、 .NET培训 、期待与您交流! ----------------------
详细请查看: http://edu.csdn.net