Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple Data structure Question Re: HashMaps

 
Ted Striker
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First time. Long time. Great site.
I have a relatively simple question. I have a Car() and a test class called CarPlant(). In the test class, I am trying to take 2 collections (one of car names, and a collection of car makers). I want to create a new collection of car sorted by maker.
Comment:
1. Ideally, I need to write another class that holds all the collections so in my test class, the method with ask for a maker and the available cars will display. I'm not there yet. Any suggestions or guidance would be appreciated.
Question: Please view my compare method within the CarPlant(). I am trying to return only the keys that are common between the two collections(cars,makers). Then I was thinking that would provide the cars sorted by maker. It's not working. Any ideas?
Thanks for the assistance.

 
Jon Strayer
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All you need to do is have Car implement Comparable and them implement compareTo so that is compares the makeName first. Something like:

This is untested code that only sorts by make and model. You may want to add a year comparison as well.
Then, any List containing cars can be sorted by Containers.sort
[ December 09, 2002: Message edited by: Jon Strayer ]
 
Ted Striker
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jon but I was hoping to avoid using an interface. Also, the more I look at it, just comparing the two hashmaps won't answer my question. I need to compare then remove. I imagine I'll use remove from iterator but I'm still plugging away.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want the results to be sorted, you really should implement Comparable or create a new Comparator class. (You casn avoid it, but your sulution will probably just be more compleex.) However if we ignore the sorting issue and focus on identifying the keys that are common in both maps, removing those that are not, we have a much simpler solution:

All non-duplicate keys have now been discarded, along with their corresponding Map entries.
If you require additional processing or control over what gets kept and what is deleted, you could also try something like this:

After all this, keys will have no entries that aren't in makers. However, makers may have entries not in keys. So you'll probably want to repeat the above process with the roles of keys and makers reversed. Modified with whatever other logic you wish to insert of course.
Also, note that there are two meanings of "key" in the above code, leading to confusing names like keysKey and keysValue. The terms "key" and "value" are already in use in the Map API, so for your own classes it might be wise to choose a slightly different term to avoid confusion.
 
Ted Striker
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the reply. I've read your posts and referenced the API some more. I think I have a better handle on what my options are and how to go about it. I'm still struggling a bit but I have more direction now (which is 1000x better than late last night).
When I worked through your code sample above I was caught up on a Casting Error I couldn't work through . Can you give me another push?
Modified and cleaned up (typo's, wrong constructor, etc..) code from above, blows up at this line with a Cast Exception.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like I wrote too quickly. Looking at your code again, the keys in each Map seem to be Car objects, not Strings. So cast them to Car rather than String...
 
Ted Striker
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was on board but I've fallen off.
From this code:

Cast to Car instead of String:

Cars keysKey will compile and run
Cars keysValue blows up blows up during run-time with a ClassCastException java.lang.Integer error.
What little step am I doing wrong. I know it's a simple thing, unfortunately I just don't see it.
Thanks again
 
Ted Striker
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While battling the first casting error;
I worked through the Comparator interface as recomended by Jon. I extended my Car class and implemented Caparable. I started simple and used an easy method.

I have another casting error!! Errrrr so frustrating. So I have two potential solutions, two casting errors. Please show me what I am doing wrong. I didn't get this method to work either.

What I am trying to do is sort one collection of objects by another collection. I plan to use the key to see what is common. The rest are removed.
[ December 10, 2002: Message edited by: Ted Striker ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ted, you need to figure out what class your objects actually are, and cast them to that class. I used Strings for both values and keys because I confused this with a similar problem I had just posted about. But it should be easy to modify if you look at how you're using the collections.

When you use the put() method, the first argument is the key, and the second is the value. What class is the key in this map? And what class is the value? These are the classes you need to cast to when you take data out of the map.
[ December 10, 2002: Message edited by: Jim Yingst ]
 
Ted Striker
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it! Now I'm cooking with gas. Thanks for the help.
Now the trick is to use your logic and create a method that sorts how I want it to. Thanks again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic