Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4621570
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:43:13+00:00 2026-05-22T02:43:13+00:00

It’s useful to me to have a data structure in Java that has all

  • 0

It’s useful to me to have a data structure in Java that has all the functionality of a List, but has a maximum storage capacity, and drops older data when newer data is added. Conceivably at some point I might want to implement a fixed size Queue which keeps a more general ordering of the data, and drops the old data lowest in that ordering, but that’s the for the future.

At the moment I’m implementing it like this:

public class FixedSizeList<T> {

  private final int maxSize;
  private final LinkedList<T> list = new LinkedList<T>();

  public FixedSizeQueue(int maxSize) {
    this.maxSize = maxSize < 0 ? 0 : maxSize;
  }

  public T add(T t) {
    list.add(t);
    return list.size() > maxSize ? list.remove() : null;
  }

  // add remaining methods...

}

Is there either (a) an existing data structure that serves my needs, or (b) a better way of implementing this data structure?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T02:43:14+00:00Added an answer on May 22, 2026 at 2:43 am

    Here’s a List with a size limit, based on Guava‘s ForwardingList:

    A list which forwards all its method
    calls to another list. Subclasses
    should override one or more methods to
    modify the behavior of the backing
    list as desired per the decorator
    pattern.

    Guava has base classes like this for all JDK-5 Collection types. Each of them fulfills the same purpose: making it easy to add value, while delegating all default functionality to the underlying collection.

    public class LimitingList<E> extends ForwardingList<E> {
    
        private final class LimitingListIterator extends ForwardingListIterator<E> {
    
            private final ListIterator<E> innerListIterator;
    
            private LimitingListIterator(final ListIterator<E> innerListIterator) {
                this.innerListIterator = innerListIterator;
            }
    
            /**
             * {@inheritDoc}
             */
            @Override
            public void add(final E element) {
                if (inner.size() < maxSize)
                    innerListIterator.add(element);
                else
                    throw new IndexOutOfBoundsException();
            }
    
            @Override
            protected ListIterator<E> delegate() {
                return innerListIterator;
            }
        }
    
        public LimitingList(final int maxSize) {
            this(new ArrayList<E>(), maxSize);
        }
    
        public LimitingList(final List<E> inner, final int maxSize) {
            super();
            this.inner = inner;
            this.maxSize = maxSize;
        }
    
        @Override
        public boolean addAll(final Collection<? extends E> collection) {
            boolean changed = false;
            for (final E item : collection) {
                final boolean tmpChanged = add(item);
                changed = changed || tmpChanged;
                if (!tmpChanged)
                    break;
            }
            return changed;
        }
    
        @Override
        public boolean add(final E e) {
            if (inner.size() < maxSize)
                return super.add(e);
            else
                return false;
        }
    
        @Override
        public ListIterator<E> listIterator() {
            return new LimitingListIterator(inner.listIterator());
        }
    
        @Override
        public void add(final int index, final E element) {
            throw new UnsupportedOperationException();
        }
    
        @Override
        public boolean addAll(final int index, final Collection<? extends E> elements) {
            throw new UnsupportedOperationException();
        }
    
        @Override
        public ListIterator<E> listIterator(final int index) {
            return new LimitingListIterator(inner.listIterator(index));
        }
    
        private final int maxSize;
        private final List<E> inner;
    
        @Override
        protected List<E> delegate() {
            return inner;
        }
    
    }
    

    It delegates all real functionality to an underlying list, which is an ArrayList per default (single argument constructor), but you can also supply (two argument constructor)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have some data like this: 1 2 3 4 5 9 2 6
I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.