黑马程序员_学习笔记第16天——Map集合

来源:互联网 发布:欧洲卡车模拟 mac 编辑:程序博客网 时间:2024/05/23 23:09
---------------------- ASP.Net+Android+IOS开发、href="http://edu.csdn.net"target="blank">.Net培训、期待与您交流! ----------------------

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

添加:put(K  key,  V  value)      putAll(Map<? extends K ,  ? extends V>  m)

删除:clear()     remove(Object key)

判断:containsValue(Object  value)     containsKey(Object  key)    isEmpty()

获取:get(Object  key)    size()    values()    entrySet()     keySet()

2、Map

-->Hashtable :底层是哈希表数据结构,不可以存入null键null值,该集合是线程同步的。jdk1.0 效率低

-->HashMap:底层是哈希表数据结构,允许使用null键和null值,该集合是不同步的。jdk1.2 效率高

-->TreeMap:底层是二叉树数据结构,线程不同步。可以用于给map集合中的键进行排序。

和Set集合很像,其实Set集合底层就是用了Map集合。

3、添加元素,如果出现添加是相同的键,那么后添加的值将覆盖原来的值,并返回原来被覆盖的值。

4、HashMap中如果调用get()方法时,返回值为null,则不一定不存在这个键,因为有可能其值就是空。

5、map集合的两种取出方式:

Set<k>keyset:将map中所有的键存入到Set集合中,因为set集合具备迭代器,所以可以迭代器方法取出所有的键,再根据get方法,获取每一个键对应的值。

Map集合的取出原理:将map集合转成set集合,再通过迭代器取出。

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();Iterator<String> it = keyset.iterator();while(it.hasNext()) {String key = it.next();String value = map.get(key);System.out.println("key:"+key+";value:"+value);}

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

interface Map {public static interface Entry {public abstract Object getKey();public abstract Object getValue();}}class HashMap implements Map {class Haha implements Map.Entry {public Object getKey(){}public Object getValue(){}}}
6、练习

public class MapTest1 {public static void main(String[] args) {Map<Student , String> map = new HashMap<Student,String>();map.put(new Student("lisi1",12), "beijing");map.put(new Student("lisi2",16), "shanghai");map.put(new Student("lisi3",14), "nanjing");map.put(new Student("lisi4",13), "wuhan");/*Set<Student> keyset = map.keySet();Iterator<Student> it = keyset.iterator();while(it.hasNext()) {Student key = it.next();String value = map.get(key);System.out.println(key+"="+value);}*/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 stu = me.getKey();String addr = me.getValue();System.out.println(stu+"="+addr);}}}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 getName() + "...." +getAge();}}

public class TreeMapTest {public static void main(String[] args) {TreeMap<Student, String> tm = new TreeMap<Student,String>(new StuNameComparator());tm.put(new Student("lisi1",12), "beijing");tm.put(new Student("lisi2",16), "shanghai");tm.put(new Student("lisi3",14), "nanjing");tm.put(new Student("lisi4",13), "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);}}}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;}}


7、什么时候使用map集合?

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

8、练习

//"sdfgzxcvasdfxcvdf"获取该字符串中的字母出现的次数,打印形式为:a(1)c(2)...public class MapTest2 {public static void main(String[] args) {String s = charCount("sdfgzxc=vas-dfx+cvdf");System.out.println(s);}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++;tm.put(chs[x], value);}*/}StringBuilder sb = new StringBuilder();Set<Character> keyset = tm.keySet();Iterator<Character> it = keyset.iterator();while(it.hasNext()) {Character c = it.next();Integer i = tm.get(c);sb.append(c+"("+i+")");}return sb.toString();}}

public class MapTest3 {public static void main(String[] args) {/*HashMap<String,HashMap<String,String>> school = new HashMap<String,HashMap<String,String>>();HashMap<String,String> yure = new HashMap<String,String>();HashMap<String,String> jiuye = new HashMap<String,String>();school.put("yurejiaoshi", yure);school.put("jiuyejiaoshi", jiuye);yure.put("01", "zhangsan");yure.put("02", "lisi");jiuye.put("01","wangwu");jiuye.put("02", "zhaoliu");getStudentInfo(yure);}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+"::"+name);}*/demo();}public static void demo() {HashMap<String,List<Student2>> school = new HashMap<String,List<Student2>>();List<Student2> reyu = new ArrayList<Student2>();List<Student2> yejiu = new ArrayList<Student2>();reyu.add(new Student2("01","zhangsan"));reyu.add(new Student2("02", "lisi"));yejiu.add(new Student2("01","wangwu"));yejiu.add(new Student2("02", "zhaoliu"));school.put("reyujiaoshi", reyu);school.put("yejiujiaoshi", yejiu);Iterator<String> it = school.keySet().iterator();while(it.hasNext()) {String jiaoshi = it.next();List<Student2> room = school.get(jiaoshi);System.out.println(jiaoshi);getInfos(room);}}public static void getInfos(List<Student2> list) {Iterator<Student2> it = list.iterator();while(it.hasNext()) {Student2 s = it.next();System.out.println(s);}}}class Student2 {private String id ;private String name;Student2(String id , String name) {this.id = id;this.name = name;}public String toString() {return name+":"+id;}}/*class MapDemo2 {public static void demo() {HashMap<String,List<Student>> school = new HashMap<String,List<Student>>();List<Student> reyu = new ArrayList<Student>();List<Student> yejiu = new ArrayList<Student>();reyu.add(new Student("01","zhangsan"));reyu.add(new Student("02", "lisi"));yejiu.add(new Student("01","wangwu"));yejiu.add(new Student("02", "zhaoliu"));Iterator<String> it = school.keySet().iterator();while(it.hasNext()) {String jiaoshi = it.next();List<Student> room = school.get(jiaoshi);System.out.println(jiaoshi);getStudentInfo(room);}}public static void getStudentInfo(List<Student> list) {Iterator<Student> it = list.iterator();while(it.hasNext()) {Student s = it.next();System.out.println(s);}}}*/



---------------------- ASP.Net+Android+IOS开发、href="http://edu.csdn.net"target="blank">.Net培训、期待与您交流! ----------------------详细请查看:http://edu.csdn.net
0 0