集合 Map接口

※ Map

Map是一种把键对象和值对象进行映射的集合,它的每一个元素都包含一对键(Key)对象和值(Value)对象。向Map集合中加入元素时,必须提供一对键对象和值对象,从Map集合中检索元素时,只要给出键对象,就会返回对应的值对象。

    map.put("2", "Tuesday");
               map.put("3", "Wednsday");
               map.put("4", "Thursday");

               String day = map.get("2");    //day的值为"Tuesday"

Map集合中的键对象不允许重复,如以相同的键对象加入多个值对象,第一次加入的值对象将被覆盖。对于值对象则没有唯一性的要求,可以将任意多个键对象映射到同一个值对象上。

     map.put("1", "Mon");
               map.put("1", "Monday");      //"1"此时对应"Monday"
               map.put("one", "Monday");    //"one"此时对应"Monday"

这里写图片描述
※ Map常见的几个继承类:
. HashMap 是线程不安全的集合。
. HashTable是线程安全的集合。
. TreeMap可以进行排序(对key进行排序)

※ HashMap类

※ HashMap构造器:
这里写图片描述
※ 方法:
这里写图片描述
这里写图片描述
※ 方法的验证:

package com.briup.collection.map;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class HashMapTest {
    public static void main(String[] args) {
        //范型参数限定参数的类型(引用类型)
        //key键要求唯一,键不能重复,值没有要求
        //key相同,新插入的value值替换原来的value
        Map<Integer, String> map=new HashMap<>();
        map.put(1, "briup");
        map.put(2, "test");
        map.put(6, "test");
        map.put(3, "wangwu");
        map.put(4, "jake");
        Map<Integer, String> map1=new HashMap<>();
        map1.put(1, "lili");
        map1.put(5, "ok");
        //map把map1集合中的元素添加到map中,key相同的
        //value覆盖原有内容
        map.putAll(map1);
        //获取所有values
//      Collection<String> coll=map.values();
//      for(String v:coll){
//          System.out.println("value:"+v);
//      }
        //map集合是否包含某个value值,包含true
//      System.out.println(map.containsValue("test1"));
        //map集合是否包含key 包含true
//      System.out.println(map.containsKey(10));
        //清空map集合
//      map.clear();
//      System.out.println(map.size());
        //map中取值基于键
//      String v=map.get(2);
//      System.out.println(v);
        //获取所有的key
        /*Set<Integer> keys=map.keySet();
        for(Integer key:keys){
            System.out.println("key:"+key+"   value:"+map.get(key));
        }*/
        Set<Entry<Integer, String>> en=map.entrySet();
        //Entry对像封装一组键值对
        //getKey()表示从Entry中取键 getValue()表示从Entry中取值
        for(Entry<Integer, String> n:en){
            System.out.println("key:"+n.getKey()+"   value:"+n.getValue());
        }

//      Iterator<Entry<Integer, String>> iter=en.iterator();
//      while(iter.hasNext()){
//          Entry<Integer, String> n=iter.next();
//          System.out.println("key:"+n.getKey()+"   value:"+n.getValue());
//      }
    }
}





package com.briup.collection.map;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.Set;

public class HashMapTest1 {
    public static void main(String[] args) {
        //Map<Cart, String> map=new HashMap<>();
        //HashMap线程不安全,HashTable线程安全
        //两个用法基本一致
        Map<Cart, String> map=new Hashtable<>();
        Cart c=new Cart(1, "java", 33);
        Cart c1=new Cart(2, "test", 23);
        Cart c2=new Cart(3, "briup", 3);
        Cart c5=new Cart(3, "briup", 53);
        Cart c3=new Cart(4, "zhans", 43);
        Cart c4=c3;
        //key为唯一,如果是自定义对象,先比价hashcode
        //才会比较equals方法
        map.put(c, "hello1");
        map.put(c1, "hello2");
        map.put(c2, "hello3");
        map.put(c3, "hello4");
        map.put(c4, "hello5");
        map.put(c5, "hello6");
        Set<Cart> set=map.keySet();
        for(Cart key:set){
            System.out.println("key:"+key+"   value:"+map.get(key));
        }
    }
}   

按哈希算法来存取键对象,有很好的存取性能,为了保证HashMap能正常工作,和HashSet一样,要求当两个键对象通过equals()方法比较为true时,这两个键对象的hashCode()方法返回的哈希码也一样。

※ TreeMap

※ TreeMap构造器:
这里写图片描述
※ 方法:
这里写图片描述
这里写图片描述
这里写图片描述
方法的实现(包含自然排序的重写):

package com.briup.collection.map;

public class Cart implements Comparable<Cart>{
    private long id;
    private String name;
    private int age;
    @Override
    public int hashCode() {
        // TODO Auto-generated method stub
        return (int) id;
    }
    @Override
    public boolean equals(Object obj) {
        Cart c=(Cart) obj;
        if(c.id==this.id&&this.name.equals(c.name)){
            return true;
        }
        return false;
    }
    @Override
    public String toString() {
        return "Cart [id=" + id + ", name=" + name + ", age=" + age + "]";
    }
    public Cart(long id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }
    public Cart() {
    }
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    //新添加的元素和集合中存在的元素比较
    //参数o就是集合中取出的元素
    @Override
    public int compareTo(Cart o) {
        // TODO Auto-generated method stub
        return this.age-o.age;
    }
}




package com.briup.collection.map;

import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;

public class TreeMapTest {

    public static void main(String[] args) {
        //treemap是基于key排序的集合,key的排序和treeSet排序相似
        //排序 客户排序和自然排序
        Map<Integer, String> tree=new TreeMap<>(new Comparator<Integer>() {

            @Override
            public int compare(Integer o1, Integer o2) {
                // TODO Auto-generated method stub
                return o2-o1;
            }
        });
        tree.put(22, "test");
        tree.put(12, "test");//22 12 2
        tree.put(2, "test");
        tree.put(8, "test");
        for(Integer key:tree.keySet()){
            System.out.println("key:"+key+"   value:"+tree.get(key));
        }
    }
}





package com.briup.collection.map;

import java.util.Map;
import java.util.TreeMap;

public class TreeMapTest2 {
    public static void main(String[] args) {
        //自定以对象作为key必须实现比较规则(
        //客户排序和自然排序)
        Map<Cart, String> tree=new TreeMap<>();
        tree.put(new Cart(1, "java", 33), "test");
        tree.put(new Cart(2, "java1", 13), "test");
        tree.put(new Cart(3, "java2", 3), "test");
        tree.put(new Cart(4, "java3", 23), "test");
        for(Cart key:tree.keySet()){
            System.out.println("key:"+key+"   value:"+tree.get(key));
        }
    }
}

实现了SortedMap接口,能对键对象进行排序。和TreeSet一样,TreeMap也支持自然排序和客户化排序两种方式。(排序按照的是KEY值)

    Map map = new TreeMap();
                  map.put("1", "Monday");
                  map.put("3", "Wednsday");
                  map.put("4", "Thursday");
                  map.put("2", "Tuesday");

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

                  打印输出:

                  1 Monday
                  2 Tuesday
                  3 Wednsday
                  4 Thursday
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值