Java HashMap(LinkedHashMap)与Hashset(LinkedHashSet)的排序

来源:互联网 发布:阿里云 代金券怎么用 编辑:程序博客网 时间:2024/05/02 00:12

Java HashMap(LinkedHashMap)与Hashset(LinkedHashSet)的排序

为什么要单独说HashMap和HashSet的排序问题?

1.首先先总结一下一些基本的数据结构的排序问题,一些之前已经讲过,这里进行一下总结:

1)对Array数组排序,不支持插入的时候排序(实际上只有treeMap和TreeSet支持),使用Arrays工具类的sort方法进行排序,一个参数代表Comparable接口排序,两个参数,则第一个参数是数组,第二个参数是一个Comparator的实现类;

2)对ArrayList和LinkedList,不支持插入的时候排序(实际上只有treeMap和TreeSet支持),使用Collection工具类的sort方法进行排序,一个参数代表Comparable接口排序,两个参数,则第一个参数是List,第二个参数是一个Comparator的实现类;

3)对TreeMap和TreeSet是支持插入的时候排序的,既可以使用Comparable接口,也可以使用Comparator接口(只是在生成Tree的时候加上一个参数,这个参数就是Comparator的实现类)

上面已经说了五种数据结构的排序方式是(都已经过实践),那么还有HashMap,LinkedHashMap,HashSet,LinkedHashSet没有说明,这篇主要2.先说明Hashmap和HashSet的排序问题。

首先他们是不能直接进行排序的,需要使用Collections的sort方法,但是有一个要求是sort方法的参数为<List list>  或<List list, Comparator<? super T>  c>,即排序对象要求必须是List类型。

     sort 方法的参数必须为List 的原因是,只有List可以定义排序的方法,让List中的元素改变在构建List时原始的相对位置(初始构建时,元素相对位置即为元素初始加入顺序)。HashSet、HashMap 在构建时,初始加入的元素已经按照元素的hashCode()方法的定义排好序。所以这里所说的HashSet 排序 和 HashMap 排序是指:将其中的元素导出到另一个集合中,对该载体集合排序。排序之后,原HashSet 和 HashMap 中元素顺序没有变。

    故而对Java无序类集合的排序问题,基本思路就是:将HashSet 或 HashMap中的元素取出放入 List 中,对List 用 Collections.sort() 方法排序,使用Comparator接口,之后输出排序后List中的元素,即为对Set/Map 中元素排序后的结果。注意HashSet、HashMap 中元素位置没有改变,依然只和 初始构建时,元素本身自定义的hashCode() 方法有关。

示例如下:

package roadArchitectWeb.Test;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.HashMap;import java.util.HashSet;import java.util.LinkedHashMap;import java.util.List;import java.util.Map;public class Mytest {public static void main(String[] args) {    /*HashMap*/    HashMap map_Data=new HashMap();      map_Data.put("A", "98");      map_Data.put("C", "50");      map_Data.put("B", "50");      map_Data.put("D", "25");      map_Data.put("E", "85");      System.out.println(map_Data);    /*根据value排序*/    List<Map.Entry<String, String>> list_Data = new ArrayList<Map.Entry<String, String>>(map_Data.entrySet());      Collections.sort(list_Data, new Comparator<Map.Entry<String, String>>()        {             public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2)            {             if(o2.getValue()!=null&&o1.getValue()!=null&&o2.getValue().compareTo(o1.getValue())>0){              return 1;             }else{              return -1;             }                          }        });      System.out.println(list_Data);    /*根据key排序*/    List<Map.Entry<String, String>> list_Data2 = new ArrayList<Map.Entry<String, String>>(map_Data.entrySet());      Collections.sort(list_Data2, new Comparator<Map.Entry<String, String>>()        {             public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2)             {             if(o2.getKey()!=null&&o1.getKey()!=null&&o2.getKey().compareTo(o1.getKey())>0){              return 1;             }else{              return -1;             }                          }        });      System.out.println(list_Data2);       /*HashSet*/    HashSet set_Data=new HashSet();      set_Data.add("A");    set_Data.add("B");    set_Data.add("C");    set_Data.add("D");    set_Data.add("E");    System.out.println(set_Data);    /*Hashset排序*/    List<String> setList = new ArrayList<String>(set_Data);      Collections.sort(setList, new Comparator<String>()        {             public int compare(String o1, String o2)             {             if(o2 !=null&&o1 !=null&&o2.compareTo(o1)>0){              return 1;             }else{              return -1;             }                          }        });      System.out.println(setList);        LinkedHashMap Lmap_Data=new LinkedHashMap<String,String>();      Lmap_Data.put("A", "98");      Lmap_Data.put("C", "50");      Lmap_Data.put("B", "50");      Lmap_Data.put("D", "25");      Lmap_Data.put("E", "85");      System.out.println(Lmap_Data);    /*根据value排序*/    List<Map.Entry<String, String>> Llist_Data = new ArrayList<Map.Entry<String, String>>(map_Data.entrySet());      Collections.sort(Llist_Data, new Comparator<Map.Entry<String, String>>()        {             public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2)            {             if(o2.getValue()!=null&&o1.getValue()!=null&&o2.getValue().compareTo(o1.getValue())>0){              return 1;             }else{              return -1;             }                          }        });      System.out.println(Llist_Data);}}
结果如下:

<span style="font-family:SimSun;font-size:18px;">{A=98, B=50, C=50, D=25, E=85}[A=98, E=85, C=50, B=50, D=25][E=85, D=25, C=50, B=50, A=98][A, B, C, D, E[E, D, C, B, A]{A=98, C=50, B=50, D=25, E=85}[A=98, E=85, C=50, B=50, D=25]</span>

 3.LinkedHashMap和LinkedHashSet本身会自动进行排序,它会记录插入的顺序;如果把他们按照HashMap和HashSet的排序方式进行排序也是可以的,上面的示例已经说明了。

0 0
原创粉丝点击