Takes out the HashMap value and returns the result sorted by the value of value as a list.

Here is a summary of how to sort Map (HashMap) using value in java. Here, we are considering a situation where you want to sort the values of A_array at the same time using B_array. Since it is a form that sorts a list with an entry type of HashMap, it can be used with other Maps such as TreeMap.



import java.util.*;

class Solution {
    public static void main(String[] args) {
        int[] A_array = {1,6,3,10,5};
        int[] B_array = {8,21,13,4,5};
        tryComperator(A_array, B_array);
    public static void tryComperator(int[] a, int[] b){
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        //Store the elements of the array in hashmap.
        for (int i = 0; i < a.length; i++){
            map.put(a[i], b[i]);
        //Store each element of Hashmap in a list.
        List<Map.Entry> mapValuesList = new ArrayList<Map.Entry>(map.entrySet());
        //Try to output mapValuesList.(10=It seems that it is output in the form of 4.)
        for (Map.Entry v: mapValuesList){
            System.out.println("map>> "+v+",key>> "+v.getKey()+"value>> "+v.getValue());
        //Instantiate the Comparator class
        Comparator<Map.Entry> comparator = new Comparator<Map.Entry>() {
            public int compare(Map.Entry entry1, Map.Entry entry2) {
                return ((Integer) entry1.getValue()).compareTo((Integer) entry2.getValue());
        //Sort using comparator.
        Collections.sort(mapValuesList, comparator);
        //Output for confirmation
        for (Map.Entry s : mapValuesList) {
map>> 1=8,key>> 1value>> 8
map>> 3=13,key>> 3value>> 13
map>> 5=5,key>> 5value>> 5
map>> 6=21,key>> 6value>> 21
map>> 10=4,key>> 10value>> 4

It was output in ascending order of value.

Reference: http://kevin3sei.blog95.fc2.com/blog-entry-159.html

Recommended Posts

Takes out the HashMap value and returns the result sorted by the value of value as a list.
Pass arguments to the method and receive the result of the operation as a return value
Display the average value of the evaluation as a star
[ruby] How to assign a value to a hash by referring to the value and key of another hash
The value is taken out from the double hash and output by the getter.
Sample program that returns the hash value of a file in Java
Get the path defined in Controller class of Spring boot as a list
Create a Yellowfin custom formatter and display the minus of the number as △ (triangle)
Be absolutely careful when putting the result of and / or in a variable!
I want to find the MD5 checksum of a file in Java and get the result as a string in hexadecimal notation.
[Java] Cut out a part of the character string with Matcher and regular expression
Simple statistical analysis of monetary value from the list searched by Mercari Java development
[Java] The problem that true was returned as a result of comparing Integer with ==
A review of the code used by rails beginners
ArrayList and the role of the interface seen from List
[Rails] Volume that displays favorites and a list of favorites
Extract a specific element from the list of objects
I want to get a list of the contents of a zip file and its uncompressed size
Branch processing by the return value of RestTemplate and the status code of ResponseEntity in Spring Boot
An application that acquires the value of the accelerometer by UDP communication between C # and Android