hashmap自定义排序java,如何在Java中对HashMap进行排序

本文介绍了如何在Java中对HashMap进行排序,包括按键和按值排序两种方式。按键排序可以使用TreeMap或LinkedHashMap,而按值排序则需要自定义Comparator并利用LinkedList和LinkedHashMap来保持排序顺序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文概述

Java HashMap默认情况下不保留任何顺序。如果需要对HashMap进行排序, 我们将根据需求对其进行显式排序。 Java提供了一个根据键和值对HashMap进行排序的选项。在本节中, 我们将学习如何根据键和值对HashMap进行排序。

按键对HashMap排序

按值对HashMap排序

按键对HashMap排序

有以下几种按键对HashMap进行排序的方法:

通过使用TreeMap

通过使用LinkedHashMap

使用LinkedHashMap时, 应遵循以下过程:

当我们使用LinkedHashMap时, 我们需要获取Key set。将Set转换为List, 对列表进行排序, 然后以相同顺序将排序后的列表添加到LinkedHashMap中。我们在示例“按值对HashMap进行排序”中完成的相同过程。

按键对HashMap排序的示例

在下面的示例中, 我们使用TreeMap构造函数对元素进行排序, 并将HashMap类的对象作为参数传递。这是按键对HashMap排序的最简单方法。

import java.util.Map;

import java.util.HashMap;

import java.util.TreeMap;

import java.util.Iterator;

public class SortHashMapByKeys

{

public static void main(String args[])

{

//implementation of HashMap

HashMap hm=new HashMap();

//addding keys and values to HashMap

hm.put(23, "Yash");

hm.put(17, "Arun");

hm.put(15, "Swarit");

hm.put(9, "Neelesh");

Iterator it = hm.keySet().iterator();

System.out.println("Before Sorting");

while(it.hasNext())

{

int key=(int)it.next();

System.out.println("Roll no: "+key+" name: "+hm.get(key));

}

System.out.println("\n");

Map map=new HashMap();

System.out.println("After Sorting");

//using TreeMap constructor to sort the HashMap

TreeMap tm=new TreeMap (hm);

Iterator itr=tm.keySet().iterator();

while(itr.hasNext())

{

int key=(int)itr.next();

System.out.println("Roll no: "+key+" name: "+hm.get(key));

}

}

}

输出:

Before Sorting

Roll no: 17 name: Arun

Roll no: 23 name: Yash

Roll no: 9 name: Neelesh

Roll no: 15 name: Swarit

After Sorting

Roll no: 9 name: Neelesh

Roll no: 15 name: Swarit

Roll no: 17 name: Arun

Roll no: 23 name: Yash

使用比较器接口按值对HashMap进行排序

在Java中, 按值对HashMap进行排序很复杂, 因为没有可用的直接方法。要按值对HashMap进行排序, 我们需要创建一个Comparator。它根据值比较两个元素。

之后, 从Map中获取元素集并将Set转换为List。使用Collections.sort(List)方法通过传递自定义比较器, 按值对元素列表进行排序。现在创建一个新的LinkedHashMap并将已排序的元素复制到其中。由于LinkedHashMap保证了映射的插入顺序。我们得到一个HashMap, 其值按排序顺序。

按“键”和“值”对HashMap进行排序之间有一个细微的区别, 即它可以具有重复的值, 但不能具有重复的键。我们无法使用TreeMap对值进行排序, 因为TreeMap通过键对元素进行排序。

按值对HashMap排序的示例

import java.util.Collections;

import java.util.Comparator;

import java.util.HashMap;

import java.util.Iterator;

import java.util.LinkedHashMap;

import java.util.LinkedList;

import java.util.List;

import java.util.Map;

import java.util.Set;

public class SortHashMapValue

{

public static void main(String[] args)

{

//implementing HashMap

HashMap hm = new HashMap();

hm.put(6, "Tushar");

hm.put(12, "Ashu");

hm.put(5, "Zoya");

hm.put(78, "Yash");

hm.put(10, "Praveen");

hm.put(67, "Boby");

hm.put(1, "Ritesh");

System.out.println("Before Sorting:");

Set set = hm.entrySet();

Iterator iterator = set.iterator();

while(iterator.hasNext())

{

Map.Entry map = (Map.Entry)iterator.next();

System.out.println("Roll no: "+map.getKey()+" Name: "+map.getValue());

}

Map map = sortValues(hm);

System.out.println("\n");

System.out.println("After Sorting:");

Set set2 = map.entrySet();

Iterator iterator2 = set2.iterator();

while(iterator2.hasNext())

{

Map.Entry me2 = (Map.Entry)iterator2.next();

System.out.println("Roll no: "+me2.getKey()+" Name: "+me2.getValue());

}

}

//method to sort values

private static HashMap sortValues(HashMap map)

{

List list = new LinkedList(map.entrySet());

//Custom Comparator

Collections.sort(list, new Comparator()

{

public int compare(Object o1, Object o2)

{

return ((Comparable) ((Map.Entry) (o1)).getValue()).compareTo(((Map.Entry) (o2)).getValue());

}

});

//copying the sorted list in HashMap to preserve the iteration order

HashMap sortedHashMap = new LinkedHashMap();

for (Iterator it = list.iterator(); it.hasNext();)

{

Map.Entry entry = (Map.Entry) it.next();

sortedHashMap.put(entry.getKey(), entry.getValue());

}

return sortedHashMap;

}

}

输出:

Before Sorting:

Roll no: 1 Name: Ritesh

Roll no: 67 Name: Boby

Roll no: 5 Name: Zoya

Roll no: 6 Name: Tushar

Roll no: 10 Name: Praveen

Roll no: 12 Name: Ashu

Roll no: 78 Name: Yash

After Sorting:

Roll no: 12 Name: Ashu

Roll no: 67 Name: Boby

Roll no: 10 Name: Praveen

Roll no: 1 Name: Ritesh

Roll no: 6 Name: Tushar

Roll no: 78 Name: Yash

Roll no: 5 Name: Zoya

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值