黑马程序员-Map集合部分

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

Map集合,与Collection集合的不同?

Map集合主要用来存放键-值对的,把键-值这一对映射存放进去。

(一)Map集合的常用方法:

1)put(key,alue)
putAll()添加
添加元素,当添加两个相同的键,后来的将覆盖原有值,并put(key,value)返回被覆盖的值。
2)clear清空集合
remove(key)删除集合中某一个元素
3)判断:
containsKey()是否存在某个键
containsValue()是否存在某个值
isEmpty()集合是否为空
4)获取
get(key)通过某个键获取对应的值
size()获取集合的大小
alues()获取集合中所有的值
5)集合中输出元素的两种方法。Map集合中取出原理:将集合转为Set集合,再利用迭代器一一取出。
keySet():返回Map集合中所有的键,返回到set集合中,而Set中有迭代器,可以一一拿到各个键,再根据get(key)可以拿到键所对应的值。
entrySet():将map中的映射关系取出,即Map.Entry(key,value),返回到Set

示例1:学生(姓名和年龄相同视为同一个人)作为键,学生地址(String)作为值存入Map集合,并取出。

class Student implements Comparable
{
    private String name;
    private int age;
    public Student(String name,int age)
    {
        this.name = name;
        this.age = age;
    }
    public int hashCode()
    {
        return name.hashCode()+age*37;
    }
    public boolean equals(Object obj)
    {
        if(Object instanceof Student)
            throw new ClassCastException("类型不匹配")
        Student s = (Student)obj;
        return this.name.equals(s.name)&&this.age==s.age
    }
    public int compareTo(Student s)
    {
        num = this.name.compareTo(s.name);
        if(num==0)
            return new Integer(this.age).compareTo(new Integer(s.age));
        return num;
    }
    public int getAge()
    {
        return this.age;
    }
    public int getName()
    {
        return this.name;
    }
    public String toString()
    {
        return this.name+":::"+this.age;
    }
}
public class MapTest
{
    public static void main(String args[])
    {
        HashMap hm = new HashMap();
        hm.add(new Student("lisi01",23),"北京");
        hm.add(new Student("lisi02",22),"上海");
        hm.add(new Student("lisi03",25),"广州");
        hm.add(new Student("lisi04",24),"南京");
        hm.add(new Student("lisi02",22),"上海");
        hm.add(new Student("lisi01",26),"杭州");

        Set set = hm.keySet();
        Iterator it = set.iterator();
        while(it.hasNext())
        {
            Person key = it.next();
            String value = hm.get(key);
            System.out.println(key.toString+"....."+value);
        }

        Set set2 = hm.entrySet();
        Iterator it = set.iterator();
        while(it.hasNext())
        {
            Map.Entry entry = it.next();
            Person key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key.toString+"....."+value);
        }

    }
}

(二)Map的常用子类有三个:

1) HashTable:底层用的数据结构为哈希表,是线程同步,不能存入空的键和值,jdk1.0,效率不高。
2) HashMap:底层用的数据结构为哈希表,是线程不同步,能存入null的键和值,jdk1.2,可以替代HashTable,效率较高。
3)TreeMap:底层用的二叉树数据结构,是线程不同步,能够对键进行排序。
其实Map与Set很像,因为Set底层实际上用的是Map。

示例2:利用TreeMap来实现学生按照年龄升序打印(指定比较器)。
首先定义比较器。

class MyComparator implements Comparator
{
    public int compare(Student s1,Student s2)
    {
        int num = new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
        if(num==0)
            return s1.getName().compareTo(s2.getName());
        return num;
    }
}
public class MapTest
{
    public static void main(String args[])
    {
        HashMap hm = new HashMap(new MyComparator());
        hm.add(new Student("lisi01",23),"北京");
        hm.add(new Student("lisi02",22),"上海");
        hm.add(new Student("lisi03",25),"广州");
        hm.add(new Student("lisi04",24),"南京");
        hm.add(new Student("lisi02",22),"上海");
        hm.add(new Student("lisi01",26),"杭州");

        Set set = hm.keySet();
        Iterator it = set.iterator();
        while(it.hasNext())
        {
            Person key = it.next();
            String value = hm.get(key);
            System.out.println(key.toString+"....."+value);
        }

        Set set2 = hm.entrySet();
        Iterator it = set.iterator();
        while(it.hasNext())
        {
            Map.Entry entry = it.next();
            Person key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key.toString+"....."+value);
        }
    }
}

示例3:“shfshfdlfhdoshfljeionfn”字符串中,每个字母出现的次数?打印格式为a(1)b(4)…..

class StringTimeDemo
{
    public static void main(String args[])
    {
        String str = "shfshfdlfhdoshfljeionfn";
        TreeMap hm = new TreeMap();
        int num = 0;
        for(int i=0;i<str.length();i++)
        {
            Char ch = str.charAt(i);
            if(!(ch>'a'&&ch<'z'||ch>'A'&&ch<'Z'))
                continue;
            int value = hm.get(ch);
            if(hm!=null)
                num = value;
            num++;
            hm.put(ch,num);
            num = 0;
        }
        Set entry = hm.entrySet();
        Iterator it = entry.iterator();
        while(it.hasNext())
        {
            Map.Entry me = it.next();
            Char ch = me.getKey();
            int num = me.getValue();
            System.out.println(ch+"("+num+")");
        }
    }
}

此篇文章为Map集合的简单介绍!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值