Java基础 集合Map

Map

Map集合:该集合存储键值对。一对一对往里存。而且要保证键的唯一性。
和Set很像,Set底层就是使用了Map集合

1,添加
    V put(K key, V value) 
    返回:此哈希表中指定键的以前的值;如果不存在该值,则返回 null 

    void putAll(Map< ? extends K,? extends V> m) 

2,删除

     V remove(Object key) 如果存在一个键的映射关系,则将其从此映射中移除(可选操作)。

    void clear()
3,判断
    boolean containsKey(Object key)如果此映射包含指定键的映射关系,则返回 true。
    boolean containsValue(Object value)如果此映射将一个或多个键映射到指定值,则返回 true。
    boolean isEmpty()
4,获取
    V get(Object key)返回指定键所映射的值;如果此映射不包含该键的映射关系,则返回 null。
    Collection<V> values()返回此映射中包含的值的 Collection 视图。
    int size()  
     Set<K> keySet() 返回此映射中包含的键的 Set 视图。
     Set<Map.Entry<K,V>> entrySet() 
      返回此映射中包含的映射关系的 Set 视图。 

基本方法演示

import java.util.*;
class MapDemo
{
    public static void main(String[] args)
    {
        Map<String,String> map = new HashMap<String,String>();

        map.put("01","value1");
        map.put("02","value2");
        map.put("03","value3");
        map.put("01","value001");//覆盖老的值,返回原来的值
        map.put(null,"snull");

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

        System.out.println("containsKey:"+map.containsKey("01"));
        System.out.println("get:"+map.get("01"));//返回value1
        System.out.println("null:"+map.get(null));//返回snull
        //System.out.println("remove:"+map.remove("01"));//返回value1
        System.out.println("remove:"+map.remove("0"));//返回null
        System.out.println("map:"+map);//

        System.out.println(coll);
    }
}

输出:

containsKey:true
get:value001
null:snull
remove:null
map:{null=snull, 01=value001, 02=value2, 03=value3}
[snull, value001, value2, value3]


Hashtable

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

HashMap

底层是哈希表数据结构,允许使用null键 null值
该集合线程不同步。jdk1.2 效率高

keySet()//获取所有键

import java.util.*;
class MapDemo1
{
    public static void main(String[] args)
    {
        Map<String,String> map = new HashMap<String,String>();

        map.put("01","value1");
        map.put("02","value2");
        map.put("03","value3");

        System.out.println("取出所有的键值对");
        Set<String> keys=map.keySet();
        for(Iterator<String> it=keys.iterator();it.hasNext();)
        {
            String k=it.next();
            String v=map.get(k);
            System.out.println(k+":"+v);
        }
        System.out.println("------------------");
    }   
}

输出:
取出所有的键值对
01:value1
02:value2
03:value3
——————

entrySet()//获取所有键值对

import java.util.*;

class MapDemo2
{
    public static void main(String[] args)
    {
        Map<String,String> map=new HashMap<String,String>();
        map.put("k1","v1");
        map.put("k2","v2");
        map.put("k3","v3");

        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();
            System.out.println("key:"+me.getKey()+" value:"+me.getValue());
        }
    }
}

输出:
key:k1 value:v1
key:k2 value:v2
key:k3 value:v3

TreeMap

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


练习

使用 map 存储 学生,地址

学生有 名字,年龄 两个属性

并打印结果使用无序使用 HashMap


import java.util.*;

class MapTest
{
    public static void main(String[] args)
    {
        Map<Student,String> map=new HashMap<Student,String>();
        map.put(new Student("张三",18),"北京");
        map.put(new Student("李四",20),"北京");
        map.put(new Student("大明",25),"上海");
        map.put(new Student("大明",25),"武汉");

        Set<Map.Entry<Student,String>> mySet=map.entrySet();

        Iterator<Map.Entry<Student,String>> ite=mySet.iterator();
        System.out.println(new Student("张三",18).equals(new Student("张三",18)));
        while(ite.hasNext())
        {
            Map.Entry<Student,String> ent=ite.next();
            Student K=ent.getKey();
            System.out.println(K+" Value:"+ent.getValue());
        }
    }

}


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*2;
    }

    public boolean equals(Object obj)
    {
        if(!(obj instanceof Student))
            throw new ClassCastException("类型不匹配");
        Student s=(Student)obj;
        if(name.equals(s.getName()) && age==s.getAge())
            return true;

        return false;
    }
    public String toString()
    {
        return name+":"+age;
    }
    public String getName()
    {
        return name;
    }
    public int getAge()
    {
        return age;
    }
}

输出:
true
大明:25 Value:武汉
张三:18 Value:北京
李四:20 Value:北京

练习2使用 TreeMap

同上题,使用学生姓名,年龄进行排序。
向TreeMap中传入一个比较器,Comparator
比较两个Student的name和age


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(s2.getAge());
        return num;
    }
}
class MapTest2
{
    public static void main(String[] args)
    {
        Map<Student,String> map=new TreeMap<Student,String>(new StuNameComparator());
        map.put(new Student("aa1",25),"北京");
        map.put(new Student("aa2",20),"北京");
        map.put(new Student("bb1",23),"上海");
        map.put(new Student("bb2",28),"武汉");
        map.put(new Student("bb2",18),"武汉");

        Set<Map.Entry<Student,String>> mySet=map.entrySet();

        Iterator<Map.Entry<Student,String>> ite=mySet.iterator();
        System.out.println(new Student("张三",18).equals(new Student("张三",18)));
        while(ite.hasNext())
        {
            Map.Entry<Student,String> ent=ite.next();
            Student K=ent.getKey();
            System.out.println(K+" Value:"+ent.getValue());
        }
    }

}


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*2;
    }

    public boolean equals(Object obj)
    {
        if(!(obj instanceof Student))
            throw new ClassCastException("类型不匹配");
        Student s=(Student)obj;
        if(name.equals(s.getName()) && age==s.getAge())
            return true;

        return false;
    }
    public String toString()
    {
        return name+":"+age;
    }
    public String getName()
    {
        return name;
    }
    public int getAge()
    {
        return age;
    }
}

输出:
true
aa1:25 Value:北京
aa2:20 Value:北京
bb2:18 Value:武汉
bb2:28 Value:武汉

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值