- Map集合及使用
- / *
- Map集合:该集合存储键值对。一对一对往里存。而且要保证键的唯一性。
- 1,添加。
- V put(K key,V value); 返回该键对应的原有的值,操作完新值覆盖旧值。
- putAll(Map<? extends K,? extends V> m)
- 2,删除。
- clear();
- remove(Object key);
- 3,判断。
- boolean containsKey(Object key);
- boolean containsValue(Object Value);
- boolean isEmpty();
- 4,获取。
- V get(Object key);
- size();
- values();
- ***Map集合的两种取出方式:
- entrySet();
- keySet();
- Map |--HashTable: 底层是哈希表结构,不可以存入null键null值,是线程同步的。效率低
- |--HashMap: 底层是哈希表结构,允许使用null键null值,是线程不同步的。效率高
- |--TreeMap: 底层是二叉树数据结构,可以用于给Map集合中元素排序线程不同步。
- 和Set很像,实际上,Set底层就是使用了Map集合。
- Map集合的两种取出方式:
- 1.Set<k> keySet = map.keySet();将Map中所有的键存入到Set集合。
- 由于Set具备迭代器,用迭代方式取出所有键,再由get方法,获取每一个键对应的值。
- 原理:将Map集合转成Set集合,元素为键,再通过迭代器取出值。
- 2.Set< Map.Entry<k,v> > entrySet:
- 将Map集合中的映射关系存入Set集合中,这个关系的数据类型为Map.Entry。
- 【注意】:
- public static interface Map.Entry<K,V>映射项(键-值对)。
- Map.entrySet 方法返回映射的collection 视图,其中的元素属于此类。获得映射项引用的唯一
- 方法是通过此 collection 视图的迭代器来实现。这些Map.Entry 对象仅 在迭代期间有效;更确切地讲,如果在迭代器返回项之后修改了底层映射,则某些映射项的行为是不确定的,除了通过 setValue 在映射项上执行操作之外。
- 【Map.Entry】
- 其实Entry也是一个接口,它是Map接口中的一个内部接口。
- interface Map
- {
- public static interface Entry{//对外提供,影射关系
- public abstract Object getKey();
- public abstract Object getValue
- }
- }
- class HashMap implements Map.Entry
- {
- class Inner implements Map.Entry
- {
- public Object getKey(){}
- public Object getValue(){}
- }
- }
- */
- /*
- import java.util.*;
- class MapDemo
- {
- public static void main(String[] args)
- {
- Map<String, String> map = new HashMap<String, String>();
- //添加元素
- map.put("01","jack1");
- map.put("02","jack2");
- map.put("03","jack3");
- System.out.println("put:"+map.put("04","jack4"));
- System.out.println("put:"+map.put("04","jack5"));//jack5被覆盖
- ①基本操作
- System.out.println("containKey:"+map.containsKey("02"));
- System.out.println("remove:"+map.remove("02"));
- System.out.println("get:"+ map.get("03"));
- //可以通过get方法的返回值来判断一个键是否存在,通过返回null来判断
- //获取Map集合中所有的值。
- Collection<String> coll = map.values();
- System.out.println(coll);
- System.out.println(map);
- ②第一种取出方法:
- //先获取map集合的所有键的Set集合,使用keySet();
- Set<String> keySet = map.keySet();
- //有了Set集合,就可以获取其迭代器。
- Iterator<String> it = keySet.iterator();
- while (it.hasNext())
- {
- String key = it.next();
- //有了键就可以通过Map集合的get方法获取对应的值
- System.out.println("key :"+key+"..."+"value :"+map.get(key));
- }
- ③第二种取出方法:
- //讲Map集合中的映射关系取出,存入到Set集合中。
- Set<Map.Entry<String,String>> entrySet = map.entrySet();
- 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+"..."+"value:"+value);
- }
- }
- }
- */
- /*
- 【练习一】:
- 每一个学生都有对应的归属地。学生Student,地址String。
- 学生属性:姓名和年龄。姓名和年龄相同视为同一个学生、
- 需要保证学生的唯一性。
- 【练习二】:接【练习一】对学生对象的年龄进行升序排序
- * /
- 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 String getName(){
- return name;
- }
- public int getAge(){
- return age;
- }
- public String toString(){
- return name+" : "+age;
- }
- //Map的put操作,会调用containsKey(使用equals)判断,因此需要覆盖hashCode和equals方法。
- public int hashCode(){
- System.out.println("check1");
- return this.age*21+this.name.hashCode();
- }
- public boolean equals(Object obj){
- if(!(obj instanceof Student))//犯错笔记:记得加!符
- throw new ClassCastException("Not a student type");
- System.out.println("check2");
- Student p = (Student)obj;
- return this.name.equals(p.name)&&this.age == p.age;
- }
- //为了防止Student被装入TreeSet集合时进行排序,需要实现Comparable接口和复写compareTo
- public int compareTo(Student stu){
- int num = new Integer(this.age).compareTo(new Integer(stu.age));
- if(num == 0)
- return this.name.compareTo(stu.name);
- return num;
- }
- }
- class Comp implements Comparator<Student>//可以利用已经实现Comparable的功能,也可以利用比较器
- {
- public int compare(Student s1,Student s2){
- int num = new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
- if(num == 0)
- return s1.getName().compareTo(s2.getName());
- return num;
- }
- }
- class MapDemo
- {
- public static void main(String[] args)
- {
- //Map<Student,String> map = new HashMap<Student,String>();
- //TreeMap元素需要排序,因此需要定义比较器。
- Map<Student,String> map = new TreeMap<Student,String>(new Comp());
- map.put(new Student("Emma",22),"Nanjing");
- map.put(new Student("Mike",25),"Beijing");
- map.put(new Student("Jack",20),"Haikou");
- map.put(new Student("Penny",27),"Wuhan");
- //map.put(new Student("Penny",27),"Wuhan");
- map.put(new Student("Lily",27),"Hankou");//覆盖掉上一个Penny
- Set<Map.Entry<Student,String>> entryset = map.entrySet();
- Iterator<Map.Entry<Student,String>> it = entryset.iterator();
- while (it.hasNext())
- {
- Map.Entry<Student,String> me =it.next();
- Student key = me.getKey();
- String value = me.getValue();
- //System.out.println("Student:"+key.getName()+""+key.getAge()+"...."+"City :"+value);
- System.out.println(key+"..."+value);
- }
- }
- }
- /*
- Map扩展知识:
- Map集合被使用是因为具备映射关系。
- 双层映射:
- (一对一+一对一)
- HashMap<String,HashMap<String,String>>h=new HashMap<String,HashMap<String,String>>();
- (一对多)
- HashMap<String,List<Student>> h = new HashMap<String,List<Student>>();*//*
- 【需求】:"sdfgzxcvasdfxcvdf"获取该字符串中的字母出现的次数。
- 打印格式:a(1)c(2)...
- 通过结果发现,字母的次数之间有映射关系。可以使用Map集合。
- */
- import java.util.*;
- class Comp implements Comparator<Character>
- {
- public int compare(Character ch1,Character ch2){
- return (new Integer(ch1)).compareTo(new Integer(ch2));
- }
- }
- class MapTest
- {
- public static void main(String[] args)
- {
- StringBuilder sb = new StringBuilder("s,dfg,zxcvasd,fxcvdf");
- show(sb);
- }
- public static void show(StringBuilder sb){
- Map<Character,Integer> map = new TreeMap<Character,Integer>(new Comp());
- for (int index=0; index<sb.length() ;index++ )
- {
- char ch = sb.charAt(index);
- //System.out.println(ch);
- if ((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))//犯错笔记:记住需要统计的只是字母个数。
- {
- if(map.containsKey(ch)){
- int value = map.get(ch);
- map.put(ch,++value);
- }else{
- map.put(ch,1);
- }
- }
- }
- Set<Character> s = map.keySet();
- Iterator<Character> it = s.iterator();
- while (it.hasNext())
- {
- char cha = it.next();
- System.out.print(cha+"("+map.get(cha)+")"+" ");
- }
- System.out.println("\n");
- }
- }
Map集合及使用
最新推荐文章于 2023-06-16 19:36:45 发布