集合——Map

这里写图片描述


Map集合: 该集合存储键值对。一对一往里存,而且要保证键的唯一性
1.添加
    put(K key, V value) 
          将指定的值与此映射中的指定键关联(可选操作)。
    putAll(Map<? extends K,? extends V> m) 
          从指定映射中将所有映射关系复制到此映射中(可选操作)。
2.删除
    clear()

3.获取
    get(Object key) 
        返回指定键所映射的值;如果此映射不包含该键的映射关系,则返回 null。
    values()
    size()

    ****entrySet()****
    keySet()
4.判断
    containsKey(Object key) 
        如果此映射包含指定键的映射关系,则返回 true
    containsValue(Object value) 
        如果此映射将一个或多个键映射到指定值,则返回 true|--HashMap  底层是哈希表数据结构,不可以存入null键,null值。该集合是线程同步的 jdk1.0
  |--Hashtable: 底层是哈希数据结构,允许null值和null键,该集合是不同步的。jdk1.2 效率高
  |--TreeMap : 底层是二叉树数据结构,线程不同步可以用于给map集合中的键排序
                和set很像,其实set底层就是使用了map集合


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

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

2.Set<Map.Entry<>>  entrySet:将map集合中的映射关系存入到set集合中
                             而这个关系的数据类型就是:Map.Entry
         Map.Entry()方法是将Map集合中的映射关系取出,存入到Set集合中 

  interface Map
  {
        public static interface Entry    Entry为一个内部接口
        {
            public abstract Object getKey();
            public abstract Object getValue();
        }
  }

   class HashMap implemts Map
   {
      class hah implemts Map.Entry
      {
        public Object getKey(){}
        public Object getValue(){}
      }
   }

map方法中没有迭代器


/* 练习(如果姓名和年龄相同视为同一人)
1.描述学生

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

3.获取Map集合中的元素
*/
import java.util.*;
class Student2 implements Comparable<Student2>
{
    private String name;
    private int age;

    Student2(String name,int age)
    {
        this.name = name;
        this.age  =age;
    }

    public int getAge()
    {
        return age;
    }

    public String getName()
    {
        return name;
    }

    public String toString()
    {
        return name+":"+age; 
    }

    public int hashCode()
    {
        return name.hashCode() + age*34;
    //  return 1;
    }

    public boolean equals(Object obj)
    {
        if(!(obj instanceof Student2))
            throw new ClassCastException("类型转化异常");

        Student2 s = (Student2)obj;

        return this.name.equals(s.name) && this.age==s.age;
    }

    public int compareTo(Student2 s)
    {
        int num = new Integer(this.age).compareTo(new Integer(s.age));

        if(num == 0)
            return this.name.compareTo(s.name);
        return num;
    }
}
class MapDemo3
{
    public static void main(String[] args)
    {
        HashMap<Student2,String> hm = new HashMap<Student2,String>();

        hm.put(new Student2("李四1",21),"wuhan");
    //  hm.put(new Student2("李四1",21),"nanning");
        hm.put(new Student2("李四2",22),"yinchan");
        hm.put(new Student2("李四3",23),"xian");
    //  hm.put(new Student2("李四3",23),"beijing");
        hm.put(new Student2("李四4",24),"shenzheng");

        //取得方式有两种方法
        //第一种方法是keySet方法
        Set<Student2> keyset = hm.keySet();
        Iterator<Student2> it = keyset.iterator();

        while(it.hasNext())
        {
            Student2 stu = it.next();
            String addr = hm.get(stu);
            System.out.println(stu+"..."+addr);
        }

        //第二种方法
        Set<Map.Entry<Student2, String>> entrySet = hm.entrySet();
        Iterator<Map.Entry<Student2,String>> iter = entrySet.iterator();

        while(iter.hasNext())
        {
             Map.Entry<Student2,String> me = iter.next();
             Student2 stu2 = me.getKey();
             String addr2 = me.getValue();
             System.out.println(stu2+"........."+addr2);
        }

    }
}

什么时候使用Map集合呢?

当数据之间存在映射关系时,就应该先想到TreeMap


/*****演示2****

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

使用TreeMap集合
*/

import java.util.*;

class StuNameCompare implements Comparator<Student2>
{
    public int compare(Student2 s1, Student2 s2)
    {
        int num = s1.getName().compareTo(s2.getName());

        if (num ==0)
            return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));

        return num;
    }
}

class MapTest
{
    public static void main(String[] args)
    {
        TreeMap<Student2, String> tm = new TreeMap<Student2, String>(new );

        tm.put(new Student2("lisi1",23),"wuhan");
        tm.put(new Student2("lisi2",22),"yinchan");
        tm.put(new Student2("lisi3",21),"xian");
        tm.put(new Student2("lisi4",24),"shenzheng");

        Set<Map.Entry<Student2,String>> entrySet = tm.entrySet();
        Iterator<Map.Entry<Student2,String>> it = entrySet.iterator();

        while(it.hasNext())
        {
            Map.Entry<Student2,String> me = it.next();
            Student2 stu = me.getKey();
            String addr = me.getValue();
            System.out.println(stu+":::"+addr);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值