• 集合框架之Map


    *Map集合:该集合存储键值对,是一对一对往里存,而且要保证键的唯一性。
    1.添加。
    put(key,value),putAll(E),
    2.删除。
    clear(),remove(key)
    3.判断。
    containsKey(key),
    containsValue(value)
    4.获取。
    get(key),
    size(),
    values(),
    entrySet(),
    keySet()
    Map
    |-Hashtable:底层是哈希表数据结构,不可以存入null键null值。该集合是线
    程同步的。
    |-HashMap:底层是哈希表数据结构,允许使用null键null值。该集合是不同
    步的
    |-TreeMap:底层是二叉树数据结构。可以用于给Map集合中的键进行排序

    和Set很像。其实Set集合底层就是使用了Map集合

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

    public class MapDemo {
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    //添加元素,如果添加时,相同的键,那么后添加的的值会覆盖原
    有键对应值
    Map<String,String> map=new HashMap<String,String>();
    System.out.println(map.put("01","zhangsan1"));
    map.put("02","zhangsan2");
    map.put("03","zhangsan3");
    System.out.println("containsKey"+map.containsKey("02"));
    //System.out.println("remove"+map.remove("02"));
    System.out.println("get:"+map.get("02"));

    map.put(null,"haha");
    System.out.println("get:"+map.get(null));

    Collection<String> coll=map.values();
    System.out.println(coll);
    System.out.println(map);
    }
    }

    map集合的两种取出方式:
    1.keySet:将map中所有的键存入到Set集合。因为set集合具备迭代器。所以可以通过迭
    代方法取出所有的键,在根据get方法。获取每一个键对应的值。

    map集合取出原理:将map集合转换成set集合。在通过迭代器取出。

    2.Set<Map.Entry<k,v>> entrySet:将map集合中的映射关系存入到了set集合中,而这
    个关系的数据类型就是:Map.Entry
    Map.Entry其实Entry也是一个接口它是Map接口中的一个内部接口。
    interface Map
    {
    public static interface Entry{
    public abstract Object getKey();
    public abstract Object getValue();
    }
    }
    class HashMap implements Map.Entry{
    实现方法
    }

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

    public class MapDemo2 {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Map<String,String> map=new HashMap<String,String>();
    map.put("01","zhangsan1");
    map.put("02","zhangsan2");
    map.put("03","zhangsan3");
    map.put("04","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集合中的映射关系取出。存入到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);
    }
    }
    }

    **************

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

    /**
    * 每一个Student,地址String。
    * 学生属性:姓名,年龄。
    * 注意:姓名和年龄相同的视为同一个学生。
    * 保证学生的唯一性。
    *
    * 1.描述学生。
    * 2.定义map容器,将学生作为键,地址作为值。存入。
    * 3.获取map集合中的元素。
    */

    public class MapTest {
    public static void main(String[] args) {
    HashMap<Student,String> hm=new
    HashMap<Student,String>();
    hm.put(new Student("zhangsan01",18),"迎春大街001");
    hm.put(new Student("zhangsan02",21),"迎春大街002");
    hm.put(new Student("zhangsan03",23),"迎春大街003");
    hm.put(new Student("zhangsan04",24),"迎春大街004");

    //第一种取出方式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>> it1=entrySet.iterator();
    while(it1.hasNext()){
    Map.Entry<Student,String> me=it1.next();
    Student stu=me.getKey();
    String addr=me.getValue();
    System.out.println(stu+"----"+addr);
    }
    }
    }
    class Student implements Comparable<Student>{
    private String name;
    private int age;
    public Student(){}
    public Student(String name,int age){
    this.name=name;
    this.age=age;
    }
    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;
    }
    public String toString(){
    return name+":"+age;
    }
    public int hasCode(){
    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;
    }
    @Override
    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;
    }


    }

    *********

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

    /**
    * 练习”sdfgzxcvasdfxcvdf“获取该字符串中的字母出现的次数。
    * 希望打印结果:a(1)c(2).......
    * 通过结果发现,每一个字母都有对应的次数
    * 说明子母和次数之间都有映射关系。
    * 注意了:发现有映射关系,可以选择map集合。
    * 因为map集合中存放就是映射关系。
    * 思路:
    * 1.将字符串转换成字符数组,因为要对每一个字母进行操作。
    * 2.定义一个map集合,因为打印结果的字母有顺序,所以使用treemap集合
    * 3.遍历字符数组
    * 将每一个字母作为键去查map集合。
    * 如果返回null,将该字母和1存入到map集合中。
    * 如果返回不是mull,说明该字母在map集合已经存在并有对应次数。
    * 那么就获取该次数并自增,然后将该字母和自增后的次数存入到map集合中,覆盖调
    用原键所对应的值。
    * 4.将map集合中的数据变成指定的字符串形式返回。
    */
    public class MapTest3 {
    public static void main(String[] args) {
    String s=charCount("aabfcdabcdefa");
    System.out.println(s);
    }
    public static String charCount(String str){
    char[] chs=str.toCharArray();
    TreeMap<Character,Integer> tm=new
    TreeMap<Character,Integer>();
    int count=0;
    for(int x=0;x<chs.length;x++){

    Integer value=tm.get(chs[x]);
    if(value!=null)
    count=value;
    count++;
    tm.put(chs[x],count);
    count=0;
    /*if(value==null){
    tm.put(chs[x],1);
    }else{
    value=value+1;
    tm.put(chs[x],value);
    }*/
    }
    //System.out.println(tm);
    StringBuilder sb=new StringBuilder();
    Set<Map.Entry<Character,Integer>> entrySet=tm.entrySet();
    Iterator<Map.Entry<Character,Integer>>
    it=entrySet.iterator();
    while(it.hasNext()){
    Map.Entry<Character,Integer> me=it.next();
    Character ch=me.getKey();
    Integer value=me.getValue();
    sb.append(ch+"("+value+")");
    }
    return sb.toString();
    }
    }

  • 相关阅读:
    Good Bye 2014 B. New Year Permutation(floyd )
    hdu 5147 Sequence II (树状数组 求逆序数)
    POJ 1696 Space Ant (极角排序)
    POJ 2398 Toy Storage (叉积判断点和线段的关系)
    hdu 2897 邂逅明下 (简单巴什博弈)
    poj 1410 Intersection (判断线段与矩形相交 判线段相交)
    HDU 3400 Line belt (三分嵌套)
    Codeforces Round #279 (Div. 2) C. Hacking Cypher (大数取余)
    Codeforces Round #179 (Div. 2) B. Yaroslav and Two Strings (容斥原理)
    hdu 1576 A/B (求逆元)
  • 原文地址:https://www.cnblogs.com/jzxf-blogs/p/4869988.html
Copyright © 2020-2023  润新知