一、集合(Map概述)
java.util
接口 Map<K,V>
类型参数:
K - 此映射所维护的键的类型
V - 映射值的类型
Map
|--Hashtable
|--HashMap
|--TreeMap
1.添加
put(K key, V value) 将指定的值与此映射中的指定键关联(可选操作)。
putAll(Map<? extends K,? extends V> m) 从指定映射中将所有映射关系复制到此映射中(可选操作)。
2.删除
void clear() 从此映射中移除所有映射关系(可选操作)。
V remove(Object key) 如果存在一个键的映射关系,则将其从此映射中移除(可选操作)。
3.判断
boolean containsKey(Object key) 如果此映射包含指定键的映射关系,则返回 true。
boolean containsValue(Object value) 如果此映射将一个或多个键映射到指定值,则返回 true。
boolean isEmpty() 如果此映射未包含键-值映射关系,则返回 true。
4.获取
V get(Object key) 返回指定键所映射的值;如果此映射不包含该键的映射关系,则返回 null。
int size() 返回此映射中的键-值映射关系数。
Collection<V> values() 返回此映射中包含的值的 Collection 视图。
Set<Map.Entry<K,V>> entrySet() 返回此映射中包含的映射关系的 Set 视图。
Set<K> keySet() 返回此映射中包含的键的 Set 视图。
二、集合(Map子类对象特点)
Map
|--Hashtable:底层是哈希表数据结构,不可以存入null健null值。该集合线程同步。jdk1.0 效 率低
|--HashMap:底层是哈希表数据结构,允许使用null健null值。该集合线程不同步。jdk1.2 效率高
|--TreeMap:底层是二叉树数据结构。线程不同步。可以用于给map集合中的健进行排序。
Map和Set很像,其实Set底层就是使用了Map集合。
三、集合(Map共性方法)
- /*
- Map共性方法:
- */
- import java.util.*;
- class MapDemo
- {
- public static void main(String[] args)
- {
- Map<String,String> map=new HashMap<String,String>();
- /*添加元素,如果出现添加时有相同的健,那么后添加的值会覆盖原有键对应的值。
- 并且put会返回原有健的值。
- */
- System.out.println(map.put("001","张三"));
- System.out.println(map.put("001","zhangsan"));
- map.put("002","李四");
- map.put("003","王五");
- System.out.println("containsKey:"+map.containsKey("002"));
- System.out.println("remove:"+map.containsKey("0021"));
- System.out.println("remove:"+map.remove("002"));
- System.out.println(map);
- System.out.println("get:"+map.get("0012"));//可以通过get方法的返回值来判断一个键是否存在。
- Collection<String> coll=map.values();
- System.out.println(coll);
- System.out.println(map);
- }
- }
四、集合(Map-keySet)
- /*
- Map集合的两种取出方式:
- 1. Set<k> keySet():将Map中所有的键存入Set集合。因为Set具备迭代器。
- 所以可以用迭代的方式取出所有的健,再根据get方法获取每个键对应的值。
- Map集合的取出原理:将Map集合转成Set集合,再通过迭代器取出。
- */
- import java.util.*;
- class MapDemo2
- {
- public static void main(String[] args)
- {
- Map<String,String> map=new HashMap<String,String>();
- map.put("001","张三");
- map.put("002","李四");
- map.put("003","王五");
- map.put("004","小明");
- Set<String> keySet=map.keySet();//获取map集合所有键的Set集合,keySet();
- Iterator<String> it=keySet.iterator();
- while(it.hasNext())
- {
- String key=it.next();
- String value=map.get(key);
- System.out.println("key:"+key+"\tvalue:"+value);
- }
- }
- }
五、集合(Map-entrySet)
- /*
- map集合的两种取出方式:
- 2.Set<Map.Entry<K,V>>entrySet():将Map集合中的映射关系存入Set集合中,而这个关系的
- 数据类型是:Map.Entry
- Map.Entry其实Entry也是一个接口,他是Map接口中的内部接口。
- interface Map
- {
- public static interface Entry
- {
- public abstract Object getKey();
- public abstract Object getValue();
- }
- }
- class HashMap implements Map
- {
- class Hahs implements Map.Entry
- {
- public abstract Object getKey(){}
- public abstract Object getValue(){}
- }
- }
- */
- import java.util.*;
- class MapDemo2
- {
- public static void main(String[] args)
- {
- Map<String,String> map=new HashMap<String,String>();
- map.put("001","张三");
- map.put("002","李四");
- map.put("003","王五");
- map.put("004","小明");
- Set<Map.Entry<String,String>> entrySet=map.entrySet();//将Map集合中的映射关系取出存入Set集合中。
- Iterator<Map.Entry<String,String>> it=entrySet.iterator();
- while(it.hasNext())
- {
- Map.Entry<String,String> me=it.next();
- String key=me.getKey();
- String value=me.getValue();
- System.out.println("key:"+key+"\tvalue:"+value);
- }
- }
- }
六、集合(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;
- }
- 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("zhangsan01",20),"beijing");
- hm.put(new Student("zhangsan02",23),"shanghai");
- hm.put(new Student("zhangsan03",24),"nanjing");
- hm.put(new Student("zhangsan04",22),"wuhan");
- //第一种取出方式:
- 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.getName()+"\t年龄:"+stu.getAge()+"\t地址:"+addr);
- }
- //第二种取出方式:
- Set<Map.Entry<Student,String>> entrySet=hm.entrySet();
- Iterator<Map.Entry<Student,String>> itr=entrySet.iterator();
- while(itr.hasNext())
- {
- Map.Entry<Student,String> me=itr.next();
- Student stu=me.getKey();
- String addr=me.getValue();
- System.out.println("姓名:"+stu.getName()+"\t年龄:"+stu.getAge()+"\t地址:"+addr);
- }
- }
- }
七、集合(TreeMap练习)
- /*
- 需求:对学生对象的年龄进行升序排序。
- 因为数据是以键值对的形式存在的,所以要使用判续的Map集合TreeMap
- */
- 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 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("zhangsan01",20),"beijing");
- tm.put(new Student("zhangsan03",24),"nanjing");
- tm.put(new Student("zhangsan02",23),"shanghai");
- tm.put(new Student("zhangsan04",22),"wuhan");
- tm.put(new Student("zhangsan04",20),"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);
- }
- }
- }
八、集合(TreeMap练习-字母出现的次数)
- /*
- 练习:
- "asfdjhsadkjfhskfsad"获取该字符串中的字母出现的次数。
- 打印形式:a(1)b(2)....
- 通过结果发现,每一个字母都有对应的次数。
- 说明字母和次数之间都有映射关系。
- 注意:
- 当发现有映射关系时,可以选择Map集合。因为Map集合中存放的就是映射关系。
- 什么时候使用Map集合呢?
- 当数据之间存在着映射关系时,就要先想到Map集合。
- 思路:
- 1.将字符串转换为字符数组。
- 2.定义一个Map集合,因为打印结构的字母有顺序,所以使用TreeMap集合
- 3.遍历字符数组。将每一个字母作为键去查Map集合。如果返回null,将
- 该字母和1存入到Map集合中。如果返回不是null,说明该字母在Map集合已经
- 存在并有对应的次数。那么就获取该次数并进行自增,然后将字母和自增后的次序
- 数存入到Map集合中,覆盖掉原来键所对应的值。
- 4.将Map集合中的数据编程指定的字符串形式返回。
- */
- import java.util.*;
- class MapTest3
- {
- public static void main(String[] args)
- {
- String s=charCount("asdasd");
- System.out.println(s);
- }
- public static String charCount(String str)
- {
- int count=0;
- char[] chs=str.toCharArray();
- TreeMap<Character,Integer> tm=new TreeMap<Character,Integer>();
- for(int i=0;i<chs.length;i++)
- {
- if(!(chs[i]>='a'&&chs[i]<='z'||chs[i]>='A'&&chs[i]<='Z'))
- continue;
- Integer value=tm.get(chs[i]);
- if(value!=null)
- count=value;
- count++;
- tm.put(chs[i],count);
- count=0;//清0
- /*
- if(value==null)
- {
- tm.put(chs[i],1);
- }
- else
- {
- value=value+1;
- tm.put(chs[i],value);
- }
- */
- }
- StringBuilder sb=new StringBuilder();
- Set<Map.Entry<Character,Integer>> entrySet=tm.entrySet();
- Iterator<Map.Entry<Character,Integer>> it=entrySet.iterator();
- while(it.hasNext())
- {
- Map.Entry<Character,Integer> me=it.next();
- Character ch=me.getKey();
- Integer value=me.getValue();
- sb.append(ch+"("+value+")");
- }
- return sb.toString();
- }
- }
九、集合(Map扩展)
- /*
- Map扩展知识:
- Map集合被使用是因为具备映射关系。
- 1.
- "yureban" "01" "zhangsan"
- "yureban" "02" "lisi"
- "jiuyeban" "01" "wangwu"
- "jiuyeban" "02" "zhaoliu"
- 2.
- "yureban" Student("01" "zhangsan")
- "yureban" Student("02" "lisi")
- "jiuyeban" Student("01" "wangwu")
- "jiuyeban" Student("02" "zhaoliu")
- 一个学校有多个教室,每个教室有个名称。
- */
- 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+"\t姓名:"+name;
- }
- }
- class MapDemo3
- {
- public static void main(String[] args)
- {
- System.out.println("1.");
- HashMap<String,String> yure=new HashMap<String,String>();
- yure.put("0001","张三");
- yure.put("0002","李四");
- HashMap<String,String> jiuye=new HashMap<String,String>();
- jiuye.put("0001","王五");
- jiuye.put("0002","赵六");
- HashMap<String,HashMap<String,String>> czbk=new HashMap<String,HashMap<String,String>>();
- czbk.put("预热班",yure);
- czbk.put("就业班",jiuye);
- //遍历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);
- }
- System.out.println("2.");
- demo();
- }
- 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("预热班",yure);
- czbk.put("就业班",jiuye);
- yure.add(new Student("0001","张三"));
- yure.add(new Student("0002","李四"));
- jiuye.add(new Student("0001","王五"));
- jiuye.add(new Student("0002","赵六"));
- 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 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+"\t姓名:"+name);
- }
- }
- }