-2

I have an extra ordinary requirement of having a Java object array list (or list initialized as an ArrayList) which will be continuously updated and is supposed to hold only the latest 5 items with the objects to be sorted in a descending order of they were added.

How could this be accomplished? I need to come up with a solution within Java SE 7 preferably using no 3rd party library.

NOTE For those who mark this question as a duplicate, you do not seem to understand the requirement of using ArrayList for this case and that Queues and ArrayLists are different object types as well as Java and C# are different languages. Do you think Stackoverflow's internal search engine is not as good as you are in locating the duplicate questions?

When marking a question as "duplicate", make sure you link a true duplicate, please.

fledglingCoder
  • 402
  • 9
  • 24
  • 1
    you may use linked list instead of arraylist to achieve O(1) time complexity for adding/removing elements – user3437460 Oct 22 '14 at 11:03
  • Every time you add, if the size is greater than 5, remove the first element. – sp00m Oct 22 '14 at 11:04
  • 1
    What's your idea about it? Where is the difficulty? – JB Nizet Oct 22 '14 at 11:04
  • Please explain in detail, why it has to be an arraylist. For this usecase, arraylist sounds inappropriate. A Queue seems to be a perfect fit, maybe a [LinkedBlockingQueue](http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/LinkedBlockingQueue.html#LinkedBlockingQueue%28int%29). – SME_Dev Oct 22 '14 at 11:09
  • You need a fixed-size [fifo](http://en.wikipedia.org/wiki/FIFO). The easiest way (but yet a 3rd party lib) [Apache Commons Collection CircularFifoQueue](https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/queue/CircularFifoQueue.html). – Stefan Oct 22 '14 at 11:12
  • @Stefan his requirement says 'no 3rd party library' – SME_Dev Oct 22 '14 at 11:13
  • 1
    This can be easily done with 10,000 ways without using 3rd party lib. But arrayList seemed to be one of the worst choice to implement this. – user3437460 Oct 22 '14 at 11:16
  • Yes, therefore he has to read the wikipedia article and write his own. – Stefan Oct 22 '14 at 11:17
  • Thanks to who all took their time to comment. I'll see if the ArrayList and non-3rd-party-lib-usage restriction can be relaxed . – fledglingCoder Oct 22 '14 at 11:48
  • @SME_Dev Solution's going to be used in a XOM in iLog JRules for z/OS whose collections support might be limited. Pending COBOL developers' approval / validation of the BOM with copybook from this XOM. – fledglingCoder Oct 28 '14 at 13:55
  • Congratulations for those who label this Java-related question as duplicate by linking to a C# question. – fledglingCoder Oct 28 '14 at 14:04

1 Answers1

0

Not sure about the descending order, but this shows one way of fixing the size

class BoundedArrayList<E> extends ArrayList<E> {
    private int fixedCapacity;

    public BoundedArrayList(int fixedCapacity) {
        super(fixedCapacity);
        this.fixedCapacity = fixedCapacity;
    }

    @Override
    public boolean add(E e) {
        // is it about to cross limit ? removing it in advance
        if(this.size() > fixedCapacity - 1) {
            E element = this.remove(fixedCapacity - 1);
            System.out.println("Removed due to overflow : " + element);
        }
        this.add(0, e);
        return true;
    }

    public static void main(String[] args) {
        BoundedArrayList<Integer> list = new BoundedArrayList<Integer>(5);
        for(int i =0 ; i < 10; i++) {
            list.add(i);
            System.out.println(list);
        }
    }
}
Thamme Gowda
  • 11,249
  • 5
  • 50
  • 57