4

Is there a way to give size limit to TreeSet in Java Collections as we do for arrays? for example in arrays we do,

anArray = new int[10];
FirmView
  • 3,130
  • 8
  • 34
  • 50
  • what do you mean a "size limit" Can you give us partial code? – Henry Harris Jul 25 '12 at 21:27
  • No - you might want to check this [FixedSizeSortedSet](http://www.java2s.com/Code/Java/Collections-Data-Structure/FixedSizeSortedSet.htm) - no guarantee that it is bug free... Actually it does not override addAll so might not work as expected... – assylias Jul 25 '12 at 21:31

6 Answers6

5

An array has a fixed length that must be specified when you create it.

A TreeSet automatically grows as you add elements to it. You cannot set its size. You can only read it.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
3

This threat can help you fixed size list in Java

Also, you can implement your own collection in order to add elements if your limit has not been reached

Community
  • 1
  • 1
manix
  • 14,537
  • 11
  • 70
  • 107
2

None of TreeSet's constructors specifies an initial size, it grows when elements are added. And there's no way to limit the maximum size of the data structure. Every time you add() a new element, you'd need to manually check if it has exceeded the maximum size allowed. You can specify this behavior by implementing a subclass that extends from TreeSet and overriding add(), addAll(), and the two constructors that receive a Collection as a parameter.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • as shown by the answer that tries to do that, it is not necessarily an easy thing. Typically, if you set has 9 items and you addAll a collection of 2, you need to check for duplicates before you decide if you addAll or not, in which case you still need to add one etc. – assylias Jul 25 '12 at 21:39
  • @assylias that's right, there _are_ implementation details to take care of, but the general idea holds: subclass TreeSet and program the fixed size limit - if it's in the OP's interest, he'll have to decide how to handle the edge cases – Óscar López Jul 25 '12 at 21:45
2

You can always make your own implementation. Here is an example to get you started; you may find that you wish to tweak it accordingly:

public class BoundedTreeSet<E> extends TreeSet<E> {

    private final int limit;

    public BoundedTreeSet(final int limit) {
        super();
        this.limit = limit;
    }

    public BoundedTreeSet(final int limit, final Collection<? extends E> c) {
        super(c);
        this.limit = limit;
    }

    public BoundedTreeSet(final int limit, final Comparator<? super E> comparator) {
        super(comparator);
        this.limit = limit;
    }

    public BoundedTreeSet(final int limit, final SortedSet<E> s) {
        super(s);
        this.limit = limit;
    }

    @Override
    public boolean add(final E e) {
        if (size() >= limit) {
            return false;
        }

        return super.add(e);
    }

    @Override
    public boolean addAll(Collection<? extends E> c) {
        if (size() + c.size() >= limit) {
            return false;
        }

        return super.addAll(c);
    }
}
Jon Newmuis
  • 25,722
  • 2
  • 45
  • 57
  • your addAll has a bug (if size() == N-1 and c.size() == 2 for example). Bottom line: not that easy to make it bug free + in that case, if c contains an item already in the list, you can actually addAll(c)... – assylias Jul 25 '12 at 21:36
  • Fixed. No one said it was easy to make it bug-free ;) The set of things that are easy to keep bug-free is quite small... – Jon Newmuis Jul 25 '12 at 21:37
  • At first sight yes. Your addAll is still buggy. – assylias Jul 25 '12 at 21:37
  • 1
    Also you would presumably spend more than 5 minutes writing something like this if you were to use it for real. This is for instruction's sake, to provide an example as to how you can approach implementing such a solution. As I noted in the answer, some tweaking may be necessary. – Jon Newmuis Jul 25 '12 at 21:38
  • I understand but I'm trying to make the point that it is more complicated than it looks. – assylias Jul 25 '12 at 21:41
  • 1
    I don't think that just because the solution is "difficult" it should be avoided. In the scope of things that one has to program to create a working application, I wouldn't say that subclassing `TreeSet` is one of the hardest... Yes, bugs will happen. But you spend time developing and you test, as with anything else. – Jon Newmuis Jul 25 '12 at 21:46
2

Here is an implementation of BoundedTreeSet in Apache Solr, which keeps the biggest values when trying to insert into a "full" set :

http://lucene.apache.org/solr/4_6_0/solr-core/org/apache/solr/util/BoundedTreeSet.html

Maven artifact available here :

<dependency>
   <groupId>org.apache.solr</groupId>
   <artifactId>solr-core</artifactId>
   <version>4.6.0</version>
</dependency>
Remi Mélisson
  • 2,724
  • 2
  • 21
  • 13
0

The closest you can come to an existing collection with capacity limits is a BlockingQueue. When adding items to the queue, you can specify a zero second (or very small) blocking timeout, so that an exception is thrown when the capacity is exceeded. For more details, see BlockingQueue.offer()

Dmitry B.
  • 9,107
  • 3
  • 43
  • 64