Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Java

Chương trình Java sắp xếp Map theo Value


Trong bài viết này, chúng ta sẽ hiểu cách sắp xếp Map theo Value. Java HashMap là triển khai bảng băm của interface Map trong Java. Nó là tập hợp các cặp key-value.

Minh họa dưới đây:

Giả sử đầu vào là:

Input HashMap: Key = Java, Value = 45 Key = Scala, Value = 20 Key = Mysql, Value = 11 Key = Python, Value = 75

Đầu ra mong muốn:

The HashMap after sorting is: Key = Mysql, Value = 11 Key = Scala, Value = 20 Key = Java, Value = 45 Key = Python, Value = 75

Thuật toán

Bước 1 - BẮT ĐẦU 
Bước 2 - Khai báo biến 
Bước 3 - Định nghĩa giá trị 
Bước 4 - Dùng 'sort' sắp xếp phần tử map 
Bước 5 - Dùng 'getValue' lấy value và 'compareTo' so sánh hai value 
Bước 6 - Duyệt hashmap, dùng 'getKey' lấy giá trị vào 'temp'. Dùng 'temp' làm return value 
Bước 7 - Hiển thị kết quả 
Bước 8 - DỪNG

Ví dụ 1

Gom thao tác dưới 'main'.

import java.util.*; 
import java.lang.*; 

public class Demo { 
    public static HashMap sort(HashMap input_map){ 
        List> list = 
            new LinkedList>(input_map.entrySet()); 
        Collections.sort(list, new Comparator>() { 
            public int compare(Map.Entry o1, 
                Map.Entry o2){ 
                return (o1.getValue()).compareTo(o2.getValue()); 
            } 
        }); 
        HashMap temp = new LinkedHashMap(); 
        for (Map.Entry aa : list) { 
            temp.put(aa.getKey(), aa.getValue()); 
        } 
        return temp; 
    } 
    
    public static void main(String[] args){ 
        HashMap input_map = new HashMap(); 
        input_map.put("Java", 45); 
        input_map.put("Scala", 20); 
        input_map.put("Mysql", 11); 
        input_map.put("Python", 75); 
        System.out.println("The HashMap is defined as:"); 
        for (Map.Entry element : input_map.entrySet()) { 
            System.out.println("Key = " + element.getKey() + ", Value = " + element.getValue()); 
        } 
        Map result_map = sort(input_map); 
        System.out.println("\nThe HashMap after sorting is:"); 
        for (Map.Entry element : result_map.entrySet()) { 
            System.out.println("Key = " + element.getKey() + ", Value = " + element.getValue()); 
        } 
    } 
}

Kết quả

The HashMap is defined as: 
Key = Java, Value = 45 
Key = Scala, Value = 20 
Key = Mysql, Value = 11 
Key = Python, Value = 75 
The HashMap after sorting is: 
Key = Mysql, Value = 11 
Key = Scala, Value = 20 
Key = Java, Value = 45 
Key = Python, Value = 75

Ví dụ 2

Đóng gói thao tác vào hàm thể hiện lập trình hướng đối tượng.

import java.util.*; 
import java.lang.*; 

public class Demo { 
    public static void main(String[] args){ 
        HashMap input_map = new HashMap(); 
        input_map.put("Java", 45); 
        input_map.put("Scala", 20); 
        input_map.put("Mysql", 11); 
        input_map.put("Python", 75); 
        System.out.println("The HashMap is defined as:"); 
        for (Map.Entry element : input_map.entrySet()) { 
            System.out.println("Key = " + element.getKey() + ", Value = " + element.getValue()); 
        } 
        List> list = 
            new LinkedList>(input_map.entrySet()); 
        Collections.sort(list, new Comparator>() { 
            public int compare(Map.Entry o1, 
                Map.Entry o2){ 
                return (o1.getValue()).compareTo(o2.getValue()); 
            } 
        }); 
        HashMap result_map = new LinkedHashMap(); 
        for (Map.Entry aa : list) { 
            result_map.put(aa.getKey(), aa.getValue()); 
        } 
        System.out.println("\nThe HashMap after sorting is:"); 
        for (Map.Entry element : result_map.entrySet()) { 
            System.out.println("Key = " + element.getKey() + ", Value = " + element.getValue()); 
        } 
    } 
}

Kết quả

The HashMap is defined as: 
Key = Java, Value = 45 
Key = Scala, Value = 20 
Key = Mysql, Value = 11 
Key = Python, Value = 75 
The HashMap after sorting is: 
Key = Mysql, Value = 11 
Key = Scala, Value = 20 
Key = Java, Value = 45 
Key = Python, Value = 75