direkt Access in a LinkedHashMap

M

Markus Innerebner

Hello @ all:
I implemented my data structure in a LinkedHashMap. Now I want to access
to the object i and from this position I want to scan sequentially the
hole LinkedHashMap.
I did this with an iterator, but I always start at the beginning of the
LinkedHashMap.

int COPOSCOUNTER = 250;
// _currentCO is the LinkedHashMap sorted by the attribute COPosition
Iterator it2 = _currentCO.values().iterator();
// try to arrive at the desired position

// Objekt is an objekt, which has the attribute COPosition
while (it2.hasNext() && ( (Objekt) it2.next()).getCOPosition() != minCOPOS - 1) {
// go to the next element
}
// arrived at position minCOPOS - 1
//..... sequantially scanning

thanks for helping me

Markus
 
M

Michael Borgwardt

Markus said:
Hello @ all:
I implemented my data structure in a LinkedHashMap. Now I want to access
to the object i and from this position I want to scan sequentially the
hole LinkedHashMap.
I did this with an iterator, but I always start at the beginning of the
LinkedHashMap.

What exactly is it you're expecting the code to do that it does not?
I couldn't determine that from the above question (Maybe you should
try de.comp.lang.java instead).

At first glance, it seems like you might be misunderstanding how Iterator
works: calling next() implicitly advances to the next element, so the
way you use it in your loop condition probably results in skipping every
second element.

BTW, it's a really bad idea to name a class "Objekt".
 
M

Markus Innerebner

Hi Michael

thanks for the answer
What exactly is it you're expecting the code to do that it does not?
I couldn't determine that from the above question (Maybe you should
try de.comp.lang.java instead).

At first glance, it seems like you might be misunderstanding how Iterator
works: calling next() implicitly advances to the next element, so the
way you use it in your loop condition probably results in skipping every
second element.

I understand what iterator are doing.
So if I want to set the iterator at the Element at postion j, I first
have to arrive at this position and this I did with:

while (it2.hasNext() && ( (Objekt) it2.next()).getCOPosition() != minCOPOS - 1) {

// go to the next element
// so I am setting the iterator at the position minCOPOS

}


And now I just want to remove all elements after minCOPOS and store
them in a second LinkedHashMap ( what I am able to do ).
I am asking me, if this is the right and efficient way to set the
iterator at that position?
BTW, it's a really bad idea to name a class "Objekt".

I know, you 're not the first who is saying that.

Markus
 
M

Michael Borgwardt

Markus said:
And now I just want to remove all elements after minCOPOS and store
them in a second LinkedHashMap ( what I am able to do ).
I am asking me, if this is the right and efficient way to set the
iterator at that position?

Ah, so it's just a performance question? Smacks of premature
optimization:
http://magnonel.guild.net/~schwern/talks/How_To_Be_Lazy/full_slides/rules_of_optimization.html

Anyway, if you need the map functionality and cannot decide which
elements to put into the second map until after you've put them
into the first, then I'm pretty sure there's no faster way other
than writing your own LinkedHashmap or modifying Sun's (which
would be a breach of license terms).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top