1

I use the code to Sort the data in the map.

 Map<Integer, Integer> map = new HashMap<>();
    List list = new ArrayList(map.entrySet());
    Collections.sort(list, new Comparator<Map.Entry<Integer, Integer>>() {
        @Override
        public int compare(Map.Entry<Integer, Integer> a, Map.Entry<Integer, Integer> b) {
            return a.getValue() - b.getValue();
        }
    });

I just copy the data from map to List and sort it. How can I get the data from the List? The method get() of list returns just the object, not my 2 integers

1
  • 1
    Don't use raw types. Your list should be of type List<Map.Entry<Integer, Integer>>. You already seem to know what a Map.Entry is, since you're sorting the list and accessing its value. So what's the problem? Have you read the javadoc of Map.Entry? Commented Dec 24, 2016 at 10:11

2 Answers 2

1

You do that :

 List list = new ArrayList(map.entrySet());

You could use generics in your list and then iterate on the Entry of the list after your sorted it:

List<Entry<Integer, Integer>> list = new ArrayList(map.entrySet());
...
// your sort the list
..
// you iterate on key-value
for (Entry<Integer, Integer> entry : list){
  Integer key =  entry.getKey();
  Integer value =  entry.getValue();
}
Sign up to request clarification or add additional context in comments.

2 Comments

Exception in thread "main" java.lang.Error: Unresolved compilation problems: The method values() is undefined for the type List Syntax error, insert ")" to complete EnhancedForStatementHeader at packS.SecondProject.SecondProject.main(SecondProject.java:85)
refresh the page I think. And don't forget to declare the List with the generic type. For the imports, specify those in java.util.
1

Your list actually contains elements of type Map.Entry<Integer, Integer>, so you can retrieve the each Entry as shown below:

Map<Integer, Integer> map = new HashMap<>();
//add the values to map here

//Always prefer to use generic types shown below (instead of raw List)
List<Map.Entry<Integer, Integer>> list = new ArrayList<>(map.entrySet());

Collections.sort(list, new Comparator<Map.Entry<Integer, Integer>>() {
  @Override
  public int compare(Map.Entry<Integer, Integer> a,
                  Map.Entry<Integer, Integer> b){
                       return a.getValue() - b.getValue();
    }
  });

  //loop over the list to retrieve the list elements
  for(Map.Entry<Integer, Integer> entry : list) {
        System.out.println(entry.getValue());
  }

2 Comments

My Eclipse can not convert from element type Object to Map.Entry<Integer, Integer>.
@MikhailNono that's precisely because you declared your list as List, rather than List<Map.Entry<Integer, Integer>>. Read all the answers and comments you got instead of ignoring them. If you didn't ignore them and still have a problem, then edit your question, post your new code, and post the exact and complete error message you get from the compiler.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.