Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>With the standard API you'd have to do it yourself, i.e. extend one of the sorted set classes and add the logic you want to the <code>add()</code> and <code>addAll()</code> methods. Shouldn't be too hard.</p> <p>Btw, I don't fully understand your example:</p> <pre><code>t1.add(9); // [1,2,3] </code></pre> <p>Shouldn't the set contain <code>[1,2,9]</code> afterwards?</p> <p><em>Edit</em>: I think now I understand: you want to only keep the smallest 3 elements that were added to the set, right?</p> <p><em>Edit 2</em>: An example implementation (not optimised) could look like this:</p> <pre><code>class LimitedSortedSet&lt;E&gt; extends TreeSet&lt;E&gt; { private int maxSize; LimitedSortedSet( int maxSize ) { this.maxSize = maxSize; } @Override public boolean addAll( Collection&lt;? extends E&gt; c ) { boolean added = super.addAll( c ); if( size() &gt; maxSize ) { E firstToRemove = (E)toArray( )[maxSize]; removeAll( tailSet( firstToRemove ) ); } return added; } @Override public boolean add( E o ) { boolean added = super.add( o ); if( size() &gt; maxSize ) { E firstToRemove = (E)toArray( )[maxSize]; removeAll( tailSet( firstToRemove ) ); } return added; } } </code></pre> <p><strike> Note that <code>tailSet()</code> returns the subset including the parameter (if in the set). This means that if you can't calculate the next higher value (doesn't need to be in the set) you'll have to readd that element. This is done in the code above.</strike></p> <p><strike> If you can calculate the next value, e.g. if you have a set of integers, doing something <code>tailSet( lastElement + 1 )</code> would be sufficient and you'd not have to readd the last element. </strike></p> <p><strike> Alternatively you can iterate over the set yourself and remove all elements that follow the last you want to keep. </strike></p> <p><strike> Another alternative, although that might be more work, would be to check the size before inserting an element and remove accordingly. </strike></p> <p><em>Update</em>: as msandiford correctly pointed out, the first element that should be removed is the one at index <code>maxSize</code>. Thus there's no need to readd (re-add?) the last wanted element. </p> <p><strong>Important note:</strong> As @DieterDP correctly pointed out, the implementation above violates the <code>Collection#add()</code> api contract which states that if a collection refuses to add an element for any reason other than it being a duplicate an excpetion <em>must</em> be thrown.</p> <p>In the example above the element is first added but might be removed again due to size constraints or other elements might be removed, so this violates the contract.</p> <p>To fix that you might want to change <code>add()</code> and <code>addAll()</code> to throw exceptions in those cases (or maybe in any case in order to make them unusable) and provide alterante methods to add elements which don't violate any existing api contract.</p> <p>In any case the above example <em>should be used with care</em> since using it with code that isn't aware of the violations might result in unwanted and hard to debug errors.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload