Map集合 16天

来源:互联网 发布:北京数据录入 编辑:程序博客网 时间:2024/05/18 01:57


黑马程序员_毕向东_Java基础视频教程第16天-01-集合(Map概述)

黑马程序员_毕向东_Java基础视频教程第16天-02-集合(Map子类对象特点)

黑马程序员_毕向东_Java基础视频教程第16天-03-集合(Map共性方法)

/*

Map集合:该集合存储键值对,一对,一对往里存,而且要保证键的唯一性,

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值该集合是线程同步的.jdk.1.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("02","zhangsan2");map.put("03","zhangsan3");System.out.println("containsKey:"+map.containsKey("022"));map.put("04",null);System.out.println("get:"+map.get("04"));//可以通过get方法的返回值判断一个键是否存在,通过返回null来判断.//  System.out.println("remove:"+map.remove("022"));//获取map集合中所有的值.Collection<String> coll=map.values();System.out.println(coll);System.out.println(map);}}


 

黑马程序员_毕向东_Java基础视频教程第16天-04-集合(Map-keySet)

黑马程序员_毕向东_Java基础视频教程第16天-05-集合(Map-entrySet)

 

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("02","zhangsan2");map.put("03","zhangsan3");map.put("01","zhangsan1");map.put("04","zhangsan4"); //先获取map集合的所有键的Set集合,keySet();Set<String>keySet=map.keySet();//有了Set集合,就可以获取期迭代器.Iterator<String> it=keySet.iterator();while(it.hasNext()){String key=it.next();//有了键可以通过map集合的get方法获取其对应的值.String value=map.get(key);System.out.println("key:"+key+",value:"+value);}}}


 

2,Set<Map.Entry<k,v>> entrySet:  将map集合中的映射关系存入到了Set集合中,而这个关系的数据类型,就是 Map.Entry

 Set<Map.Entry<String,String>> entrySet=map.entrySet();

 

Map.Entry 其实Entry也是一个接口,它是Map接口的一个内部接口.

interface Map{public static interface Entry{public abstract Object.getKey();public abstract Object getValue();}}class HasmMap implements Map{class Haha implements Map.Entry{public abstract Object.getKey(){};public abstract Object getValue(){};}}


 

黑马程序员_毕向东_Java基础视频教程第16天-06-集合(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("lisi1",21),"北京");hm.put(new Student("lisi2",22),"北京1");hm.put(new Student("lisi3",23),"北京2");hm.put(new Student("lisi4",24),"北京4");//第一种取出方式 keySetSet<Student> keySet=hm.keySet();Iterator<Student>it=hm.keySet().iterator();while(it.hasNext()){Student stu=it.next();String addr=hm.get(stu);System.out.println(stu+".."+addr);}//第二种取出方式entrySetSet<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);}}}


 黑马程序员_毕向东_Java基础视频教程第16天-07-集合(TreeMap练习)

 

需求:对学生对象的年龄进行升序排序.

因为数据是以键值对形式存在的.

所以要使用可以排序的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("blisi1",21),"北京");tm.put(new Student("clisi2",22),"北京1");tm.put(new Student("lisi3",23),"北京2");tm.put(new Student("lisi4",24),"北京4");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);}}}


黑马程序员_毕向东_Java基础视频教程第16天-08-集合(TreeMap练习-字母出现的次数)

 练习

"sdfgzxcvasdfxcvdf"获取该字符串中字母出现的次数.

希望打印结果:a(1)c(2).....

通过结果发现,每一个字母都有对应的次数,

说明字母和次数之间都有映射关系.

注意了,当发现有映射关系时,可以选择map集合,

因为map集合中存放的就是映射关系.

 

什么使用map结合呢?

当数据之间存在这映射关系时,就要先想到 map集合

 

第一次用a字母为键去找集合,那么集合没有a这个键,所以也没有对应的次数.

返回null

如果null就将字母a和1存入集合

 

如果指定的键已经存在,说明有对应的次数,就将对应的次数取出,并自增后重新

存入集合.

 

思路:

1将字符串转换中数组,因为要对每一个字母进行操作.

2定义一个Map集合,因为打印结果字母有顺序,所以使用treemap集合.

3遍历字符数组

将每一个字母作为键去查map集合.

如果返回null,将该字母和1存在map集合中

如果返回不是null  说明该字母在map集合内已经存在并有对应次数.

那么就获取该次数,并进行自增,然后将该字母和自增后的次数存入map集合中,

覆盖调用原键所对应的值.

4将map集合中的数据变成指定的字符串形式返回.

/*"sdfgzxcvasdfxcvdf"获取该字符串中字母出现的次数.希望打印结果:a(1)c(2).....*/import java.util.*;class MapTest3{public static void main(String[] args){String str="s+d_fgz+xcva,,sdfxcvdf";str=charCount(str);System.out.println(str);}public static String charCount(String str){char[] chs=str.toCharArray();TreeMap<Character,Integer> tm=new TreeMap<Character,Integer>();int count=0;for(int x=0;x<chs.length;x++){if(!(chs[x]>='a'&&chs[x]<='z'||chs[x]>='A'&&chs[x]<='Z'))continue;Integer value=tm.get(chs[x]);if(value!=null)count=value;count++;tm.put(chs[x],count);count=0;/*if(value==null){tm.put(chs[x],1);}else{value=value+1;tm.put(chs[x],value);}*/}//System.out.println(tm);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();}}

黑马程序员_毕向东_Java基础视频教程第16天-09-集合(Map扩展)

 

map扩展知识.

map集合被使用是因为具备映射关系.

 

 

集合,一定要多用几遍,要不然怎么才会熟练,感觉集合,要把对对象的概念理解非常清晰,写代码才会非常有速度.


0 0
原创粉丝点击