I tried to do this

ArrayList<String> a=new ArrayList<String>(2);
a.add("dss");
a.add("dsfs");
a.add("fsfs");

Though i have specified a limit of 2 on the collection, i am able to add more. If this is general about Collections, is there any Collection which sets a limit and strictly follows it ?

Dani AI

Generated

already pointed out why your ArrayList kept growing. If the goal is a collection that actually stops accepting new elements once a limit is reached, use a bounded or fixed-size implementation rather than relying on ArrayList capacity.

One simple option when the contents are known up front is a fixed-size list (size cannot change). For example, creating a list from an array will refuse any add/remove operations (it throws UnsupportedOperationException):

List<String> fixed = Arrays.asList("one", "two");
fixed.add("three"); // UnsupportedOperationException

See the Arrays API for details: java.util.Arrays documentation.

For a true runtime-enforced maximum (start empty, allow adds up to N, then refuse or block), use a bounded queue from java.util.concurrent. ArrayBlockingQueue and LinkedBlockingQueue accept a capacity and enforce it. Behavior differs by method: add() throws IllegalStateException if full, offer() returns false, and put() blocks until space is available. Example:

ArrayBlockingQueue<String> q = new ArrayBlockingQueue<>(2);
q.add("a");
q.add("b");
q.offer("c"); // returns false
// q.add("c"); would throw IllegalStateException
// q.put("c"); would block until space frees

See the ArrayBlockingQueue docs: java.util.concurrent.ArrayBlockingQueue.

Note: if you implement a manual check like if (list.size() < max) list.add(x); be careful in multithreaded contexts — that check-then-act pattern is racy. Use the thread-safe bounded queue implementations or synchronize appropriately.

Recommended Answers

All 2 Replies

Check the API of ArrayList. Never assume that classes and methods do what you think they do. The argument you have specified is the initial capacity of the ArrayList, not the size.

Check the constructors of the ArrayList class.

From the API:

Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically. The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost.

An application can increase the capacity of an ArrayList instance before adding a large number of elements using the ensureCapacity operation. This may reduce the amount of incremental reallocation.

Every time you add an element, the size of the list increases. When the size reaches its initial capacity, the list increases its capacity by a default value. Then you keep on adding elements and if you reach again its capacity, it will again increase.

Meaning that if you have the initial capacity to be small (in your case 2) and you add 1000 elements, then you will have many increases which takes time.
If you use a large initial capacity(2000) then the add method calls will be fast. Not many increases would be needed, but you would have wasted a lot of space.

In general for small applications you don't really care, so the default constructor would be enough to use.

Do you need to have the ArrayList have a max size?

thanks a lot!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.