PUT key at a particular location in LinkedHashMap

Hi, Can a key be inserted at a particular index of LinkedHashMap. As LinkedHashMap is a doubly linked list I think there it a way out to add a key at particular index.Thanks,Rhea
[206 byte] By [Rheaa] at [2007-11-27 2:08:47]
# 1

> Hi,

>

> Can a key be inserted at a particular index of

> LinkedHashMap. As LinkedHashMap is a doubly linked

> list I think there it a way out to add a key at

> particular index.

>

> Thanks,

> Rhea

What is your real goal? Do you want to insert the values in a way

so that, later, when you call the iterator, that it returns the values in a particular order?

Regarding your original question: No. LinkedHashMap does not provide public methods

for choosing where to insert the new node (mainly because it would turn "add" into an O(n) operation).

Let's say you want to insert the node into the 4000th position.

That means LinkedHashMap would have to walk the list until it gets to the 4000th position.

And that goes against the whole purpose of having a HashMap (which is to have constant time add()/contains() under ideal load)

KathyMcDonnella at 2007-7-12 1:58:30 > top of Java-index,Core,Core APIs...
# 2

Thanks a lot for the answers.

Actually my requirement is

I need to maintain a sequence of numbers e.g. 1,2,3,4,5,6,7,8,9

The numbers can be added and removed dynamically

Irrespective of addition and deletion I need to maintain the order

e.g

1) LinkedHashMap has numbers 1,2,3,4,5,6,7,8,9

2) Remove(Start, End) removes the range out. i.e. Remove(3, 6) - LinkedHashMap has 1,2,7,8,9

3) Add(Start, End) add the range i.e. Add(3, 6) - LinkedHashMap will have 1,2,3,4,5,6,7,8,9

4) Unfortunately I am unable to find a data structure that will cater my requirement

Thanks,

Rhea

Rheaa at 2007-7-12 1:58:30 > top of Java-index,Core,Core APIs...
# 3
Wait, are you talking about a Set, or a Map?And do you want the elements sorted (by some criteria or comparator),or only ordered (meaning we maintain the original order and there are no logicalcomparison between them)?
KathyMcDonnella at 2007-7-12 1:58:30 > top of Java-index,Core,Core APIs...
# 4
I want the elements sorted but I need efficient dynamic addition and deleteion.I have implemented LinkedHashMap for my requirement because1. I need good efficient data structure which will allow addition and deletion very dynamic and quick.2. I need a order to
Rheaa at 2007-7-12 1:58:30 > top of Java-index,Core,Core APIs...
# 5

You did not answer my question.

It seems you need a "Set", not a "Map". (Do you know the difference?)

Another way to put it is: would a TreeSet work for you?

* It keeps elements sorted AT ALL TIME

* It has efficient insertion and deletion and iterator

http://java.sun.com/j2se/1.5.0/docs/api/java/util/TreeSet.html

KathyMcDonnella at 2007-7-12 1:58:30 > top of Java-index,Core,Core APIs...
# 6
Hi Kathy,Thanks so much for your response. Actually I am not able to decide the data structure for my requirement, hence I was unable to answer your question of which data structure I need.I will check TreeSetThanks once again,Rhea
Rheaa at 2007-7-12 1:58:30 > top of Java-index,Core,Core APIs...
# 7
The very very sad part of using TressSet is if I need elements 1,2,3,4,5,6,7,8,9,10,20The tree maintains element as 1,10,2,20,3,4,5,6,7,8,9hence I cannot use TreeSet as wellThanks,Rhea
Rheaa at 2007-7-12 1:58:30 > top of Java-index,Core,Core APIs...
# 8

> The very very sad part of using TressSet is

>

> if I need elements 1,2,3,4,5,6,7,8,9,10,20

>

> The tree maintains element as

> 1,10,2,20,3,4,5,6,7,8,9

You're mistaken. 1 10 2 20 3 4 5 6 is if you insert Strings into it.

If you inserted Integers into it, then it will be 1 2 3 4 5 6 7 8 10 20

TreeSet<String> x = new TreeSet<String>();

x.add("2");

x.add("10");

x.add("1");

for(String elem:x) { System.out.println(elem); } // This will print 1 10 2

TreeSet<Integer> y = new TreeSet<Integer>();

y.add(2);

y.add(10);

y.add(1);

for(Integer elem:y) { System.out.println(elem); } // This will print 1 2 10

KathyMcDonnella at 2007-7-12 1:58:30 > top of Java-index,Core,Core APIs...
# 9
Yes, my numbers are repersented as String. I need them as String. Thanks,Rhea
Rheaa at 2007-7-12 1:58:30 > top of Java-index,Core,Core APIs...
# 10

> Yes, my numbers are repersented as String. I need them as String.

Just because your INPUT is String, and your OUTPUT is String,

doesn't mean you cannot store them in a TreeSet using its integer value.

Or, unless you are still withholding information.

Are the inputs always numerical? "100", "2", "300"?

Or can the input contain non-numbers also? "abc", "xyz"?

Addendum: even if you insist on storing Strings, you could use a custom comparator for TreeSet

so that it knows how to properly order them. But I don't want to suggest this

since I think you will be better off storing integers rather than Strings.

KathyMcDonnella at 2007-7-12 1:58:30 > top of Java-index,Core,Core APIs...
# 11
Yes, the inputs will always be numerical from 0 to 23Thanks,Rhea
Rheaa at 2007-7-12 1:58:30 > top of Java-index,Core,Core APIs...
# 12

Just because your INPUT is String, and your OUTPUT is String,

doesn't mean you cannot store them in a TreeSet using its integer value.

Addendum: even if you insist on storing Strings, you could use a custom comparator for TreeSet

so that it knows how to properly order them. But I don't want to suggest this

since I think you will be better off storing integers rather than Strings.

KathyMcDonnella at 2007-7-12 1:58:30 > top of Java-index,Core,Core APIs...
# 13
Actually Kathy, I string representation is a MUST for me.Thanks,Rhea
Rheaa at 2007-7-12 1:58:30 > top of Java-index,Core,Core APIs...
# 14

> Actually Kathy, I string representation is a MUST for me.

You are really, really irritating me.

1) The same data exists at multiple places in your system.

It comes in as characters (bytes in the byte stream, actually).

Then gets parsed into String. And becomes chars again when you System.out.println().

So the data already exists in multiple formats during your program.

For example, if you want to save your data in an Oracle database,

why would you care whether Oracle saves your String as chars/bytes/codepoint/whatever?

Just because your INPUT is String, and your OUTPUT is String,

doesn't mean you cannot store them INTERMEDIATELY as integer.

2) Unless, of course, this is an homework assignment. And that you have

wasted a huge chunk of my time by being vague.

KathyMcDonnella at 2007-7-12 1:58:30 > top of Java-index,Core,Core APIs...
# 15
To be very precise this is not a homework assignment, I was sincerely trying to getting my problem resolved. Being new to Java my questions may seem irritating to you, but honestly I am trying to get my things working.Thanks for spending your precious time.Rhea.
Rheaa at 2007-7-21 20:19:35 > top of Java-index,Core,Core APIs...
# 16

Okay, now listen to me very carefully.

1) You CAN store "1", "10", "2" into a TreeSet<String>.

Then write a custom comparator that knows "1" comes before "2" which comes before "10".

http://java.sun.com/j2se/1.5.0/docs/api/java/util/TreeSet.html

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Comparator.html

2) You CAN store 1, 10, 2 into a TreeSet<Integer>.

Then convert String into Integer before adding.

And convert Integer into whatever format you need when you're iterating.

Both choices is doable. Choice 2 is much better because it makes

treeset much faster (int comparison vs String comparison),

it uses far less memory, and you don't need to write a custom comparator.

KathyMcDonnella at 2007-7-21 20:19:35 > top of Java-index,Core,Core APIs...
# 17
Thank you very much.Rhea
Rheaa at 2007-7-21 20:19:35 > top of Java-index,Core,Core APIs...