Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Java XML & JSON this week in the XML and Related Technologies forum!
  • 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
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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: 12952
216
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: 20307
110
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: 10720
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: 10720
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: 5456
55
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?
 
If somebody says you look familiar, tell them you are in porn. Or in these tiny ads:
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!