java - Number list from a Map -


i have map simplifying in essense contains key values of data such as:

yahya, 4  john, 4 blake, 2 jill 2 janoe, 6 jilly 12 zapon, 5 zoe, 4 hamed, 1 

i need order following output:

1. jilly, 12 pts 2. janoe, 6 pts 3. zapon, 5 pts 4. john, 4 pts 4. yahya, 4 pts 4. zoe, 4 pts 7. blake, 2 pts 7. jill, 2 pts 9. hamed, 1 pts 

i have used comparator order map values according value:

public <k, v extends comparable<v>> map<k, v> sortbyvalues(final map<k, v> map) {     comparator<k> valuecomparator =  new comparator<k>() {         public int compare(k k1, k k2) {             int compare = map.get(k2).compareto(map.get(k1));             if (compare == 0) return 1;             else return compare;         }     };     map<k, v> sortedbyvalues = new treemap<k, v>(valuecomparator);     sortedbyvalues.putall(map);     return sortedbyvalues; } 

and read : how element position java map, order hashmap alphabetically value , more not sure how put together.

i know can use key values:

for (map.entry<string, integer> entry : map.entryset()){     system.out.println(entry.getkey() + " ," + entry.getvalue() + " pts");  } 

however 2 functionalities missing:

  1. sort alphabetically when keys same
  2. keep same numbering when value same , jump correct count afterwards.

tried this:

map<string, integer> map = sortbyvalues(grouplist);             int count = 1;             int counter = 1;             int previousscore = 0;              (map.entry<string, integer> entry : map.entryset()) {                 //counter = count;                 if (previousscore == entry.getvalue()) {                      system.out.println(counter - 1 + " " + entry.getkey() + "," + entry.getvalue() + " pts");                  } else {                      system.out.println(counter + " " + entry.getkey() + "," + entry.getvalue() + " pts");                     previousscore = entry.getvalue();                     count++;                 }                 counter++;              } 

any thoughts appreciated , welcome. can suggest method achieve required result ?

  1. in valuecomparator replace if (compare == 0) return 1; if(compare == 0) return k1.compareto(k2).
  2. when generating "numbering" (you have not shown code it), keep 2 counts. increase 1 of them on every iteration, , assign other one, if current value different last one. use latter generate "numbering".

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -