黑马程序员--第十六天:map
2013-06-18 19:41
190 查看
---------------------- ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ----------------------
---------------------- ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ----------------------
//16-1 import java.util.Collection; import java.util.HashMap; import java.util.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:底层是Hash表数据结构,不可能存入null(键和值都不行)。该集合是线程同步的 jdk 1.0 效率低 * |--HashMap:底层是Hash表数据结构,可以使用null(键和值都可以),线程不同步。 jdk 1.2 效率高 * |--TreeMap:底层是二叉树数据结构。线程不同步。可以给map集合中的键进行排序。 * * 和Set很像。 * 其实,Set底层使用的就是Map集合。 */ public class DS { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Map<String,String> map = new HashMap<String,String>(); map.put("01","mike01"); map.put("01","mike04");//put 相同的键,put用新的值替换旧的值,并返回旧的值。 map.put("02","mike01"); map.put("03","mike01"); System.out.println("containsKey: "+map.containsKey("01")); System.out.println("remove: "+map.remove("01")); System.out.println(map); System.out.println("get: "+ map.get("0123")); map.put(null, "val-null"); System.out.println("get null "+ map.get(null)); //可以通过get方法来判断 一个键是否存在,但需要注意value 为 null 的情况。 Collection<String> coll = map.values(); System.out.println(coll); System.out.println(map); } } //16-4_5 package com.eclipsedemo; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; /* *Map 集合的两种取出方法。 *keySet: 将map中所有的键存入到Set集合。因为Set具备迭代器。 * 所以可以用迭代器方式取出所以的键,再根据get方法获取每一个键对应的值。 * Map 集合取出的原理: 将map集合转换成Set集合,再通过迭代器取出。 * *Set<Map.Entry<k,v>> entrySet; * 将Map集合中映射关系取出,存入到Set集合中。而这个关系的数据类型就是Map.Entry。 * */ public class DS { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Map<String,String> map = new HashMap<String,String>(); map.put("01","mike01"); map.put("05","mike04"); map.put("02","mike01"); map.put("03","mike01"); //用 map.entrySet(); Set<Map.Entry<String, String>> entrySet = map.entrySet(); Iterator<Map.Entry<String, String>> ite = entrySet.iterator(); while(ite.hasNext()){ Map.Entry<String, String> en = ite.next(); String key = en.getKey(); String value = en.getValue(); System.out.println(key+".."+value); } //获取所有键的Set集合 //用map.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); } } } //16-6 import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Set; /* *每一个学生都有对应的归属地。 学生Student,地址String 学生属性:姓名,年龄 注意:姓名和年龄相同视为同一学生。 保证学生的唯一性。 1.描述学生。 2.定义map容器。将学生作为键,地址作为值,存入。 3.获取map集合的元素。 */ public class DS { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub HashMap<Student,String> hm = new HashMap<Student,String>(); hm.put(new Student("mike",20),"new York"); hm.put(new Student("mik",33),"new k"); hm.put(new Student("mke",25),"n York"); hm.put(new Student("mi",30),"new York"); Iterator <Student> it = hm.keySet().iterator(); while(it.hasNext()){ Student s = it.next(); String addr = hm.get(s); System.out.println(s+"..."+addr); } Set<Map.Entry<Student, String>> setM = hm.entrySet(); Iterator<Map.Entry<Student, String>> itm = setM.iterator(); while(itm.hasNext()){ Map.Entry<Student, String> en =itm.next(); Student s = en.getKey(); String addr = en.getValue(); System.out.println(s+"..."+addr); } } } class Student implements Comparable<Student>{ private String name; private int age; Student(String name, int age){ this.name = name; this.age = age; } @Override public int hashCode() { // TODO Auto-generated method stub return super.hashCode(); } @Override public boolean equals(Object obj) { // TODO Auto-generated method stub 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; } @Override public String toString() { // TODO Auto-generated method stub return this.name+ "..." + this.age; } @Override public int compareTo(Student s) { // TODO Auto-generated method stub int num = this.age - s.age; if (0 == num){ return this.name.compareTo(s.name); } return num; } } //16-8 package com.eclipsedemo; import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.TreeMap; public class MyDemo { public static void main(String []args){ TreeMap<Character, Integer> tm = new TreeMap<Character, Integer>(); String s = "abadsfasdfcvczxeadfasdfadfgjtfjyiliuopmvhjnmeartwrgfcbfnhrwqdas"; char[] chs = s.toCharArray(); StringBuilder sb = new StringBuilder(); for (char c : chs){ Integer value = tm.get(c); if(value == null) value = 0; value++; tm.put(c, value); } Set<Map.Entry<Character, Integer>> set = tm.entrySet(); Iterator<Map.Entry<Character, Integer>> it = set.iterator(); while(it.hasNext()){ Map.Entry<Character, Integer> mp = it.next(); System.out.println(mp.getKey()+" = "+mp.getValue()); sb.append(mp.getKey()+"("+mp.getValue()+")"); } System.out.println(sb.toString()); } }
---------------------- ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ----------------------
相关文章推荐
- 黑马程序员——>第十六天<集合框架(Map)>
- 黑马程序员_源自梦想 Collection、Map、Collections
- 黑马程序员_Map集合
- 黑马程序员——Java基础——集合(下)之Map
- 黑马程序员 Map 集合小结
- 黑马程序员之泛型和Map集合学习
- 黑马程序员-Java 集合框架(二)-Map、Collections、新特性
- 黑马程序员——Map
- 黑马程序员——javaSE_集合框架-Map
- 黑马程序员——第七篇:Map集合、collection工具类
- 黑马程序员—Map集合
- 黑马程序员--11--Map集合
- 黑马程序员——java集合框架(Map和工具类)
- 黑马程序员---------Map集合和一些集合工具类
- 黑马程序员——Map集合
- 黑马程序员:集合框架Map
- 黑马程序员---Map集合 学习笔记
- 黑马程序员--Map集合
- 黑马程序员-JAVASE入门(Map集合)
- 黑马程序员—16—java基础:有关map结合的学习笔记和心得体会