Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8622503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:04:20+00:00 2026-06-12T07:04:20+00:00

Hi I am using following code to sort my HashMap, it sorts the map

  • 0

Hi I am using following code to sort my HashMap, it sorts the map correctly but does not count the repeated values,

Map<String, Integer> mymap = new HashMap<String, Integer>();
mymap.put("item1", 5);
mymap.put("item2", 1);
mymap.put("item3", 7);
mymap.put("item4", 1);

Map<String, Integer> tempMap = new HashMap<String, Integer>();
for (String wsState : mymap.keySet()) {
    tempMap.put(wsState, mymap.get(wsState));
}

List<String> mapKeys = new ArrayList<String>(tempMap.keySet());
List<Integer> mapValues = new ArrayList<Integer>(tempMap.values());
HashMap<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
TreeSet<Integer> sortedSet = new TreeSet<Integer>(mapValues);
Object[] sortedArray = sortedSet.toArray();
int size = sortedArray.length;
for (int i = 0; i < size; i++) {
    sortedMap.put(mapKeys.get(mapValues.indexOf(sortedArray[i])),
            (Integer) sortedArray[i]);
}
for (Map.Entry<String, Integer> entry : mymap.entrySet())
    System.out.println("Item is:" + entry.getKey() + " with value:"
            + entry.getValue());

System.out.println("***");

for (Map.Entry<String, Integer> entry : sortedMap.entrySet())
    System.out.println("Item is:" + entry.getKey() + " with value:"
            + entry.getValue());

Result is as following (the item 4 is not shown because its value is same as item2’s !!!) :

Item is:item4 with value:1
Item is:item2 with value:1
Item is:item3 with value:7
Item is:item1 with value:5
***
Item is:item2 with value:1
Item is:item1 with value:5
Item is:item3 with value:7

It is a HashMap and need to be sorted by value.
Expected output is :

Item is:item3 with value:7
Item is:item1 with value:5
Item is:item2 with value:1
Item is:item4 with value:1

OR

Item is:item2 with value:1
Item is:item4 with value:1
Item is:item1 with value:5
Item is:item3 with value:7
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T07:04:21+00:00Added an answer on June 12, 2026 at 7:04 am

    You are using TreeSet<Integer> sortedSet

    SETS by definition will not allow duplicates.

    Here is an example that is Sorting by Value like you expect without losing any of the Entries.

    import java.util.*;
    
    public class Test {
    
    public static Map<String, Integer> sortByValueDesc(Map<String, Integer> map) {
        List<Map.Entry<String, Integer>> list = new LinkedList(map.entrySet());
        Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o2.getValue().compareTo(o1.getValue());
            }
        });
    
        Map<String, Integer> result = new LinkedHashMap<>();
        for (Map.Entry<String, Integer> entry : list) {
            result.put(entry.getKey(), entry.getValue());
        }
        return result;
    }
    
        public static void main(String[] args) {
    
            HashMap<String, Integer> map = new HashMap<String, Integer>();
    
            map.put("item1", 1);
            map.put("item2", 2);
            map.put("item3", 1);
            map.put("item4", 7);
            map.put("item5", 3);
            map.put("item6", 4);
    
            for (Map.Entry<String, Integer> entry : map.entrySet()) {
                System.out.println("Item is:" + entry.getKey() + " with value:"
                        + entry.getValue());
            }
    
            System.out.println("*******");
    
            Map<String,Integer> sortedMap = sortByValueDesc(map);
    
            for (Map.Entry<String, Integer> entry : sortedMap.entrySet()) {
                System.out.println("Item is:" + entry.getKey() + " with value:"
                        + entry.getValue());
            }
    
        }
    
    }
    

    The result I am Getting is (Now That I check you want greater values to be first):

    Item is:item4 with value:7
    Item is:item2 with value:2
    Item is:item3 with value:1
    Item is:item1 with value:1
    Item is:item6 with value:4
    Item is:item5 with value:3
    *******
    Item is:item4 with value:7
    Item is:item6 with value:4
    Item is:item5 with value:3
    Item is:item2 with value:2
    Item is:item3 with value:1
    Item is:item1 with value:1
    

    For why you are losing one element here is your problem:

    //HERE YOU ARE GETTING ALL THE VALUES
    List<Integer> mapValues = new ArrayList<Integer>(tempMap.values());
    HashMap<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
    
    //YOU ARE INSERTING THE VALUES TO A TreeSet WHICH WILL REMOVE DUPLICATES
    TreeSet<Integer> sortedSet = new TreeSet<Integer>(mapValues);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the following java code to sort the values: - Collections.sort(values); It
I am using the following code to sort an ArrayAdapter alphabetically: adapter.sort(new Comparator<String>() {
I am currently using the following code to sort a dictionary: Dictionary<string, TKosStock> NewList
Using following code I try to get updated list of checkbuttons' corresponding text values,
I want to draw some sort graphics on winform using following code : private
I do not see any improvements in processing speed using the following code: IEnumerable<Quote>
I am using the following code to keep list of objects in a MAP,
I'm using the following code to sort the results in a UIPicker. The results
I am trying to sort a xml using linq queries using the following code
I am using the following code to draw text onto a jpg image but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.