Java基础-Map

Map的一些操作

package com.sandy.MapDemo;

import java.util.*;

public class MapDemo {

    public static void main(String[] args) {

        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        map.put(1,14);
        map.put(2,14);
        map.put(9,7);
        map.put(4,10);
        map.put(5,41);
        map.put(5,45);
        map.put(8,40);
        map.put(6,29);
        map.put(null,91);
        map.put(10,null);


        /*
        遍历Map的方法
         */
        /*
        方法一:通过EntrySet获取
         */
        System.out.println("遍历Map集合Key值: ");
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            System.out.println("key:"+entry.getKey()+"  value: "+entry.getValue());
        }

        /*
        方法二:通过Iterator遍历
         */
        System.out.println("遍历Map集合Key值: ");
        Iterator<Map.Entry<Integer, Integer>> iteratorEntry = map.entrySet().iterator();
        while(iteratorEntry.hasNext()){
            Map.Entry<Integer, Integer> entry = iteratorEntry.next();
            System.out.println("key:"+entry.getKey()+"  value: "+entry.getValue());
        }

        /*
        方法三:通过KeySet或Values()遍历
         */


        System.out.println("MaxKey: "+getMaxKey(map));
        System.out.println("MaxValue: "+getMaxValue(map));
        System.out.println("KeyOfMaxValue: "+getKeyOfMaxValue(map));
    }

    public static Object getMaxKey(Map<Integer, Integer> map){
        if(map == null) return null;

        Set set = map.keySet();

        if(set.contains(null))
            set.remove(null);
        Object[] obj = set.toArray();
        Arrays.sort(obj);
        return obj[obj.length - 1];
    }

    public static Object getMaxValue(Map<Integer, Integer> map){
        if(map == null) return null;

        Collection collection = map.values();

        if(collection.contains(null))
            collection.remove(null);
        Object[] obj =  collection.toArray();

        Arrays.sort(obj);
        return obj[collection.size()-1];
    }

    public static Object getKeyOfMaxValue(Map<Integer, Integer> map){
        if(map == null) return null;

        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
                if(entry.getValue() == (int) getMaxValue(map))
                    return entry.getKey();
        }

        return null;
    }

}

P.S. 这里的remove直接改变了Collection,如果需要不改变Collection。需要用到深拷贝。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Map接口的实现类为HashMap,Map接口定义的集合又称为查找表,用于存储“键值对”。Key可以看成Value...
    芽冰阅读 2,615评论 0 0
  • Map 常用的Map Hashtable : 底层是哈希表函数结构,不可以存入null键null值,该集合是线程同...
    小庄bb阅读 1,256评论 0 0
  • Map 我们都知道 Map 是键值对关系的集合,并且键唯一,键一对一对应值。 关于 Map 的定义,大概就这些吧,...
    Anonymous___阅读 3,828评论 0 1
  • 问我手里抱着几本书 还有张练字帖 应该是阴天 我在路上跑了 遇到一个男人跟一个女人开着辆拖拉机 我说了声...
    是他631阅读 1,225评论 0 0
  • 今日收获: 1.以终为始的结果导向 2.改变就在一瞬间 不足: 1.不太懂得取舍,要学会舍得,有舍才有得 2.不擅...
    2017奔跑吧阅读 1,594评论 0 0

友情链接更多精彩内容