Source Code : Fixed Size Sorted Set
Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com.
Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com.
Click Here or search from google with Libraries Name you get jar file related it
Fixed Size Sorted Set
//package org.decomposer.util;
import java.util.Comparator;
import java.util.TreeSet;
public class FixedSizeSortedSet<E> extends TreeSet<E>
{
private static final long serialVersionUID = 1L;
private final Comparator<? super E> _comparator;
private final int _maxSize;
public FixedSizeSortedSet(int maxSize)
{
this(null, maxSize);
}
public FixedSizeSortedSet(Comparator<? super E> comparator, int maxSize)
{
super(comparator);
_comparator = comparator;
_maxSize = maxSize;
}
@Override
public boolean add(E e)
{
if(size() >= _maxSize)
{
E smallest = last();
int comparison;
if(_comparator == null) comparison = ((Comparable<E>)e).compareTo(smallest);
else comparison = _comparator.compare(e, smallest);
if(comparison > 0)
{
remove(smallest);
return super.add(e);
}
return false;
}
else
{
return super.add(e);
}
}
}
Thank with us