Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is it possible to insert a new key value pair at a position in a Map?

 
Preet Prasannan.
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have some key value pairs.
I want to store the same inside a map.
But there is a requirement that I need to insert a new key value pair at a position i.e. may be between the first and the second key value pair.
Currently I iterate over this and store into a temporary Map and then store after rearranging the pairs.
Is is possible to do so in any other collection without iterating?

Thanks a lot,
Preet
 
Preet Prasannan.
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we have any other Collection (other than Map )that can be used to perform the same i.e. key value pair with positioning.?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the order you want based on some defined ordering of the keys?

By default a Map (e.g. a HashMap) doesn't really have an "order". From the HashMap documentation:
This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.

So if you're using a HashMap, your requirement doesn't actually make any sense.

However, you can use a SortedMap (e.g. a TreeMap). That keeps the map in order according to the keys - using either the natural order, or another order defined by using a Comparator object.

Any use?
 
Preet Prasannan.
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mathew,
Thanks.As you said ,I am indeed using a sorted map.
It is sorted.
I am able to insert by iterating over the map.
But lets say I have 1000 elements. And I need to insert a new key value pair on the 800th position.
So it would mean that I would have to iterate over 799 pairs.
Though it solves my purpose but I was looking for a cleaner code expecting to find if something like <collection>.put(key-value pair, position) exists in collection.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't quite understand. If you're using a java.util.SortedMap, the map itself makes sure that the order is maintained. You just use map.put(key, value), and it will automatically be placed in the right place.

If you're not using that type, exactly what type are you using?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
index based sorting!. HashMap uses hashing to store an element . it is not possible. better choice is ArrayList
 
Preet Prasannan.
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot to everyone.
Will use the sorted map.
And in case if I stumble on something,will update the same.
Thanks a lot once again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic