commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tim O'Brien <tobr...@discursive.com>
Subject Re: [collections] Name that data structure
Date Tue, 05 Jul 2005 09:03:05 GMT
Wendy, you could most certainly use a LRUMap with a fixed size.   Give 
each item a unique key and let the Map take care of uniqueness.  LRUMap 
will take care of discarding the least recently used entry once it 
reached the maximum defined size, and the Iterator returns most recently 
used to least recently used.  This would be the easiest way to do this, 
by far.

Or you could do something that takes more work, but I think it more fun:

Define a Predicate:

import java.util.Collection;
mport org.apache.commons.collections.Predicate;

public class UniqueInCollection implements Predicate {

    private Collection collection;
   
    public UniqueInCollection(Collection collection) {
        this.collection = collection;
    }
   
    public boolean evaluate(Object o) {
        return !collection.contains( o );
    }

}

Then use a CircularFifoBuffer married to the Predicate.  Only downside 
is that you have to catch IllegalArgumentException throw by 
PredicatedBuffer:

import java.util.Iterator;

import org.apache.commons.collections.Buffer;
import org.apache.commons.collections.Predicate;
import org.apache.commons.collections.buffer.CircularFifoBuffer;
import org.apache.commons.collections.buffer.PredicatedBuffer;

public class RecentlyVisited {
   
    public static void main(String[] args) {
       
        Buffer buffer = new CircularFifoBuffer(5);
        Predicate unique = new UniqueInCollection(buffer);
       
        Buffer recentVisited = PredicatedBuffer.decorate(buffer, unique);
       
        add( recentVisited, "Page 1" );
        add( recentVisited, "Page 2" );
        add( recentVisited, "Page 3" );
        add( recentVisited, "Page 4" );
        add( recentVisited, "Page 1" );
        add( recentVisited, "Page 2" );
        add( recentVisited, "Page 1" );
        add( recentVisited, "Page 21" );
        add( recentVisited, "Page 22" );
        add( recentVisited, "Page 1" );
       
        Iterator i = buffer.iterator();
        while( i.hasNext() ) {
            String value = (String) i.next();
            System.out.println( value );
        }
       
    }
   
    public static void add(Buffer recentVisited, String page) {
        try {
            recentVisited.add( page );
        } catch( IllegalArgumentException iae ) {
            // do nothing, buffer will complain if predicate fails.
        }
    }

}


Mattias Jiderhamn wrote:

> Possibly it could also be a MRU (Most Recently Used) cache.
>
> At 2005-07-03 23:39, you wrote:
>
>> I'd say you were looking for an ordinary priority queue, where the
>> priority=the timestamp. Try the Heap class.
>>
>> Sincerely,
>> Silas Snider
>>
>> On 7/3/05, Wendy Smoak <java@wendysmoak.com> wrote:
>> > I'm looking through the Collections API, but not finding exactly 
>> what I
>> > want... hoping someone who's more familiar with it can point me in 
>> the right
>> > direction.
>> >
>> > What I'm trying to do is more or less what you see on catalog sites 
>> where
>> > they'll list the most recent items you've looked at, newest on 
>> top.  So it's
>> > ordered (List), but has no duplicates (Set), and I need to have a  
>> max size.
>> >
>> > ListOrderedSet is almost there, except that it retains the 'old' 
>> position if
>> > you add the same item again.  (And has no max length.)
>> >
>> > So... before I either write it myself or extend ListOrderedSet to 
>> make it do
>> > what I want, does anyone have another suggestion?  And what would 
>> _you_ call
>> > it?
>> >
>> > Thanks,
>> > Wendy Smoak
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
>
>
>




---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message