File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Is it possible to insert a new key value pair at a position in a Map? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Is it possible to insert a new key value pair at a position in a Map?" Watch "Is it possible to insert a new key value pair at a position in a Map?" New topic
Author

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

Preet Prasannan.
Ranch Hand

Joined: Feb 09, 2009
Posts: 64
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


"The more I learn,the more,I get to know, is left to learn."
Preet Prasannan.
Ranch Hand

Joined: Feb 09, 2009
Posts: 64
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

Joined: Apr 06, 2010
Posts: 4425
    
    8

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

Joined: Feb 09, 2009
Posts: 64
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

Joined: Apr 06, 2010
Posts: 4425
    
    8

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

Joined: Jan 28, 2008
Posts: 5575

index based sorting!. HashMap uses hashing to store an element . it is not possible. better choice is ArrayList
Preet Prasannan.
Ranch Hand

Joined: Feb 09, 2009
Posts: 64
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is it possible to insert a new key value pair at a position in a Map?