direkt Access in a LinkedHashMap

Discussion in 'Java' started by Markus Innerebner, Jun 28, 2004.

  1. 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
    Markus Innerebner, Jun 28, 2004
    #1
    1. Advertising

  2. Markus Innerebner wrote:

    > 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".
    Michael Borgwardt, Jun 28, 2004
    #2
    1. Advertising

  3. 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
    Markus Innerebner, Jun 28, 2004
    #3
  4. Markus Innerebner wrote:
    > 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).
    Michael Borgwardt, Jun 28, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrej Litowka

    Can't read a value in a LinkedHashMap

    Andrej Litowka, Nov 20, 2003, in forum: Java
    Replies:
    1
    Views:
    2,691
    John C. Bollinger
    Nov 20, 2003
  2. Markus Innerebner
    Replies:
    1
    Views:
    577
    Markus Innerebner
    May 29, 2004
  3. enrique
    Replies:
    8
    Views:
    539
    Kevin McMurtrie
    Apr 29, 2005
  4. newsnet customer

    Getting Keys from a LinkedHashMap

    newsnet customer, Apr 14, 2006, in forum: Java
    Replies:
    1
    Views:
    574
    Patricia Shanahan
    Apr 14, 2006
  5. Albert

    LinkedHashMap next method ?

    Albert, May 18, 2009, in forum: Java
    Replies:
    4
    Views:
    1,436
    Seamus MacRae
    May 20, 2009
Loading...

Share This Page