Hallo auch,
ich bastel mir da grad was zusammen und bin bei der Api der PriorityBlockingQueue auf folgendes gestossen:
ich benutzte das Beispiel 1:1 als innere private klasse.
eclipse sage mir dann aber "static field seq can not be declared static; static fields can only be declared in static or top-level types"
ja soweit klar, füg ich also einfach ein static in der klassendeklaration ein, aber was bitte bringt ein "private static class" denn!? Und warum ist die Api dahingehend fehlerhaft?
ich bastel mir da grad was zusammen und bin bei der Api der PriorityBlockingQueue auf folgendes gestossen:
Operations on this class make no guarantees about the ordering of elements with equal priority. If you need to enforce an ordering, you can define custom classes or comparators that use a secondary key to break ties in primary priority values. For example, here is a class that applies first-in-first-out tie-breaking to comparable elements. To use it, you would insert a new FIFOEntry(anEntry) instead of a plain entry object.
Java:
class FIFOEntry<E extends Comparable<? super E>> implements Comparable<FIFOEntry<E>> {
final static AtomicLong seq = new AtomicLong();
final long seqNum;
final E entry;
public FIFOEntry(E entry) {
seqNum = seq.getAndIncrement();
this.entry = entry;
}
public E getEntry() {
return entry;
}
public int compareTo(FIFOEntry<E> other) {
int res = entry.compareTo(other.entry);
if (res == 0 && other.entry != this.entry)
res = (seqNum < other.seqNum ? -1 : 1);
return res;
}
}
ich benutzte das Beispiel 1:1 als innere private klasse.
eclipse sage mir dann aber "static field seq can not be declared static; static fields can only be declared in static or top-level types"
ja soweit klar, füg ich also einfach ein static in der klassendeklaration ein, aber was bitte bringt ein "private static class" denn!? Und warum ist die Api dahingehend fehlerhaft?
Zuletzt bearbeitet: