zoukankan      html  css  js  c++  java
  • MAP接口课堂练习

    package com.wfu.ch08;
    
    import java.util.Collection;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Set;
    import java.util.TreeMap;
    
    public class Test2 {
        public static void main(String[] args) {
            Map<String,Integer> map=new TreeMap<String,Integer>();
            //添加元素
            map.put("tom",20);
            map.put("rose",18);
            map.put("mike",18);
            map.put("black",21);
            map.put("tom",19);
    //        System.out.println(map);//键是不允许重复的,如果重复,新直会替换旧值,值可以重复
    //        int x=map.get("tom");
    //        System.out.println(x);
            //Map集合的遍历方法//Map集合的遍历(1、entrySet   2、keySet    3、values())
            //“键的集合”,通过map.keySet()方法获取
    //        Set<String> set=map.keySet();//此时set中存放的是字符串(键值)
    ////        System.out.println(set);
    //        for(String s:set){
    //            System.out.print(s+"="+map.get(s)+"  ");
    //        }
    //        System.out.println();
    //        Iterator<String> iterator=set.iterator();
    //        while(iterator.hasNext()){
    //            String s=iterator.next();
    //            System.out.print(s+"="+map.get(s)+" ");
    //        }
            //“值得集合”,通过map.vaules()方法获取
    //        Collection<Integer> value=map.values();
    ////        System.out.println(value);
    //        Iterator<Integer> iterator=value.iterator();
    //        while(iterator.hasNext()){
    //            int v=iterator.next();
    //            System.out.print(v+" ");
    //        }
            //“键值对的集合”,通过map.entrySet()方法获取
            Set<Map.Entry<String,Integer>> set=map.entrySet();
            System.out.println(set);
            for(Map.Entry<String,Integer> me:set){
                String key=me.getKey();
                Integer value=me.getValue();
                System.out.print(key+"="+value+"  ");            
            }
            System.out.println();
            Iterator<Map.Entry<String,Integer>> iterator=set.iterator();
            while(iterator.hasNext()){
                Map.Entry<String,Integer> entry=iterator.next();
                System.out.print(entry.getKey()+"="+entry.getValue()+" ");
            }
            
        }
    
    }
    课堂练习

    课堂总结:

    MAP

    Map是一种把键对象和值对象关联的容器,Map中的键不允许重复,Map不是Collection,但可以将Map转换为Collection,Map提供了用于转换集合的三个方法:

    1、entrySet()返回一个包含了Map中元素的集合,每个元素都包含键和值。

    2、keySet()返回值的集合。

    3、values()返回值的集合。

    区别与联系

    下面从元素是否有序,是否可重复来进行区别,以便记忆和正确选用集合类。

    集合   是否有序 是否可重复
    Collection  
    List  
    S  
    E  
    T  
    M   使用key-value来映射和存储数据,
    A   Key必须唯一,
    P   value可以重复。
  • 相关阅读:
    python函数定义,函数参数
    jmeter之实战总结
    Codeforces Round #384 (Div. 2) B. Chloe and the sequence
    Codeforces Round #384 (Div. 2) C. Vladik and fractions
    CodeForces
    Codeforces Round #383 (Div. 2) B. Arpa’s obvious problem and Mehrdad’s terrible solution
    Codeforces Round #385 (Div. 2) A. Hongcow Learns the Cyclic Shift
    CodeForces
    CodeForces
    Codeforces Round #382 (Div. 2) B. Urbanization
  • 原文地址:https://www.cnblogs.com/llhbk/p/7755283.html
Copyright © 2011-2022 走看看