[OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
-Deepak
SCJP - 93%
My Blog
Divyanand Kutagulla wrote:Am doing this right?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Deepak Bajoria wrote:...but in case you want to have a map sorted according to values, this is what you can do...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
1. Your compare() method doesn't check when values are equal and return 0. This might be deliberate in order to retain ALL the original key mappings, but I suspect it's flawed.
Winston Gutkowski wrote:
2. What would you expect sortedRepresentation.get("X") to return (assuming "X" is a valid key)? I suspect you'll get null more often than you think; but I have to admit, I'm not absolutely sure.
-Deepak
SCJP - 93%
My Blog
Deepak Bajoria wrote:This comparator is designed to sort only the map which was passed to it while invoking the constructor, in which case sortedRepresentation.get("X") is never going to give null.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Jeanne Boyarsky wrote:What are you trying to accomplish? It looks like sorting the map by the values while retaining the relationship between key/value pairs? If so, I don't see any problems with your approach.
Deepak Bajoria wrote:What you are doing here is creating a separate LinkedList of EntrySet and sorting it according to Values. While this seems OK, but in case you want to have a map sorted according to values, this is what you can do.
Create a comparator
Now create a TreeMap to use this comparator:
This would be slightly cleaner way of doing the thing.
Winston Gutkowski wrote:
Really? You're creating a TreeMap, which is a binary tree structure. Since your Comparator is actually only concerned with what was in the original Map, and is going to order those keys according to their associated values, I could definitely see problems if the original Map is subsequently updated; not to mention the idea of ordering something (your String keys) by something they're not even directly associated with, AND which might be duplicated [Edit: thinking about it; that may actually be the showstopper].
It might work as you've written it (ie, with no "equal" value), and like I say, it's an interesting idea; but I'm not sure I'd want to do any predictive analysis on it.
Winston
-Deepak
SCJP - 93%
My Blog
Winston Gutkowski wrote:As Jeanne said: the approach seems reasonable, except for one thing: LinkedLists take quadratic time to sort.
Divyanand Kutagulla wrote:
Hi Deepak,
Thank you for your reply.
In my code I do create a comparator
I wanted to use Collections.sort method to accomplish the sorting And I wanted to be memory efficient
Yes creating a TreeMap would help was it is sorted map. but it would take additional memory, I already have a map - granted it is not sorted, but I can use the Collections.sort to sort it with the right comparator.
Divyanand
-Deepak
SCJP - 93%
My Blog
Matthew Brown wrote:You sure about that? I'm pretty sure Collections.sort has N log N performance even on linked lists - it copies it to an array, sorts it there, and copies it back.
Deepak Bajoria wrote:Thanks Winston, I see what you mean. What I am trying to do is to figure out how the ArrayList or the ReverseMap approach that you suggested is going to solve the problem of additional element being added to the original map after sort is performed.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Divyanand Kutagulla wrote:Further to my question I modifiwd my sorting code thus:
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
Divyanand Kutagulla wrote:Further to my question I modifiwd my sorting code thus:
Looks fine, and although Matthew is almost certainly correct about how Collections.sort() works, I suspect it will be a bit faster.
However, even better is probably:
List<Map.Entry<String, Integer>> representationEntries = new ArrayList<>(representations.entrySet());
The fact is Collections.sort() doesn't care what kind of List it's sorting, so it's usually best not to restrict it.
Indeed, it's almost always better to use interfaces for collections except when you're actually creating them. It's part of a technique called "programming to the interface", and applies to things other than just collections too.
HIH
Winston
Divyanand Kutagulla wrote:This post and the other post on Map Iteration ( sorry dunno how to link it) were motivated by my experiments with collections and the Date, Calendar and locale classes.
I am am prepping for the OCP 7 upgrade exam and (i am a 1.2 certified programmer), and thought I'd write some code to help to understand what I am learning.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Don't get me started about those stupid light bulbs. |