I've tried implementing priority queue with fixed size in Java using TreeSet
and overriding add()
method:
public class FixedSizePriorityQueue<E> extends TreeSet<E> {
private final int capacity;
public FixedSizePriorityQueue(final int capacity) {
this.capacity = capacity;
}
public FixedSizePriorityQueue(
final int capacity,
final Comparator<? super E> comparator) {
super(comparator);
this.capacity = capacity;
}
@Override
public boolean add(final E e) {
// initialized with 0 or less than zero capacity
if (capacity <= 0) {
return false;
}
// keep adding until we fill the queue
if (size() < capacity) {
return super.add(e);
}
if (comparator() != null
&& comparator().compare(this.last(), e) < 0) {
pollLast();
return super.add(e);
}
return false;
}
}
I would really appreciate your thoughts, hints and comments.
2 Answers 2
Two thoughts:
If there is other code and you need all the features of a TreeSet great, otherwise delegate to a TreeSet member variable.
Your code
if (capacity <= 0)
is somewhat superfluous because of the next testif (size() < capacity)
-
\$\begingroup\$ thanks for noticing
if (capacity <= 0)
.size()
will always return 0 or greater than 0 :) \$\endgroup\$robosoul– robosoul2013年08月30日 10:25:58 +00:00Commented Aug 30, 2013 at 10:25
I rarely check the return of add methods. I would suggest throwing an exception to indicate the container is full. Add methods that return available space and full state.
-
6\$\begingroup\$ I didn't downvote on this answer, but this is not what I would consider a "proper" review. Look at some other answers on this site, get a feel for what they should look like, and then come back with that knowledge and integrate it into your answer. \$\endgroup\$syb0rg– syb0rg2014年02月19日 00:56:43 +00:00Commented Feb 19, 2014 at 0:56
-
\$\begingroup\$ @syb0rg How is this not a proper review? (This answer was recently flagged, that's why I'm asking) \$\endgroup\$Simon Forsberg– Simon Forsberg2017年07月16日 08:42:59 +00:00Commented Jul 16, 2017 at 8:42
-
\$\begingroup\$ @SimonForsberg It seemed better served as a comment, and I still believe it would be. There isn't enough content in my opinion. Why doesn't the OP check the return of add methods, there is no reason given nor a link to point me to evidence? Would one throw a custom exception, or a standard library exception, and why? Where is the benefit in adding functions that return available space and full state, and how would that be used to refine the code in the question? Now for you and I, these questions are well known. But I think newcomers need more explanation than what was supplied here. \$\endgroup\$syb0rg– syb0rg2017年07月16日 17:50:22 +00:00Commented Jul 16, 2017 at 17:50
-
\$\begingroup\$ @syb0rg That arguably makes it a bad answer, not "not an answer". Short answers are fine. \$\endgroup\$Simon Forsberg– Simon Forsberg2017年07月17日 06:52:47 +00:00Commented Jul 17, 2017 at 6:52
FixedSizePriorityQueue
be aTreeSet
? Do you want to support all methods of aTreeSet
? I would internally use aTreeSet
but not extend it. \$\endgroup\$pollLast()
method inadd
method? \$\endgroup\$pollLast()
, I remove the most irrelevant element and add new one, usingsuper.add()
, leaving super to prioritize. \$\endgroup\$