根据值对 Hashmap 进行排序
给定学生在以学科名称为关键且得分为价值的学科中得分 100 分的分数。我们的任务是根据值(即根据标记)对哈希图进行排序。
例子:
Input : Key = Math, Value = 98
Key = Data Structure, Value = 85
Key = Database, Value = 91
Key = Java, Value = 95
Key = Operating System, Value = 79
Key = Networking, Value = 80
Output : Key = Operating System, Value = 79
Key = Networking, Value = 80
Key = Data Structure, Value = 85
Key = Database, Value = 91
Key = Java, Value = 95
Key = Math, Value = 98
解决方案:想法是将条目集存储在列表中,并根据值对列表进行排序。然后从列表中获取值和键并将它们放入新的哈希图中。因此,根据值对新的 hashmap 进行排序。
下面是上述思想的实现:
Java
// Java program to sort hashmap by values
import java.util.*;
import java.lang.*;
public class GFG {
// function to sort hashmap by values
public static HashMap sortByValue(HashMap hm)
{
// Create a list from elements of HashMap
List > list =
new LinkedList >(hm.entrySet());
// Sort the list
Collections.sort(list, new Comparator >() {
public int compare(Map.Entry o1,
Map.Entry o2)
{
return (o1.getValue()).compareTo(o2.getValue());
}
});
// 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)
{
HashMap hm = new HashMap();
// enter data into hashmap
hm.put("Math", 98);
hm.put("Data Structure", 85);
hm.put("Database", 91);
hm.put("Java", 95);
hm.put("Operating System", 79);
hm.put("Networking", 80);
Map hm1 = sortByValue(hm);
// print the sorted hashmap
for (Map.Entry en : hm1.entrySet()) {
System.out.println("Key = " + en.getKey() +
", Value = " + en.getValue());
}
}
}
Java
// Java program to sort hashmap by values
import java.lang.*;
import java.util.*;
public class GFG {
// function to sort hashmap by values
public static HashMap
sortByValue(HashMap 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.getValue().compareTo(i2.getValue()));
// 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)
{
HashMap hm
= new HashMap();
// enter data into hashmap
hm.put("Math", 98);
hm.put("Data Structure", 85);
hm.put("Database", 91);
hm.put("Java", 95);
hm.put("Operating System", 79);
hm.put("Networking", 80);
Map hm1 = sortByValue(hm);
// print the sorted hashmap
for (Map.Entry en :
hm1.entrySet()) {
System.out.println("Key = " + en.getKey()
+ ", Value = "
+ en.getValue());
}
}
}
Java
// Java program to sort hashmap by values
import static java.util.stream.Collectors.*;
import java.lang.*;
import java.util.*;
import java.util.stream.*;
import java.util.stream.Collectors;
public class gfg3 {
// function to sort hashmap by values
public static HashMap
sortByValue(HashMap hm)
{
HashMap temp
= hm.entrySet()
.stream()
.sorted((i1, i2)
-> i1.getValue().compareTo(
i2.getValue()))
.collect(Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(e1, e2) -> e1, LinkedHashMap::new));
return temp;
}
// Driver Code
public static void main(String[] args)
{
HashMap hm
= new HashMap();
// enter data into hashmap
hm.put("Math", 98);
hm.put("Data Structure", 85);
hm.put("Database", 91);
hm.put("Java", 95);
hm.put("Operating System", 79);
hm.put("Networking", 80);
Map hm1 = sortByValue(hm);
// print the sorted hashmap
for (Map.Entry en :
hm1.entrySet()) {
System.out.println("Key = " + en.getKey()
+ ", Value = "
+ en.getValue());
}
}
}
输出
Key = Operating System, Value = 79
Key = Networking, Value = 80
Key = Data Structure, Value = 85
Key = Database, Value = 91
Key = Java, Value = 95
Key = Math, Value = 98
使用Java 8 Lambda
在这里,我们将更改排序方式,并使用 lambda 表达式进行排序。逻辑是一样的,甚至我们也传递了比较器对象,但只使用了 lambda。
下面是上述方法的实现:
Java
// Java program to sort hashmap by values
import java.lang.*;
import java.util.*;
public class GFG {
// function to sort hashmap by values
public static HashMap
sortByValue(HashMap 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.getValue().compareTo(i2.getValue()));
// 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)
{
HashMap hm
= new HashMap();
// enter data into hashmap
hm.put("Math", 98);
hm.put("Data Structure", 85);
hm.put("Database", 91);
hm.put("Java", 95);
hm.put("Operating System", 79);
hm.put("Networking", 80);
Map hm1 = sortByValue(hm);
// print the sorted hashmap
for (Map.Entry en :
hm1.entrySet()) {
System.out.println("Key = " + en.getKey()
+ ", Value = "
+ en.getValue());
}
}
}
输出
Key = Operating System, Value = 79
Key = Networking, Value = 80
Key = Data Structure, Value = 85
Key = Database, Value = 91
Key = Java, Value = 95
Key = Math, Value = 98
在Java 8 中使用流
在这里,我们将使用流对地图进行排序。我们将使用stream()方法获取 entrySet 的流,然后使用 sorted() 方法中的 lambda 表达式对流进行排序,最后,我们将使用 toMap() 将其转换为映射 方法。在 toMap() 方法中,我们使用LinkedHashMap::new方法引用来保留地图的排序顺序。
Java
// Java program to sort hashmap by values
import static java.util.stream.Collectors.*;
import java.lang.*;
import java.util.*;
import java.util.stream.*;
import java.util.stream.Collectors;
public class gfg3 {
// function to sort hashmap by values
public static HashMap
sortByValue(HashMap hm)
{
HashMap temp
= hm.entrySet()
.stream()
.sorted((i1, i2)
-> i1.getValue().compareTo(
i2.getValue()))
.collect(Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(e1, e2) -> e1, LinkedHashMap::new));
return temp;
}
// Driver Code
public static void main(String[] args)
{
HashMap hm
= new HashMap();
// enter data into hashmap
hm.put("Math", 98);
hm.put("Data Structure", 85);
hm.put("Database", 91);
hm.put("Java", 95);
hm.put("Operating System", 79);
hm.put("Networking", 80);
Map hm1 = sortByValue(hm);
// print the sorted hashmap
for (Map.Entry en :
hm1.entrySet()) {
System.out.println("Key = " + en.getKey()
+ ", Value = "
+ en.getValue());
}
}
}
输出
Key = Operating System, Value = 79
Key = Networking, Value = 80
Key = Data Structure, Value = 85
Key = Database, Value = 91
Key = Java, Value = 95
Key = Math, Value = 98