常用对象API_Map集合

Map集合

一次添加一对元素。Collection一次添加一个元素。Map也称双列集合,Collection集合称为单列集合。其实map集合中存储的就是键值对。map集合必须保证键的唯一性。

常用方法:

1.添加

Value put(K key, V value)//返回前一个和key关联的值,如果没用返回null

2.删除

void clear()//清空map集合
value remove(key)//根据指定的key删除这个键值对

3.判断

boolean containsKey(Object key) //如果此映射包含指定键的映射,则返回 true
boolean containsValue(Object value) //如果此地图将一个或多个键映射到指定的值,则返回 true  
boolean isEmpty()//如果此地图不包含键值映射,则返回 true

4.获取

V get(Object key)//返回到指定键所映射的值,或 null如果此映射包含该键的映射,当然可以通过返回null,来判断是否包含指定键
int size()//获取键值对的个数
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

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

    }
      public static void method(Map<Integer,String> map){//学号和姓名
        //添加元素
        System.out.println(map.put(8,"wangcai"));//null
        System.out.println(map.put(8,"xiaoqiang"));//wangcai
        map.put(2,"zhangsan");
        map.put(7,"zhaoliu");
        //删除
        System.out.println("remove"+map.remove(2));
        //判断
        System.out.println("get:"+map.get(2));
        System.out.println(map);
    }
}

keySet方法

返回此地图中包含的键的Set视图。 该集合由地图支持,因此对地图的更改将反映在集合中,反之亦然。 如果在集合中的迭代正在进行中修改映射(除了通过迭代器自己的remove操作),迭代的结果是未定义的。 该组支持元件移除,即从映射中相应的映射,经由Iterator.remove,Set.remove,removeAll,retainAll和clear操作。 它不支持add或addAll操作。

Set<K> keySet() //返回此地图中包含的键的Set视图  
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

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

    }
    public static void method(Map<Integer,String> map){
        map.put(8,"wangwu");
        map.put(2,"zhaoliu");
        map.put(7,"xiaoqiang");
        map.put(6,"wangcai");
        //取出map中的所有元素
        //原理,通过KeySet方法获取map中所有的键所在的Set集合,在通过Set的迭代器取到每一个键
        //在对每一个键获取其对应的值即可
        Set<Integer> keySet=map.keySet();
        Iterator<Integer> it=keySet.iterator();
        while (it.hasNext()){
            Integer key=it.next();
            String value=map.get(key);
            System.out.println(key+":"+value);
        }
    }
}

entrySet方法

返回此地图中包含的映射的Set视图。 该集合由地图支持,因此对地图的更改将反映在集合中,反之亦然。 如果在集合中的迭代正在进行时修改映射(除了通过迭代器自己的remove操作,或者通过迭代器返回的映射条目上的setValue操作),迭代的结果是未定义的。 该组支持元件移除,即从映射中相应的映射,经由Iterator.remove,Set.remove,removeAll,retainAll和clear操作。 它不支持add或addAll操作。

Set<Map.Entry<K,V>> entrySet() //返回此地图中包含的映射的Set视图
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

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

    }
    public static void method_2(Map<Integer,String> map){
        map.put(8,"wangwu");
        map.put(2,"zhaoliu");
        map.put(7,"xiaoqiang");
        map.put(6,"wangcai");
        /*通过Map转成set就可以迭代
        * 找到了另一个方法,entrySet
        * 该方法将键和值的映射关系作为对象存储道了Set集合中,而这个映射关系类型就是Map.Entry类型(结婚证)*/
        Set<Map.Entry<Integer,String>> entrySet=map.entrySet();

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

        while (it.hasNext()){
            Map.Entry<Integer,String> me=it.next();
            Integer key=me.getKey();
            String value=me.getValue();
            System.out.println(key+"::::"+value);
        }
    }

}

Value方法

返回此地图中包含的值的Collection视图。 集合由地图支持,因此对地图的更改将反映在集合中,反之亦然。 如果在集合中的迭代正在进行中修改映射(除了通过迭代器自己的remove操作),迭代的结果是未定义的。 该collection支持元素移除,即从映射中相应的映射,经由Iterator.remove,Collection.remove,removeAll,retainAll和clear操作。 它不支持add或addAll操作。

Collection<V> values() //返回此地图中包含的值的Collection视图
import java.util.*;

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

    }
    public static void method_2(Map<Integer,String> map){
        map.put(8,"wangwu");
        map.put(2,"zhaoliu");
        map.put(7,"xiaoqiang");
        map.put(6,"wangcai");

        Collection<String> values=map.values();
        Iterator<String> it=values.iterator();
        while (it.hasNext()){
            System.out.println(it.next());
        }
    }
}

Map常用的子类:

Hashtable

内部结构是哈希表,是同步的。不允许null作为键,null作为值。

      子类Properties 

       用来存储键值对型的配置文件的信息,可以和IO技术相结合。

HashHap

内部结构是哈希表,不是同步的。允许null作为建,unll作为值。

TreeMap

内部结构是二叉树,不是同步的。可以对Map集合中的键进行排序。

HashMap存储自定义对象

import com.monfolld.p.bean.Person;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

public class HashMapDemo {
    public static void main(String[] args){
        /*将对象和对象的归属地通过键与值存储到map集合中*/
        HashMap<Person,String> hm=new HashMap<Person, String>();
        hm.put(new Person("lisi",38),"beijing");
        hm.put(new Person("zhangsan",24),"shanghai");
        hm.put(new Person("xiaoqiang",31),"shenyang");
        hm.put(new Person("wangcai",28),"dalian");

        Set<Person> keySet=hm.keySet();
        Iterator<Person> it=keySet.iterator();
        while (it.hasNext()){
            Person key=it.next();
            String value=hm.get(key);
            System.out.println(key.getName()+":"+key.getAge()+"---"+value);
        }
    }
}

TreeMap存储自定义对象

 

import com.monfolld.p.bean.Person;

import java.util.*;

public class TreeMapDemo {
    public static void main(String[] args){
        TreeMap<Person,String> hm=new TreeMap<Person, String>();
        //比较器TreeMap<Person,String> hm=new TreeMap<Person, String>(new ComparatorByName());
        hm.put(new com.monfolld.p.bean.Person("lisi",38),"beijing");
        hm.put(new com.monfolld.p.bean.Person("zhangsan",24),"shanghai");
        hm.put(new com.monfolld.p.bean.Person("xiaoqiang",31),"shenyang");
        hm.put(new com.monfolld.p.bean.Person("wangcai",28),"dalian");
        Iterator<Map.Entry<Person,String>> it=hm.entrySet().iterator();
        while (it.hasNext()){
            Map.Entry<Person,String> me=it.next();
            Person key=me.getKey();
            String value=me.getValue();
            System.out.println(key.getName()+":"+key.getAge()+"---"+value);
        }
    }
}

LinkedHashMap


public class LinkedHashMapDemo {
    public static void main(String[] args){
        HashMap<Integer,String> hm=new LinkedHashMap<Integer, String>();  //会按照原来的顺序

        hm.put(7,"zhouqi");
        hm.put(3,"zhangsan");
        hm.put(1,"qianyi");
        hm.put(5,"wangwu");

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

        while (it.hasNext()){
            Map.Entry<Integer,String> me=it.next();

            Integer key=me.getKey();
            String value=me.getValue();
            System.out.println(key+":"+value);
        }
    }
}
/*
7:zhouqi
3:zhangsan
1:qianyi
5:wangwu*/

Map集合练习

要求:“sada+b- sxdAB `fg”获取该字符串中,每一个字母出现的次数

要求打印结果是:a(2)b(1)...;

对于结果的分析发现,字母和次数之间存在着映射关系,而且这种关系很多。

很多就需要存储,能存储映射关系的容器有数组和Map集合。

关系一方式游戏编号吗?没有。

那就使用Map集合,有发现可以保证唯一性的方法具备着顺序a,b,c..

所以就使用TreeMap集合。

package com.monfolld;

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

/*这个集合最终存储的是字母和次数的对应关系
* 1.因为操作的是字符串字母,所以先将字符串变成字符数组
* 2.遍历字符串数组,用内一个字母作为键取查表Map集合这个表
* 如果该字母键不存在,就将该字母作为键1作为值存储到map集合中
* 如果该字符键存在,九江该字母对应的值取出并+1,在将该字母和+1后的值存储到map集合中,
* 键相同值会覆盖,这样就记录了字母出现的次数
* 3.遍历结束,map集合就记录所有字母出现的此时。*/
public class MapTest {
    public static void main(String[] args){
        String str="sada+b- sxdAB `fg";
        String s=getCharCount(str);
        System.out.println(s);
    }
    public static String getCharCount(String str){

        //将字符串变成字符串数组
        char[] chs=str.toCharArray();

        //定义一个map集合表
        Map<Character,Integer> map=new TreeMap<Character,Integer>();

        for (int i=0;i<chs.length;i++){
            if (chs[i]>='a'&&chs[i]<='z') {
                //将数组中的字母作为键取查map表
                Integer value = map.get(chs[i]);
                int count = 1;
                //判断值是否为null
                if (value != null) {
                    count = value + 1;
                }
                map.put(chs[i], count);
          /*  if (value==null){
                map.put(chs[i],1);
            }else {
                map.put(chs[i],value+1);
            }*/
            }
        }
        return maptoString(map);
    }
    private static String maptoString(Map<Character,Integer> map){
        StringBuilder sb=new StringBuilder();

        Iterator<Character> it=map.keySet().iterator();
        while (it.hasNext()){
            Character key=it.next();
            Integer value=map.get(key);
            sb.append(key+"("+value+")");

        }
        return sb.toString();
    }
}

Map集合练习_Map查表法

import java.util.HashMap;
import java.util.Map;

public class MapTest2 {
    /*Map在有映射关系时,可以优先考虑
    在查表法中的应用较为多见*/
    public static void main(String[] args){
        String week=getWeek(1);
        System.out.println(week);
        System.out.println(getWeekByMap(week));
    }
    public static String getWeekByMap(String week){
        Map<String,String> map=new HashMap<String, String>();
        map.put("星期一","Mon");
        map.put("星期二","Tus");
        return map.get(week);
    }
    public static String getWeek(int week){
        if (week<1||week>7)
            throw new RuntimeException("没有对应的星期,请您重新输入");
        String[] weeks={"","星期一","星期二"};
        return weeks[week];

    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值