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 8619793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:25:33+00:00 2026-06-12T06:25:33+00:00

I want to sort a list, using a map which contains the values for

  • 0

I want to sort a list, using a map which contains the values for each item.

Map<Integer, Float> map = new HashMap<>();
List<Integer> list = new ArrayList<>();

map.put(0, 0.0f);
map.put(1, 5.0f);
map.put(2, 2.0f);

list = new ArrayList<>(map.keySet());

Collections.sort(list, new Comparator<Integer>() {
    public int compare(Integer left, Integer right) {
        Float leftCost = map.get(left);
        Float rightCost = map.get(right);
        return leftCost.compareTo(rightCost);
    }
})

I want the order to be 0,2,1 because the value of 1 is higher than 2. But java dont let me do this. I get the following error: Cannot refer to a non-final variable map inside an inner class defined in a different method

How can I do this a way like that?

  • 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-12T06:25:34+00:00Added an answer on June 12, 2026 at 6:25 am

    Just make it final:

    final Map<Integer, Float> map = new HashMap<Integer, Float>();
    List<Integer> list = new ArrayList<Integer>(); // this assignment is unncessary [1]
    
    map.put(0, 0.0f);
    map.put(1, 5.0f);
    map.put(2, 2.0f);
    
    list = new ArrayList<Integer>(map.keySet()); // 1. assignment is replaced here
    
    Collections.sort(list, new Comparator<Integer>() {
        public int compare(Integer left, Integer right) {
            Float leftCost = map.get(left);
            Float rightCost = map.get(right);
            return leftCost.compareTo(rightCost);
        }
    })
    

    As your map is mutable, you can still modify it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking on how to sort a list effectively using weighted values. Each item
I want to sort a list of integer values, but before sorting them I
I want to sort list items by their priority, which the user types in,
I have List I want to sort Desc by Priority, which is int and
Possible Duplicate: Java Ordered Map I have list of product object in the HashMap<Integer,Product>
I want to sort a list of person say List<Person> persons=new List<Person>(); persons.Add(new Person(Jon,Bernald,45000.89));
I have a vector of Student objects which I want to sort using #include
I want to sort a list of objects using a value that can take
If I have a Map like this: HashMap<Integer, ComparableObject> map; and I want to
I want to sort a list of strings based on the string length. I

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.