HashMap与TreeMap的排序以及四种遍历方式

一、Map概述

1、Map是将键映射到值( key-value )的对象。一个映射不能包含重复的键;每个键最多只能映射到一个值。

2、Map与Collection的区别
(1)Map 是以键值对的方式存储元素,键唯一,值可以重复。
(2)Collection存储的是单列元素,子接口Set元素唯一,子接口List可以重复。
(3)Map的数据结构针对键有效,跟值无关,Collection针对元素有效。

3、HashMap 与TreeMap 的创建,排序。

/***
 * Map类的一些简单操作
 * 包括TreeMap的排序,HashMap的排序,四种遍历方式
 * @author Administrator
 *
 */
public class MapTest {
 
    public static void main(String[] args) {
 
        //创建map对象,并赋值
        
        Map<String,String> treeMap = new TreeMap<String,String>(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });
        
        treeMap.put("sufow1", "舒泉1");
        treeMap.put("sufow2", "舒泉2");
        treeMap.put("sufow3", "舒泉3");
        treeMap.put("sufow4", "舒泉4");
        treeMap.put("sufow5", "舒泉5");
        treeMap.put("sufow6", "舒泉6");
        treeMap.put("sufow7", "舒泉7");
        
        System.out.println("TreeMap");
        
        //遍历map 方式一,根据keyset方法获取所有的key,再根据key获取值
        for(String str :treeMap.keySet()){
            String val = treeMap.get(str);
            System.out.println(val);
        }
        
        Map<String,String> hashMap = new HashMap<String ,String>();
        
        
        hashMap.put("sufow1", "舒泉1");//可以写入null值
        hashMap.put("sufow2", "舒泉2");
        hashMap.put("sufow3", "舒泉3");
        hashMap.put("sufow4", "舒泉4");
        hashMap.put("sufow5", "舒泉5");
        hashMap.put("sufow6", "舒泉6");
        hashMap.put("sufow7", "舒泉7");
        
        System.out.println("HashMap");
        
        //先排序
        
        List<Map.Entry<String, String>> list = new ArrayList<Map.Entry<String, String>>(hashMap.entrySet());
        
        //通过比较器来排序
        Collections.sort(list, new Comparator<Map.Entry<String, String>>() {
            @Override
            public int compare(Entry<String, String> o1,
                    Entry<String, String> o2) {
                return o1.getValue().compareTo(o2.getValue());
            }
        });
        
        //遍历List,第二种通过Map.Entry
        for(Map.Entry<String, String> m :list){
            String val =  m.getValue();
            System.out.println(val);
        }
        
        //第二种遍历map的方式,根据迭代器Iterator
        Iterator<Map.Entry<String, String>> iterator = hashMap.entrySet().iterator();
        
        while(iterator.hasNext()){
            Map.Entry<String, String> map = iterator.next();
            System.out.println(map.getKey());
            System.out.println(map.getValue());
        }
        
        //第三种通过values,只能遍历value
        for(String str:hashMap.values()){
            System.out.println(str);
        }
        
        
        //第四种,通过Map.Entry
        for(Map.Entry<String, String> map:hashMap.entrySet()){
            System.out.println(map.getKey());
            System.out.println(map.getValue());
        }
    }

4、总结
(1)map排序的两种方式
第一种:通过 new Comparator的接口,实现方法,进行排序。
第二种:先将Map转换成List , 通过Collections的sort方法进行排序。

(2)Map的四种遍历方式
第一种:通过Map的keySet()方法遍历。
第二种:通过Map的values()方法遍历。
第三种:通过Map的entrySet()方法的Iterator方法将转换成Iterator。通过迭代器遍历。
第四种:通过Map的entrySet()方法,将其转换成Map.Entry对象,在通过foreach遍历,通过Map.Entry对象的getValue()获取值。

转自:https://blog.csdn.net/woshimaxiao1/article/details/83754231

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 四、集合框架 1:String类:字符串(重点) (1)多个字符组成的一个序列,叫字符串。生活中很多数据的描述都采...
    佘大将军阅读 804评论 0 2
  • Java集合框架 Java平台提供了一个全新的集合框架。“集合框架”主要由一组用来操作对象的接口组成。不同接口描述...
    小石38阅读 389评论 0 0
  • 概述 集合框架被设计成要满足以下几个目标: 该框架必须是高性能的。基本集合(动态数组,链表,树,哈希表)的实现也必...
    Tian_Peng阅读 389评论 0 1
  • 一、为什么会出现集合类 1.集合是一个容器,为了方便的对多个对象进行操作。 2.集合容器同数组容器的...
    大禹编程扛把子阅读 626评论 0 0
  • 一 Set List Map的定义 1. 什么是set Set的功能方法 Set具有与Collection完全一样...
    史慧君阅读 4,111评论 1 49