> 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)
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
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
> 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
> 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.
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.
> 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.
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.