黑马程序员—Java基础—集合框架3

来源:互联网 发布:m20火箭筒数据 编辑:程序博客网 时间:2024/06/13 05:31


                                ------- android培训java培训、期待与您交流! ----------

 

                                                                    集合框架3

一、Map概述
Collection单列集合
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子类对象特点
Map
|--Hashtable:底层是哈希表数据结构,不可以存入null键null值。该集合是线程同步的。jdk1.0.效率低。
|--HashMap:底层是哈希表数据结构,允许使用 null 值和 null 键,该集合是不同步的。将hashtable替代,jdk1.2.效率高。
|--TreeMap:底层是二叉树数据结构。线程不同步。可以用于给map集合中的键进行排序。
Map和Set很像,Set底层就是使用了Map集合。

三、Map共性方法

<span style="font-family:SimSun;font-size:18px;">import java.util.*;</span>
<div style="text-align: left;"><span style="font-family:SimSun;font-size:18px;"><strong style="color: rgb(51, 51, 51); line-height: 26px; text-align: center; background-color: rgb(255, 255, 255);"></strong></span></div><span style="font-family:SimSun;font-size:18px;">class  MapDemo{public static void main(String[] args) {Map<String,String> map = new HashMap<String,String>();//添加元素,添加元素,如果出现添加时,相同的键。那么后添加的值会覆盖原有键对应值。//并且put方法会返回被覆盖的值。System.out.println("put:"+map.put("01","zhangsan1"));System.out.println("put:"+map.put("01","wnagwu"));map.put("02","zhangsan2");map.put("03","zhangsan3");System.out.println("containsKey:"+map.containsKey("022"));//System.out.println("remove:"+map.remove("02"));System.out.println("get:"+map.get("023"));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);}}</span>

四、Map-keySet

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集合中。//先获取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);}}}


五、Map-entrySet
Set<Map.Entry<k,v>> entrySet:将map集合中的映射关系存入到了set集合中,
而这个关系的数据类型就是:Map.Entry
Entry其实就是Map中的一个static内部接口。
为什么要定义在内部呢?
因为只有有了Map集合,有了键值对,才会有键值的映射关系。
关系属于Map集合中的一个内部事物。
而且该事物在直接访问Map集合中的元素。

<div style="color: rgb(51, 51, 51); font-family: SimSun; line-height: 26px; text-align: left;"><span style="font-family: SimSun;">import java.util.*;</span></div><span style="font-family:SimSun;color:#333333;"></span><div style="text-align: left;"><span style="line-height: 26px;">class MapDemo2 </span></div><span style="line-height: 26px;">{</span><div style="color: rgb(51, 51, 51); font-family: SimSun; line-height: 26px; text-align: left;"><span style="font-family: SimSun;">public static void main(String[] args) </span></div><span style="font-family:SimSun;color:#333333;"><span style="line-height: 26px;">{Map<String,String> map = new HashMap<String,String>();</span></span><div style="color: rgb(51, 51, 51); font-family: SimSun; line-height: 26px; text-align: left;"><span style="font-family: SimSun;">map.put("01","zhangsan1");</span></div><span style="font-family:SimSun;color:#333333;"><span style="line-height: 26px;">map.put("02","zhangsan2");map.put("03","zhangsan3");</span></span><div style="color: rgb(51, 51, 51); font-family: SimSun; line-height: 26px; text-align: left;"><span style="font-family: SimSun;">Set<Map.Entry<String,String>> entrySet = map.entrySet();</span></div><span style="font-family:SimSun;color:#333333;"><span style="line-height: 26px;">map.put("04","zhangsan4");//将Map集合中的映射关系取出。存入到Set集合中。</span></span><div style="color: rgb(51, 51, 51); font-family: SimSun; line-height: 26px; text-align: left;"><span style="font-family: SimSun;">{</span></div><span style="font-family:SimSun;color:#333333;"><span style="line-height: 26px;">Iterator<Map.Entry<String,String>> it = entrySet.iterator();while(it.hasNext())</span></span><div style="color: rgb(51, 51, 51); font-family: SimSun; line-height: 26px; text-align: left;"><span style="font-family: SimSun;">System.out.println(key+":"+value);</span></div><span style="font-family:SimSun;color:#333333;"><span style="line-height: 26px;">Map.Entry<String,String> me = it.next();String key = me.getKey();String value = me.getValue();}}</span></span><div style="color: rgb(51, 51, 51); font-family: SimSun; line-height: 26px; text-align: left;"><span style="font-family: SimSun;">}</span></div>


六、Map练习

每一个学生都有对应的归属地。
学生Student,地址String。
学生属性:姓名,年龄。
注意:姓名和年龄相同的视为同一个学生。
保证学生的唯一性。

1,描述学生。

2,定义map容器。将学生作为键,地址作为值。存入。

3,获取map集合中的元素。

import java.util.*;class Student implements Comparable<Student>  //当需要创建多个对象备用时,首先要实现Comparable{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;}//其次,hasSet传输数据,需要复写hashCode和equalspublic 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");// 复写了hashCode和equals,两个键为同一个键,新值tianjin覆盖旧值beijinghm.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");//第一种取出方式 keySetSet<Student> keySet = hm.keySet();Iterator<Student> it = keySet.iterator();//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);}}}

七、TreeMap练习
需求:对学生对象的年龄进行升序排序。
因为数据是以键值对形式存在的。
所以要使用可以排序的Map集合。TreeMap。

<div style="color: rgb(51, 51, 51); font-family: SimSun; line-height: 26px; text-align: left;"><span style="font-family: SimSun;">import java.util.*;</span></div><div style="text-align: left;"><span style="color:#333333;"><span style="line-height: 26px;"></span></span></div><div style="color: rgb(51, 51, 51); font-family: SimSun; line-height: 26px; text-align: left;"><span style="font-family: SimSun;">class StuNameComparator implements Comparator<Student></span></div><span style="font-family:SimSun;color:#333333;"></span><div style="text-align: left;"><span style="line-height: 26px;">{</span></div><span style="line-height: 26px;"></span><div style="text-align: left;">public int compare(Student s1,Student s2)</div>{<div style="color: rgb(51, 51, 51); font-family: SimSun; line-height: 26px; text-align: left;"><span style="font-family: SimSun;">if(num==0)</span></div><span style="font-family:SimSun;color:#333333;"><span style="line-height: 26px;">int num = s1.getName().compareTo(s2.getName());</span></span><div style="color: rgb(51, 51, 51); font-family: SimSun; line-height: 26px; text-align: left;"><span style="font-family: SimSun;">class  MapTest2</span></div><span style="font-family:SimSun;color:#333333;"><span style="line-height: 26px;">return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));return num;}}{</span></span><div style="color: rgb(51, 51, 51); font-family: SimSun; line-height: 26px; text-align: left;"><span style="font-family: SimSun;">TreeMap<Student,String> tm = new TreeMap<Student,String>(new StuNameComparator());</span></div><span style="font-family:SimSun;color:#333333;"><span style="line-height: 26px;">public static void main(String[] args) {tm.put(new Student("blisi3",23),"nanjing");tm.put(new Student("lisi1",21),"beijing");</span></span><div style="color: rgb(51, 51, 51); font-family: SimSun; line-height: 26px; text-align: left;"><span style="font-family: SimSun;"></span></div><span style="font-family:SimSun;color:#333333;"><span style="line-height: 26px;">tm.put(new Student("alisi4",24),"wuhan");tm.put(new Student("lisi1",21),"tianjin");tm.put(new Student("lisi2",22),"shanghai");</span></span><div style="color: rgb(51, 51, 51); font-family: SimSun; line-height: 26px; text-align: left;"><span style="font-family: SimSun;">Map.Entry<Student,String> me = it.next();</span></div><span style="font-family:SimSun;color:#333333;"><span style="line-height: 26px;">Set<Map.Entry<Student,String>> entrySet = tm.entrySet();Iterator<Map.Entry<Student,String>> it = entrySet.iterator();while(it.hasNext()){Student stu = me.getKey();</span></span><div style="color: rgb(51, 51, 51); font-family: SimSun; line-height: 26px; text-align: left;"><span style="font-family: SimSun;">}</span></div><span style="font-family:SimSun;color:#333333;"><span style="line-height: 26px;">String addr = me.getValue();System.out.println(stu+":::"+addr);}</span></span><div style="color: rgb(51, 51, 51); font-family: SimSun; line-height: 26px; text-align: left;"><span style="font-family: SimSun;">}</span></div>

八、TreeMap练习2

import java.util.*;class  MapTest3{public static void main(String[] args) {String s= charCount("ak+abAf1c,dCkaAbc-defa");System.out.println(s);}public static String charCount(String str){char[] chs = str.toCharArray();         //泛型接受引用数据类型,不能存放char,只能存放基本数据类型包装类CharcterTreeMap<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();}}








0 0