Map集合排序问题

首先我们应该清楚这个概念:这里的有序和无序不是指集合中的排序,而是是否按照元素添加的顺序来存储对象。

list是按照元素的添加顺序来存储对象的,因此是有序的。他的实现类ArrayList、LinkedList、Vector都是有序的。

Map是无序的,它的存储结构是哈希表<key,value>键值对,map中插入元素是根据key计算出的哈希值来存储元素的,因此他不是按照元素的添加顺序来存储对象的,所以Map是无序的。它的实现类有:HashMap、TableMap和TreeMap。

其中LinkedHashMap是有序的,hashMap用来保证存储的值键值对,list用来保证插入的顺序和存储的顺序一致。

 

Set是无序的,并且set中的元素不能重复。set的底层实现其实是Map,它是计算key的哈希值来确定元素在数组中的存放位置,所以是无序的,应为在Map中key的值不能重复,所以set中的元素不能重复。它的实现类有:haseSet、TreeSet。

其中LinkedHashSet是有序的,其中haseSet用来保证数据唯一,List用来保证插入的顺序和存储的顺序一致。

 Set和List接口是Collection接口的子接口,分别代表无序集合和有序集合,Queue是Java提供的队列实现。

 Map用于保存具有key-value映射关系的数据

Java 中有四种常见的Map实现——HashMap, TreeMap, Hashtable和LinkedHashMap:

  • HashMap就是一张hash表,键和值都没有排序。
  • TreeMap以红黑树结构为基础,键值可以设置按某种顺序排列。
  • LinkedHashMap保存了插入时的顺序。
  • Hashtable是同步的(而HashMap是不同步的)。所以如果在线程安全的环境下应该多使用HashMap,而不是Hashtable,因为Hashtable对同步有额外的开销,不过JDK 5之后的版本可以使用conncurrentHashMap代替HashTable。

本文重点总结HashMap,HashMap是基于哈希表实现的,每一个元素是一个key-value对,其内部通过单链表解决冲突问题,容量不足(超过了阀值)时,同样会自动增长。

HashMap是非线程安全的,只用于单线程环境下,多线程环境下可以采用concurrent并发包下的concurrentHashMap。

HashMap 实现了Serializable接口,因此它支持序列化。

HashMap还实现了Cloneable接口,故能被克隆。

package com.xieh;

import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

public class MyMap {

    public static void main(String[] args) {
        testHashMap();
        System.out.println("以上是HashMap结果
");
        testLinkedHashMap();
        System.out.println("以上是LinkedHashMap结果
");
        testTreeMap();
        System.out.println("以上是TreeMap结果
");
        testSortTreeMap();
        System.out.println("以上是排序的TreeMap结果
");
    }

    public static void testHashMap() {
        // HashMap是无序集合
        Map<String, Integer> map = new HashMap<String, Integer>();
        map.put("我是一", 1);
        map.put("我是二", 2);
        map.put("我是三", 3);
        Set<Entry<String, Integer>> entrySet = map.entrySet();
        for (Entry<String, Integer> entry : entrySet) {
            System.out.println(entry.getKey() + "  " + entry.getValue());
        }
    }

    public static void testLinkedHashMap() {
        // LinkedHashMap是有序集合
        Map<String, Integer> map = new LinkedHashMap<String, Integer>();
        map.put("我是一", 1);
        map.put("我是二", 2);
        map.put("我是三", 3);
        Set<Entry<String, Integer>> entrySet = map.entrySet();
        for (Entry<String, Integer> entry : entrySet) {
            System.out.println(entry.getKey() + "  " + entry.getValue());
        }

    }

    public static void testTreeMap() {
        // TreeMap默认是无序集合,可以实现排序
        Map<String, Integer> map = new TreeMap<String, Integer>();
        map.put("我是一", 1);
        map.put("我是二", 2);
        map.put("我是三", 3);
        Set<Entry<String, Integer>> entrySet = map.entrySet();
        for (Entry<String, Integer> entry : entrySet) {
            System.out.println(entry.getKey() + "  " + entry.getValue());
        }

    }

    public static void testSortTreeMap() {
        // TreeMap实现排序
        Map<String, Integer> map = new TreeMap<String, Integer>(new Comparator<String>() {

            // return -1; 降序
            // return 1; 升序
            @Override
            public int compare(String o1, String o2) {
                return 1;
            }

        });
        map.put("我是6", 1);
        map.put("我是7", 2);
        map.put("我是8", 3);
        Set<Entry<String, Integer>> entrySet = map.entrySet();
        for (Entry<String, Integer> entry : entrySet) {
            System.out.println(entry.getKey() + "  " + entry.getValue());
        }

    }

}

原文地址:https://www.cnblogs.com/xiejn/p/13966520.html