• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

sort array list of strings in descending order comparing to its equivalent in hashmap  RSS feed

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:It seems to me a concrete example is needed to clarify the requirements. Plain English descriptions can be inexact and ambiguous and open to individual interpretation.

What I understand is this:

Given a Map<String, Integer> {
 "English" -> 90,
 "Spanish" -> 70,
 "Bahasa" -> 50,
 "Japanese" -> 62,
 "Tamil" -> 5,
 "French" -> 73,
 "German" -> 79
}

The first step of filtering results in a String array containing everything except Bahasa and Tamil since these have values less than 60.

Next step is to sort them in descending order by value in the Map so:

["English", "German", "French", "Spanish", "Japanese"]

That is final result.

Is this the correct interpretation of the requirements?


In my understanding, it should be German, French, Japanese, Spanish, English (sorted descending by String, not by Integer).
 
Sheriff
Posts: 13366
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Marcello Lippi wrote:In my understanding, it should be German, French, Japanese, Spanish, English (sorted descending by String, not by Integer).


That makes no sense at all. The "correct" list you gave is not even sorted alphabetically, not ascending nor descending.

Please go back to your instructor and ask for a concrete example so that people who want to help can stop guessing.

This is the biggest source of bugs in software, a lack of understanding of what really needs to be done.
 
Bartender
Posts: 20562
120
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you have something wrong in that interpretation.

The key sequence would be as follows:

Sorting descending by String (key) would be "Spanish, Japanese, French, German, English" or something like that.

Sorting descending by Integer (value) would be "English, German, French, Spanish, Japanese". Again, pardon me if I missed or misplaced an item.
 
Bartender
Posts: 10759
68
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Marcello Lippi wrote:Honestly, I would love to see solution for my initial code. Yes, it is extremely ugly, but at least I do understand it. In the same time, your solutions are brilliant, but I don't understand streams completely yet.


My thoughts exactly. We seem to have gone off on a "Stream tangent", when the answer for Marcello is very simple:

Instead of sorting the names of the languages in descending order, which is what you did in lines 19-20 or your code, sort the Entrys, filtered exactly as you've already done, by their VALUE.

That involves using Collections.sort(Comparator), which you may not have come across yet; but it's the only way to do it easily.
[EDIT]: And if you're on Java Version 8 or later, there's now a comparingByValue() method in Map.Entry which will return you exactly the Comparator you want.

You obviously already know how to filter your Map entries; just put the ones you want to keep in a List<Map.Entry<String, Integer>> instead of a List<String>.

If you have any questions about how to proceed from there, let us know.

But you've done fine so far - just a couple of hurdles left. :-)

Winston
 
Winston Gutkowski
Bartender
Posts: 10759
68
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And just FYI:

Marcello Lippi wrote:

is vastly overcomplicated, and probably only required because you didn't qualify your Map.Entry assignment.

You already know that your Map is a Map<String, Integer>, therefore its entries will be Map.Entry<String, Integer>s, and results.entrySet().iterator() will return an Iterator<Map.Entry<String, Integer>>; so your assignment should be:and the 'if' statement above should be:HIH

Winston
 
Saloon Keeper
Posts: 5755
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why work explicitly with an iterator when an enhanced for loop is much cleaner?
 
You totally ruined the moon. You're gonna hafta pay for that you know. This tiny ad agrees:
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!