根据Java中的键对 HashMap 进行排序
我们以 HashMap 的形式给出学生得分的详细信息,其中学生的名字是 Key,得分是 Value。我们的任务是根据键值对地图进行排序,即按字母(字典)顺序排列的学生姓名。
例子:
Input : Key = Jayant, Value = 80
Key = Anushka, Value = 80
Key = Amit, Value = 75
Key = Abhishek, Value = 90
Key = Danish, Value = 40
Output : Sorted Map according to Names:
Key = Abhishek, Value = 90
Key = Amit, Value = 75
Key = Anushka, Value = 80
Key = Danish, Value = 40
Key = Jayant, Value = 80
使用 TreeMap(putAll 方法)
想法是将HashMap的所有数据放入TreeMap中。 TreeMap 遵循基于红黑树的实现。地图根据其键的自然顺序进行排序。点击这里查看更多。
Java
// Java Code to sort Map by key value
import java.util.*;
class sortmapKey {
// This map stores unsorted values
static Map map = new HashMap<>();
// Function to sort map by Key
public static void sortbykey()
{
// TreeMap to store values of HashMap
TreeMap sorted = new TreeMap<>();
// Copy all data from hashMap into TreeMap
sorted.putAll(map);
// Display the TreeMap which is naturally sorted
for (Map.Entry entry : sorted.entrySet())
System.out.println("Key = " + entry.getKey() +
", Value = " + entry.getValue());
}
// Driver Code
public static void main(String args[])
{
// putting values in the Map
map.put("Jayant", 80);
map.put("Abhishek", 90);
map.put("Anushka", 80);
map.put("Amit", 75);
map.put("Danish", 40);
// Calling the function to sortbyKey
sortbykey();
}
}
Java
// Java Code to sort Map by key value
import java.util.*;
class sortmapKey {
// This map stores unsorted values
static Map map = new HashMap<>();
// Function to sort map by Key
public static void sortbykey()
{
// TreeMap to store values of HashMap
TreeMap sorted
= new TreeMap<>(map);
// Display the TreeMap which is naturally sorted
for (Map.Entry entry :
sorted.entrySet())
System.out.println("Key = " + entry.getKey()
+ ", Value = "
+ entry.getValue());
}
// Driver Code
public static void main(String args[])
{
// putting values in the Map
map.put("Jayant", 80);
map.put("Abhishek", 90);
map.put("Anushka", 80);
map.put("Amit", 75);
map.put("Danish", 40);
// Calling the function to sortbyKey
sortbykey();
}
}
Java
// Java Code to sort Map by key value
import java.util.*;
class sortmapKey {
// This map stores unsorted values
static Map map = new HashMap<>();
// Function to sort map by Key
public static void sortbykey()
{
ArrayList sortedKeys
= new ArrayList(map.keySet());
Collections.sort(sortedKeys);
// Display the TreeMap which is naturally sorted
for (String x : sortedKeys)
System.out.println("Key = " + x
+ ", Value = " + map.get(x));
}
// Driver Code
public static void main(String args[])
{
// putting values in the Map
map.put("Jayant", 80);
map.put("Abhishek", 90);
map.put("Anushka", 80);
map.put("Amit", 75);
map.put("Danish", 40);
// Calling the function to sortbyKey
sortbykey();
}
}
Java
// Java Code to sort Map by key value
import java.util.*;
class sortmapKey {
// This map stores unsorted key
static Map map = new HashMap<>();
// function to sort hashmap by keys
public static Map
sortByKey(Map hm)
{
// Create a list from elements of HashMap
List > list
= new LinkedList >(
hm.entrySet());
// Sort the list using lambda expression
Collections.sort(
list,
(i1, i2) -> i1.getKey().compareTo(i2.getKey()));
// put data from sorted list to hashmap
HashMap temp
= new LinkedHashMap();
for (Map.Entry aa : list) {
temp.put(aa.getKey(), aa.getValue());
}
return temp;
}
// Driver Code
public static void main(String args[])
{
// putting values in the Map
map.put("Jayant", 80);
map.put("Abhishek", 90);
map.put("Anushka", 80);
map.put("Amit", 75);
map.put("Danish", 40);
// Calling the function to sortbyKey
Map hm1 = sortByKey(map);
// print the sorted hashmap
for (Map.Entry en :
hm1.entrySet()) {
System.out.println("Key = " + en.getKey()
+ ", Value = "
+ en.getValue());
}
}
}
Java
// Java Code to sort Map by key value
import static java.util.stream.Collectors.*;
import java.lang.*;
import java.util.*;
import java.util.stream.*;
import java.util.stream.Collectors;
class sortmapKey {
// This map stores unsorted values
static Map map = new HashMap<>();
// Function to sort map by Key
public static void sortbykey()
{
HashMap temp
= map.entrySet()
.stream()
.sorted((i1, i2)
-> i1.getKey().compareTo(
i2.getKey()))
.collect(Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(e1, e2) -> e1, LinkedHashMap::new));
// Display the HashMap which is naturally sorted
for (Map.Entry entry :
temp.entrySet()) {
System.out.println("Key = " + entry.getKey()
+ ", Value = "
+ entry.getValue());
}
}
// Driver Code
public static void main(String args[])
{
// putting values in the Map
map.put("Jayant", 80);
map.put("Abhishek", 90);
map.put("Anushka", 80);
map.put("Amit", 75);
map.put("Danish", 40);
// Calling the function to sortbyKey
sortbykey();
}
}
输出
Key = Abhishek, Value = 90
Key = Amit, Value = 75
Key = Anushka, Value = 80
Key = Danish, Value = 40
Key = Jayant, Value = 80
注意: TreeMap 为 containsKey、get、put 和 remove 操作提供有保证的log(n)时间成本。
使用 TreeMap(构造函数)
Java
// Java Code to sort Map by key value
import java.util.*;
class sortmapKey {
// This map stores unsorted values
static Map map = new HashMap<>();
// Function to sort map by Key
public static void sortbykey()
{
// TreeMap to store values of HashMap
TreeMap sorted
= new TreeMap<>(map);
// Display the TreeMap which is naturally sorted
for (Map.Entry entry :
sorted.entrySet())
System.out.println("Key = " + entry.getKey()
+ ", Value = "
+ entry.getValue());
}
// Driver Code
public static void main(String args[])
{
// putting values in the Map
map.put("Jayant", 80);
map.put("Abhishek", 90);
map.put("Anushka", 80);
map.put("Amit", 75);
map.put("Danish", 40);
// Calling the function to sortbyKey
sortbykey();
}
}
输出
Key = Abhishek, Value = 90
Key = Amit, Value = 75
Key = Anushka, Value = 80
Key = Danish, Value = 40
Key = Jayant, Value = 80
使用数组列表
在这种方法中,我们使用 ArrayList 构造函数创建一个键列表。然后我们使用 Collections.sort() 方法对列表进行排序。
Java
// Java Code to sort Map by key value
import java.util.*;
class sortmapKey {
// This map stores unsorted values
static Map map = new HashMap<>();
// Function to sort map by Key
public static void sortbykey()
{
ArrayList sortedKeys
= new ArrayList(map.keySet());
Collections.sort(sortedKeys);
// Display the TreeMap which is naturally sorted
for (String x : sortedKeys)
System.out.println("Key = " + x
+ ", Value = " + map.get(x));
}
// Driver Code
public static void main(String args[])
{
// putting values in the Map
map.put("Jayant", 80);
map.put("Abhishek", 90);
map.put("Anushka", 80);
map.put("Amit", 75);
map.put("Danish", 40);
// Calling the function to sortbyKey
sortbykey();
}
}
输出
Key = Abhishek, Value = 90
Key = Amit, Value = 75
Key = Anushka, Value = 80
Key = Danish, Value = 40
Key = Jayant, Value = 80
使用Java 8 Lambda
在这里,我们将更改排序方式,并使用 lambda 表达式进行排序。逻辑是一样的,甚至我们也传递了比较器对象,但只使用了 lambda。
下面是上述方法的实现:
Java
// Java Code to sort Map by key value
import java.util.*;
class sortmapKey {
// This map stores unsorted key
static Map map = new HashMap<>();
// function to sort hashmap by keys
public static Map
sortByKey(Map hm)
{
// Create a list from elements of HashMap
List > list
= new LinkedList >(
hm.entrySet());
// Sort the list using lambda expression
Collections.sort(
list,
(i1, i2) -> i1.getKey().compareTo(i2.getKey()));
// put data from sorted list to hashmap
HashMap temp
= new LinkedHashMap();
for (Map.Entry aa : list) {
temp.put(aa.getKey(), aa.getValue());
}
return temp;
}
// Driver Code
public static void main(String args[])
{
// putting values in the Map
map.put("Jayant", 80);
map.put("Abhishek", 90);
map.put("Anushka", 80);
map.put("Amit", 75);
map.put("Danish", 40);
// Calling the function to sortbyKey
Map hm1 = sortByKey(map);
// print the sorted hashmap
for (Map.Entry en :
hm1.entrySet()) {
System.out.println("Key = " + en.getKey()
+ ", Value = "
+ en.getValue());
}
}
}
输出
Key = Abhishek, Value = 90
Key = Amit, Value = 75
Key = Anushka, Value = 80
Key = Danish, Value = 40
Key = Jayant, Value = 80
使用Java 8 流
在这里,我们将使用流对地图进行排序。我们将使用stream()方法获取 entrySet 的流,然后使用 sorted() 方法中的 lambda 表达式对流进行排序,最后,我们将使用 toMap() 将其转换为映射 方法。在 toMap() 方法中,我们使用LinkedHashMap::new方法引用来保留地图的排序顺序。
Java
// Java Code to sort Map by key value
import static java.util.stream.Collectors.*;
import java.lang.*;
import java.util.*;
import java.util.stream.*;
import java.util.stream.Collectors;
class sortmapKey {
// This map stores unsorted values
static Map map = new HashMap<>();
// Function to sort map by Key
public static void sortbykey()
{
HashMap temp
= map.entrySet()
.stream()
.sorted((i1, i2)
-> i1.getKey().compareTo(
i2.getKey()))
.collect(Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(e1, e2) -> e1, LinkedHashMap::new));
// Display the HashMap which is naturally sorted
for (Map.Entry entry :
temp.entrySet()) {
System.out.println("Key = " + entry.getKey()
+ ", Value = "
+ entry.getValue());
}
}
// Driver Code
public static void main(String args[])
{
// putting values in the Map
map.put("Jayant", 80);
map.put("Abhishek", 90);
map.put("Anushka", 80);
map.put("Amit", 75);
map.put("Danish", 40);
// Calling the function to sortbyKey
sortbykey();
}
}
输出
Key = Abhishek, Value = 90
Key = Amit, Value = 75
Key = Anushka, Value = 80
Key = Danish, Value = 40
Key = Jayant, Value = 80