• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sorting values in a tree map with string and list bean as parameters

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying out a solution to sort a tree map based on its values

Let me declare the Map



i have created a separate comparator method to compare the values .


ValueComparator class



I knew the values should be of type string for the comparable to Work.But in my case , it will be a bean list.So how to get the comparator to work for my case ?

i am stuck in this for a while ..Any suggestions and ideas would be great .

Thanks guys in advance...
 
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should implement a parameterized instance of Comparator, rather than using raw type, which you're doing currently. So, change your ValueComparator declaration to:

Also, change all the raw type usage of Map with parameterized instantiation. You should not use raw types in newer code.

jegadees waran wrote:I knew the values should be of type string for the comparable to Work


Where did you read that? It's not the case. You can have Comparator of any type, as in above declaration.
 
jegadees waran
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jain,

Thanks for your reply..I have modified most of My maps with parameters..But while doing it, i couldn't able to create a instance of comparator in MapSort class



I get an error here..
 
R. Jain
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What error are you getting?
P.S: Please don't edit your post, as further reply becomes meaningless. Rather add a new post with modified code.
 
jegadees waran
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The constructor TreeMap<String,List<Bean>>(ValueComparator) is undefined
 
R. Jain
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops! It was my mistake. ValueComparator should implement the Comparator<String>, which is the key here.
 
jegadees waran
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This sounds confusing for me..Can you please explain with my code here ..To be specific about the issue which i am facing ..
1)i have a TreeMap with string and list bean as parameters.Since the treemap is not ordered , i need to order it through the bean which contains the date
2) The ordered treeMap will again be inserted to main resultMap ..

i will be passing the Map<String,List<Bean>> to my MapSort class



How to proceed from here?I need to incorporate the comparator in this class too ..
 
R. Jain
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What are you confused about? Did you try modifying the ValueComparator to implement Comparator<String>? In that case, the compare() method will take 2 String arguments, which will be 2 keys in your map. Since you already have the map in your ValueComparator class, just get the value for both the keys, and compare them accordingly.
 
jegadees waran
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In My case i need to order the Map based on the values which i have got and not on the keys..
So String will be the key and List<Bean> will be my values..I need to use an comparator on the list <Bean> for ordering..

Hope this clears your question..Thanks
 
R. Jain
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're only writing the same text of question and again and again, and are not showing the modified ValueComparator. Try to read and understand my last response. I'm not going to give you the complete solution. You've to come up with that on your own. I'll just help you with that. I'm saying it again - can you try implementing the Comparator<String>. You seem to be thinking that, it will be used for comparing keys. But, you can also use it to sort map on value. You just have to use Map#get(key) method to get the value for both the keys.
 
jegadees waran
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This will be my MapSort class



ValueComparator class with String implementation



 
R. Jain
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good. Now you've setup the skeleton of your ValueComparator. Moving forward, what do you think you would need in order to compare the values in the compare() method? You know the String arguments are keys and not the values themselves. So how are you going to get the value for those keys?
HINT: You already have the map in your ValueComparator class. So, if I give you a map key, how would you get the value for that key in the map? Add code for this in your compare() method.

Once you are able to get the values, whose type is List<Bean>, for both the keys, you can then move ahead with actually comparing those lists.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jegadees waran wrote:I am trying out a solution to sort a tree map based on its values...


First off - you need to get a few pieces of terminology straight:

1. You don't 'sort' a TreeMap because it's already "sorted" (indeed, it implements SortedMap (←click)). That is to say: it already has an order. What you might need to do is order it differently (but I suspect not).

2. Don't confuse the words 'sort' and 'order'. 'Sort' is an action - usually done by a method; 'Order' is implicit - ie, it says how you want the items (in your case, it would appear, lists) TO BE sorted. Comparator implements an order; it does NOT sort things.

The implication of the first point is that ALL TreeMaps have an order - even your "unsorted" one. This in turn means that either:
1. The items being stored are Comparable (java.lang.Comparable), or:
2. You supply a Comparator when you create the TreeMap.

If one of those two things isn't true, then you will get errors.

Second: We (or I) am still not sure why you want to do this. If we did, we might be able to advise you better.
However, just off the top of my head:
1. A TreeMap called "unsorted" just doesn't make any sense.
2. Your declaration is for a TreeMap<String, List<Bean>>, but I still have no idea what the "String" part is supposed to contain. If it's just a list of all the values in your List, then it's completely redundant and simply a waste of space; and I think you'd be much better off with a TreeSet<List<Bean>>.

However, it all comes back to not knowing what you're trying to do, so if you could TellTheDetails (←click), I think we'd be a lot better off.

HIH

Winston
 
jegadees waran
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Winston ,

Apologies for posting a question which is not clear..To brief more on my question

I have two maps



ResultMap is already loaded with Values from query result.


The outputMap will be as follows



I need to order the outputMap as per courseDate and insert it back to the resultMap..

These are the things i am trying to do..Thanks..



 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jegadees waran wrote:I need to order the outputMap as per courseDate and insert it back to the resultMap.
These are the things i am trying to do..Thanks...


OK, Now we're getting somewhere. However, I now have some more questions.

What is the "ADV001"? It would appear to be a key generated by whatever database you're getting these results from, but I'm not quite sure how you come up with "ADV003". Is this a "next sequence number"? If so, it probably (not sure) has no business in a Java program. I'd simply write it the value back to the database and let IT deal with the "next sequence" stuff.

Which all gets back to the fact that I don't think a Map (of any sort, but particularly a TreeMap) is what you actually want. A Set or a List seem much more likely.

But we still don't know what you're trying to do. This looks like course data, but I'm still in the dark as to why you're doing this. And it's very difficult to give advice when you don't know what's going on.

And if it's an issue of data privacy, it's even more important to tell us the problem. What you have given us so far is your solution (which is plainly not working).

Winston
 
Did you miss me? Did you miss this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic