• Post Reply Bookmark Topic Watch Topic
  • New Topic

Replace multiple duplicate values of hashmap  RSS feed

 
Romeo Ranjan
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All-

I have a hashmap that has duplicate values.


Expected Output



If the value has no duplicates then no change. If yes, I want to replace the second value by incrementing the series (from 2000 to 2001) and if there is another duplicate then the next series 2002. Looking for some clean and simple idea of implementing this, no third party tools. So far I have thought of this logic in terms of

 
Stephan van Hulst
Saloon Keeper
Posts: 7991
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This looks like a strange requirement. Where are you getting your initial map from? Why are the numbers saved as Strings, and not simply as a second mapping to Integers?

If you're bent on doing this, I would find some way to keep track of the last series you used per value. I wonder how you could keep track of such a mapping?

Note that the result of the algorithm is undefined, unless you use an ordered input map, such as LinkedHashMap or SortedMap.
 
Romeo Ranjan
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply. Tracking of the last series value is already taken care of. The intialmap values are from the database and thats how we store some of our values. I am ok with it not being ordered. All I want is the second value to have the incremented series and so on and a best approach to do this.

Thanks!
 
Stephan van Hulst
Saloon Keeper
Posts: 7991
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So far you have created a mapping from duplicate values to the frequency of those values. This doesn't really tell you anything. What you want to do instead, is *while* iterating over your map entries, you want to update them according to the amount of times you've encountered a particular value *so far*. You can do all of this in one loop.
 
Romeo Ranjan
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But then how to omit the first entry of the duplicate value and increment the following.

 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In line 4 of Stephans code there may be some difficulty in that the number part of the Values
is not unique. So I like the idea of using a map of the values/frequencies, as OP suggested.
and keeping track how often you have seen that value in Stephans algorithm.

Another way I can think of, is to put the hashmap into a treemap, with a comparator
that compares two map.entries on their value part, and, possibly, then on their key part.
It is then easy to iterate over this treemap, updating the value if the value is equal
to the previous value, and put the eventually updated map.entry in a new hashmap.

Question: is it possible for the original hashmap to have values like "E 2000" and "E 2005"?
If so, then that is some extra complication.

Greetz,
Piet
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmmm... a treemap might not be such a good idea, on second thought.

I like the idea of defining a class Pair<String, String>, that implements
Comparable<String, String>. Then create a List of these things.
(a direct List of Map.Entry would also be possible).
Put all the map.entries in it, sort the list, and then iterate over it,
updating the values as you go.

Gretz,
Piet
 
Stephan van Hulst
Saloon Keeper
Posts: 7991
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Romeo, you don't omit the first entry. Entries that are not duplicates are trivial special cases that will be handled correctly by the algorithm, that's why there's a lastNumber != null check.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!