黑马程序员技术交流社区

标题: 【阳哥笔记】极速秒杀Java基础之笔记系列—Day18(集合 )! [打印本页]

作者: 阳哥忠粉    时间: 2015-6-7 12:04
标题: 【阳哥笔记】极速秒杀Java基础之笔记系列—Day18(集合 )!





笔记总链接:http://bbs.itheima.com/thread-200600-1-1.html

6、集合

6.2 集合类

    6.2.5 Map、HashMap、TreeMap

    Map:一次添加一对元素,Collection一次添加一个元素。
    Map也称为双列集合,Collection集合称为单列集合。
    其实Map集合中存储的就是键值对。
    map集合中必须保证键的唯一性。

   
P.S.    关于泛型相关的知识请参看Java高级知识笔记:http://bbs.itheima.com/thread-107670-1-1.html

    常用方法:
    1、添加
    value put(key,value):返回前一个和key关联的值,如果没有返回null。

    2、删除
    void clear():清空map集合。
    value remove(Object key):根据指定的key删除这个键值对。

    3、判断
    boolean containsKey(key);
    boolean containsValue(value);
    boolean isEmpty();

    4、获取
    value get(key):通过键获取值,如果没有该键返回null。
                            当然可以通过返回null,来判断是否包含指定键。
    int size():获取键值对个数。


    示例1:
  1. import java.util.HashMap;
  2. import java.util.Map;

  3. public class MapDemo{
  4.        public static void main(String[] args){
  5.             Map<Integer,String> map = new HashMap<Integer,String>();
  6.              method(map);
  7.       }

  8.        public static void method(Map<Integer,String> map){ //学号和姓名
  9.              //添加元素
  10.             System. out.println(map.put(8,"旺财" ));
  11.             System. out.println(map.put(8,"小强" ));
  12.             System. out.println(map);

  13.             map.put(2, "张三");
  14.             map.put(7, "赵六");
  15.             System. out.println(map);

  16.              //删除
  17.             System. out.println("remove:" + map.remove(2));

  18.              //判断
  19.             System. out.println("containsKey:" + map.containsKey(7));

  20.              //获取
  21.             System. out.println("get:" + map.get(7));
  22.       }
  23. }
复制代码
   运行结果:

    获取Map集合元素并打印方式一:

    示例2:
  1. import java.util.HashMap;
  2. import java.util.Iterator;
  3. import java.util.Map;
  4. import java.util.Set;

  5. public class MapDemo{
  6.        public static void main(String[] args){
  7.             Map<Integer,String> map = new HashMap<Integer,String>();
  8.             method(map);
  9.       }

  10.        public static void method(Map<Integer,String> map){
  11.             map.put(8, "王五");
  12.             map.put(2, "赵六");
  13.             map.put(7, "小强");
  14.             map.put(6, "旺财");

  15.              //取出map中的所有元素。
  16.              //原理,通过keySet方法获取map中所有的键所在的set集合,在通过set的迭代器获取到每一个键。
  17.              //再对每一个键通过map集合的get方法获取其对应的值即可。

  18.             Set<Integer> keySet = map.keySet();
  19.             Iterator<Integer> it = keySet.iterator();

  20.              while(it.hasNext()){
  21.                   Integer key = it.next();
  22.                   String value = map.get(key);
  23.                   System.out.println(key + ":" + value);
  24.             }
  25.       }
  26. }
复制代码
    运行结果:

    获取Map集合元素并打印方式二:

    示例3:
  1. import java.util.HashMap;
  2. import java.util.Iterator;
  3. import java.util.Map;
  4. import java.util.Set;

  5. public class MapDemo{
  6.        public static void main(String[] args){
  7.             Map<Integer,String> map = new HashMap<Integer,String>();
  8.              method(map);
  9.       }

  10.        public static void method(Map<Integer,String> map){
  11.             map.put(8, "王五");
  12.             map.put(2, "赵六");
  13.             map.put(7, "小强");
  14.             map.put(6, "旺财");

  15.              /*
  16.             通过Map转成Set就可以迭代。
  17.             找到了另一个方法,entrySet。
  18.             该方法将键和值的映射关系作为对象存储到了Set集合中,而这个映射关系的类型就是Map.Entry类型
  19.             */
  20.             Set<Map.Entry<Integer,String>> entrySet = map.entrySet();

  21.             Iterator<Map.Entry<Integer,String>> it = entrySet.iterator();

  22.              while(it.hasNext()){
  23.                   Map.Entry<Integer,String> me = it.next();
  24.                   Integer key = me.getKey();
  25.                   String value = me.getValue();
  26.                   System. out.println(key + ":" + value);
  27.             }
  28.       }
  29. }
复制代码
   运行结果:

    获取Map集合元素并打印方式三:

    示例4:
  1. import java.util.Collection;
  2. import java.util.HashMap;
  3. import java.util.Iterator;
  4. import java.util.Map;

  5. public class MapDemo{
  6.        public static void main(String[] args){
  7.             Map<Integer,String> map = new HashMap<Integer,String>();
  8.              method(map);
  9.       }

  10.        public static void method(Map<Integer,String> map){
  11.             map.put(8, "王五");
  12.             map.put(2, "赵六");
  13.             map.put(7, "小强");
  14.             map.put(6, "旺财");

  15.             Collection<String> values = map.values();

  16.             Iterator<String> it = values.iterator();
  17.              while(it.hasNext()){
  18.                   System. out.println(it.next());
  19.             }
  20.       }
  21. }
复制代码
    运行结果:

    Map常用的子类:
          |--Hashtable:内部结构是哈希表,是同步的。不允许null作为键,null作为值。
               |--Properties:用来存储键值对型的配置文件的信息,可以和IO技术相结合。
          |--HashMap:内部结构式哈希表,不是同步的。允许null作为键,null作为值。
          |--TreeMap:内部结构式二叉树,不是同步的。可以对Map结合中的键进行排序。

    hashSet实现Set接口,由哈希表(实际上是一个HashMap实例)支持。


    示例5:
  1. import java.util.HashMap;
  2. import java.util.Iterator;

  3. class Student {
  4.        private String name;
  5.        private int age;

  6.        public Student(){
  7.       }

  8.        public Student(String name,int age){
  9.              this.name = name;
  10.              this.age = age;
  11.       }

  12.        public void setName(String name){
  13.              this.name = name;
  14.       }

  15.        public String getName(){
  16.              return this .name;
  17.       }

  18.        public void setAge(int age){
  19.              this.age = age;
  20.       }

  21.        public int getAge(){
  22.              return this .age;
  23.       }

  24.        public int hashCode() {
  25.              final int prime = 31;
  26.              int result = 1;
  27.             result = prime * result + age;
  28.             result = prime * result + ((name == null) ? 0 : name.hashCode());
  29.              return result;
  30.       }

  31.        public boolean equals(Object obj) {
  32.              if (this == obj)
  33.                    return true ;
  34.              if (obj == null)
  35.                    return false ;
  36.              if (getClass() != obj.getClass())
  37.                    return false ;
  38.             Student other = (Student) obj;
  39.              if (age != other.age)
  40.                    return false ;
  41.              if (name == null) {
  42.                    if (other.name != null)
  43.                          return false ;
  44.             } else if (!name.equals(other.name))
  45.                    return false ;
  46.              return true ;
  47.       }
  48. }

  49. public class HashMapDemo{
  50.        public static void main(String[] args){
  51.              //将学生对象和学生的归属地通过键与值存储到map集合中
  52.             HashMap<Student,String> hm = new HashMap<Student,String>();

  53.             hm.put( new Student("lisi" ,38),"北京");
  54.             hm.put( new Student("zhaoliu" ,24),"上海");
  55.             hm.put( new Student("xiaoqiang" ,31),"沈阳");
  56.             hm.put( new Student("wangcai" ,28),"大连");
  57.             hm.put( new Student("zhaoliu" ,24),"铁岭");
  58.             
  59.             Iterator<Student> it = hm.keySet().iterator();

  60.              while(it.hasNext()){
  61.                   Student key = it.next();
  62.                   String value = hm.get(key);
  63.                   System.out.println(key.getName() + ":" + key.getAge() + "---" + value);
  64.             }
  65.       }
  66. }
复制代码
   运行结果:

    P.S.
    键有了判断依据,HashMap中的值就被覆盖。

    示例6:
  1. import java.util.Comparator;
  2. import java.util.Iterator;
  3. import java.util.Map;
  4. import java.util.TreeMap;

  5. class ComparatorByName implements Comparator<Student>{
  6.        public int compare(Student s1,Student s2){
  7.              int temp = s1.getName().compareTo(s2.getName());
  8.              return temp == 0?s1.getAge() - s2.getAge():temp;
  9.       }
  10. }

  11. public class HashMapDemo{
  12.        public static void main(String[] args){
  13.              //将学生对象和学生的归属地通过键与值存储到map集合中
  14.             TreeMap<Student,String> tm = new TreeMap<Student,String>(new ComparatorByName());

  15.             tm.put( new Student("lisi" ,38),"北京");
  16.             tm.put( new Student("zhaoliu" ,24),"上海");
  17.             tm.put( new Student("xiaoqiang" ,31),"沈阳");
  18.             tm.put( new Student("wangcai" ,28),"大连");
  19.             tm.put( new Student("zhaoliu" ,24),"铁岭");
  20.             
  21.             Iterator<Map.Entry<Student,String>> it = tm.entrySet().iterator();

  22.              while(it.hasNext()){
  23.                   Map.Entry<Student,String> me = it.next();
  24.                   Student key = me.getKey();
  25.                   String value = me.getValue();
  26.                   System.out.println(key.getName() + ":" + key.getAge() + "---" + value);
  27.             }
  28.       }
  29. }
复制代码
   运行结果:

~END~



~爱上海,爱黑马~



作者: 1014914737    时间: 2015-6-7 12:17
收到.....................
作者: 顾浩    时间: 2015-6-7 13:57
完全跟不上速度的说~~~~~~
作者: 流水0215    时间: 2015-6-9 09:11
不错,赞一个
作者: 魏海洲    时间: 2015-6-16 21:18

完全跟不上速度的说~~~~~~
作者: q19871127    时间: 2015-6-16 21:42
支持下 非常好
作者: 魏海洲    时间: 2015-6-16 21:48
不错,赞一个
作者: 魏海洲    时间: 2015-6-19 19:17

不错,赞一个
作者: 魏海洲    时间: 2015-6-25 18:56

完全跟不上速度的说~~~~~~
作者: Z、c    时间: 2015-7-5 11:03
每日学习签到
作者: 星痕-凌    时间: 2015-7-7 21:15
顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶!!!!!!!!!
作者: twa42    时间: 2015-7-13 17:07
谢谢阳哥
作者: fever_ai_my    时间: 2015-7-13 17:16
必须赞一个
作者: joerk    时间: 2015-7-14 21:35
马上就要学到集合了,阳哥的笔记真好,学习不苦恼。。。
作者: 搁浅丶那份    时间: 2015-7-14 23:45
感谢阳哥,大爱阳哥
作者: 星痕-凌    时间: 2015-7-18 20:59
很不错,感觉很详尽,努力学好!!!!!!!!!!!!!!
作者: SuperApollo    时间: 2015-7-30 21:45
太牛了,支持
作者: jaunce    时间: 2015-7-30 22:07
收到/@!!!!!!!!!!!!!!
作者: 水佳伟    时间: 2015-7-30 22:08
楼主好人,长知识了
作者: 思考。。。    时间: 2015-7-31 16:57
复习用太棒了!!!
作者: 放心飞    时间: 2015-8-31 10:50
看完一节,收益匪浅,加油!
作者: weiyuning    时间: 2015-8-31 14:59
上完基础班了,来重新复习一下
作者: yangxuqiang    时间: 2015-9-29 22:30
头都大了
作者: yqlbd    时间: 2015-11-14 15:20
看完了,非常感谢。
作者: xing912    时间: 2015-11-14 15:56
先下下来存着
作者: lktz    时间: 2016-2-13 02:36
标记一下...
作者: 我不会Java    时间: 2018-5-22 10:14
谢谢分享




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2