public boolean add ( E e ) { // not yet contained? if ( set . contains ( e ) ) { return false ; } // add new element queue . add ( e ) ; set . add ( e ) ; // maintain size limit while ( queue . size ( ) > sizeLimit ) { remove ( ) ; } return true ; }
Add an element at the tail of the queue replacing the least recently added item if the queue is full . The item is only added to the queue if it is not already contained . Runs in constant time assuming the hash function disperses the elements properly among the buckets of the underlying hash set .