您的位置:首页 > 编程语言 > Java开发

Java 集合Map --16

2015-08-18 17:03 519 查看

Map集合

该集合存储键值对。

一对一对往里存,而且要保证键的唯一性

1.添加元素




2.删除

clear()

3.判断

containsKey(Object key)

containsValue(Object value)

4.获取

get(Object key)

size()

values()

entrySet()

keySet()

Map集合:

|–Hashtable

底层是哈希表数据结构,不可以存入null值和null健。该集合是线程同步的,jdk1.0出现的,效率低

|–HashMap

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

|–TreeMap

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

和Set很像,Set底层就是使用了Map集合

[code]import java.util.*;

class MapDemo
{
    public static void main(String[] args)
    {
        Map<Integer,String> map = new HashMap<Integer,String>();

        //添加元素
        //put方法新的值会替换老的值
        map.put(01,"zhangsan1");
        map.put(02,"zhangsan2");
        map.put(03,"zhangsan3");

        sop("containsKey:"+map.containsKey(02));
        sop("containsValue:"+map.containsValue("zhangsan3"));
        //sop("remove:"+map.remove(02));

        //获取
        sop("get:"+map.get(02));

        sop(map);

        //获取map集合中所有的值
        Collection<String> coll = map.values();

        sop(coll);

        //HashMap可以使用null值和null健
        map.put(null,"haha");
        sop("get:"+map.get(null));
    }

    public static void sop(Object obj)
    {
        System.out.println(obj);
    }
}




map集合的两种取出方式

1,Set keySet

将map中所有的键存入到Set集合,因为set具备迭代器。

所以可以迭代方式取出所有的键。再根据get方法,获取每一个键对应的值

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

[code]import java.util.*;

class MapDemo22
{
    public static void main(String[] args)
    {
        Map<String,String> map = new HashMap<String,String>();

        //添加元素
        map.put("02","zhangsan1");
        map.put("03","zhangsan2");
        map.put("04","zhangsan3");
        map.put("01","zhangsan4");

        //先获取map集合 所有键的Set集合,keySet();
        Set<String> keySet = map.keySet();

        //有了Set集合,就可以获取其迭代器
        Iterator<String> it = keySet.iterator();

        while(it.hasNext())
        {
            String key = it.next();

            //有了键可以通过map集合的get方法获取其对应的值

            String value = map.get(key);
            System.out.println("key:"+key+",value:"+value);
        }
    }   
}







Map.Entry 其实Entry也是一个接口,它是Map接口中的一个内部接口

[code]interface Map
{
    public static interface Entry
    {
        public abstract Object getKey();
        public abstract Object getValue();
    }
}

class HashMap implements Map.Entry
{   
    class Hash implements Map.Entry
    {
        public Object getKey(){}
        public Object getValue(){}
    }

}


[code]import java.util.*;

class MapDemo2
{
    public static void main(String[] args)
    {
        Map<String,String> map = new HashMap<String,String>();

        //添加元素
        map.put("02","zhangsan1");
        map.put("03","zhangsan2");
        map.put("04","zhangsan3");
        map.put("01","zhangsan4");

        //将Map集合中的映射关系取出。存入到Set集合中。
        Set<Map.Entry<String,String>> entrySet = map.entrySet();

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

        while(it.hasNext())
        {
            Map.Entry<String,String> me = it.next();//获取关系

            String key = me.getKey();//获取键
            String value = me.getValue();//获取值

            System.out.println(key+":"+value);
        }
    }
}




练习

每一个学生都有对应的归属地

学生属性:姓名,年龄

注意:姓名年龄相同视为同一个学生

保证学生的唯一性

1,描述学生

2,定义map容器,将学生作为健,地址作为值,存入

3,获取map集合中的元素

[code]import java.util.*;
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 name+".."+age;
    }
}

class MapTest
{
    public static void main(String[] args)
    {
        HashMap<Student,String> hm = new HashMap<Student,String>();

        hm.put(new Student("lisi03",22),"beijing");
        hm.put(new Student("lisi03",23),"beijing");
        hm.put(new Student("lisi02",21),"chongqing");
        hm.put(new Student("lisi02",21),"wuhan");
        hm.put(new Student("lisi00",25),"jilin");
        hm.put(new Student("lisi",20),"shanghai");

        //第一种取出方式  keySet
        Set<Student> keySet = hm.keySet();
        Iterator<Student> it = keySet.iterator();

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

        //第二种取出方式  entrySet
        Set<Map.Entry<Student,String>> entrySet = hm.entrySet();

        Iterator<Map.Entry<Student,String>> iter = entrySet.iterator();

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




可以排序的Map集合。TreeMap

[code]import java.util.*;

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;

    }
}

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

        tm.put(new Student("lisi6",23),"nanjing");
        tm.put(new Student("alisi1",22),"beijing");
        tm.put(new Student("lisi4",21),"shanghai");
        tm.put(new Student("blisi3",24),"wuhan");
        tm.put(new Student("lisi2",25),"tianjin");

        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);
        }
    }
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: