commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Wendy Smoak <>
Subject Re: [collections] Name that data structure
Date Tue, 12 Jul 2005 18:22:44 GMT

I'm using your suggestion, and thanks for providing a complete example!  I
needed that. :)
(It's here if anyone needs to refer to it: )

As written, however, it doesn't rearrange the values when you re-visit an
item.  In your example, after '1' gets added for the second time, the list
should be 2,3,4,1 and instead it is still 1,2,3,4.  (And that means '1' will
be evicted too soon.)

To fix that, I tried to simply remove any item before I add it, so it will
always go to the end of the list as a new item:

    public static void add(Buffer recentVisited, String page) {
        recentVisited.remove( page );
        try {
            recentVisited.add( page );
        } catch( IllegalArgumentException iae ) {
            // do nothing, buffer will complain if predicate fails.
        System.out.println( recentVisited );

And was surprised to get :
     [java] [Page 1]
     [java] [Page 1, Page 2]
     [java] [Page 1, Page 2, Page 3]
     [java] [Page 1, Page 2, Page 3, Page 4]
     [java] [Page 2, Page 3, Page 4, Page 1]  <--- it's working!
     [java] [Page 3, Page 4, Page 1, Page 2]
     [java] java.lang.ArrayIndexOutOfBoundsException: -1
     [java] Caused by: java.lang.ArrayIndexOutOfBoundsException: -1
     [java]     at
     [java]     at
     [java]     at
     [java]     at RecentlyVisited.add(
     [java]     at RecentlyVisited.main(

It's coming from the third instance of
   add( recentVisited, "Page 1" );
in the example code.

I put together a simple example that demonstrates the problem:

Bug?  Surely I'm not doing anything wrong by calling remove(...) on a
Collection?  (Inefficient though it may be, first I just want to see it

Meanwhile, is there another way I can get the buffer to be in the correct
LRU order?  The LRUMap did work, but it's ugly having to put  the empty
String into the Map, when I don't need key/value pairs.

Wendy Smoak

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message