温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java中怎么对HashMap按键值排序

发布时间:2021-08-13 13:57:20 来源:亿速云 阅读:165 作者:Leah 栏目:编程语言

本篇文章给大家分享的是有关Java中怎么对HashMap按键值排序,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

1. HashMap存储每对键和值作为一个Entry<K,V>对象。例如,给出一个HashMap,

Map<String,Integer> aMap = new HashMap<String,Integer>();

键的每次插入,都会有值对应到散列映射上,生成一个Entry <K,V>对象。通过使用这个Entry <K,V>对象,我们可以根据值来排序HashMap。

2.创建一个简单的HashMap,并插入一些键和值。

ap<String,Integer> aMap = new HashMap<String,Integer>();          // adding keys and values         aMap.put("Five", 5);         aMap.put("Seven", 7);         aMap.put("Eight", 8);         aMap.put("One",1);         aMap.put("Two",2);         aMap.put("Three", 3);

3.从HashMap恢复entry集合,如下所示。

Set<Entry<String,Integer>> mapEntries = aMap.entrySet();

4.从上述mapEntries创建LinkedList。我们将排序这个链表来解决顺序问题。我们之所以要使用链表来实现这个目的,是因为在链表中插入元素比数组列表更快。

List<Entry<String,Integer>> aList = new LinkedList<Entry<String,Integer>>(mapEntries);

5.通过传递链表和自定义比较器来使用Collections.sort()方法排序链表。

Collections.sort(aList, new Comparator<Entry<String,Integer>>() {              @Override              public int compare(Entry<String, Integer> ele1,                      Entry<String, Integer> ele2) {                  return ele1.getValue().compareTo(ele2.getValue());              }          });

6.使用自定义比较器,基于entry的值(Entry.getValue()),来排序链表。

7. ele1.getValue(). compareTo(ele2.getValue())&mdash;&mdash;比较这两个值,返回0&mdash;&mdash;如果这两个值完全相同的话;返回1&mdash;&mdash;如果***个值大于第二个值;返回-1&mdash;&mdash;如果***个值小于第二个值。

8. Collections.sort()是一个内置方法,仅排序值的列表。它在Collections类中重载。这两种个方法是

public static <T extends Comparable<? super T>> void sort(List<T> list)  public static <T> void sort(List<T> list, Comparator<? super T> c)

9.现在你已经排序链表,我们需要存储键和值信息对到新的映射中。由于HashMap不保持顺序,因此我们要使用LinkedHashMap。

// Storing the list into Linked HashMap to preserve the order of insertion.      Map<String,Integer> aMap2 = new LinkedHashMap<String, Integer>();         for(Entry<String,Integer> entry: aList) {             aMap2.put(entry.getKey(), entry.getValue());         }

10.完整的代码如下。

package com.speakingcs.maps;  import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set;  public class SortMapByValues {      public static void main(String[] args) {          Map<String,Integer> aMap = new HashMap<String,Integer>();          // adding keys and values         aMap.put("Five", 5);         aMap.put("Seven", 7);         aMap.put("Eight", 8);         aMap.put("One",1);         aMap.put("Two",2);         aMap.put("Three", 3);          sortMapByValues(aMap);      }      private static void sortMapByValues(Map<String, Integer> aMap) {          Set<Entry<String,Integer>> mapEntries = aMap.entrySet();          System.out.println("Values and Keys before sorting ");         for(Entry<String,Integer> entry : mapEntries) {             System.out.println(entry.getValue() + " - "+ entry.getKey());         }          // used linked list to sort, because insertion of elements in linked list is faster than an array list.         List<Entry<String,Integer>> aList = new LinkedList<Entry<String,Integer>>(mapEntries);          // sorting the List         Collections.sort(aList, new Comparator<Entry<String,Integer>>() {              @Override             public int compare(Entry<String, Integer> ele1,                     Entry<String, Integer> ele2) {                  return ele1.getValue().compareTo(ele2.getValue());             }         });          // Storing the list into Linked HashMap to preserve the order of insertion.         Map<String,Integer> aMap2 = new LinkedHashMap<String, Integer>();         for(Entry<String,Integer> entry: aList) {             aMap2.put(entry.getKey(), entry.getValue());         }          // printing values after soring of map         System.out.println("Value " + " - " + "Key");         for(Entry<String,Integer> entry : aMap2.entrySet()) {             System.out.println(entry.getValue() + " - " + entry.getKey());         }      } }

以上就是Java中怎么对HashMap按键值排序,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI