[英]How can I sort a LinkedList based on the last names without using an sort methods such as Collections.sort
[英]How can I cancel Collections.sort()
关于这个问题 ,它正在讨论监视Collections.sort()
的可能性,我想知道是否存在一种使用ProgressMonitor
取消对Collections.sort()
的调用的好方法。
监视进度的一种方法是使用自定义的Comparator来跟踪其调用。
沿着这条线,让此Comparator检查cancelled
标志并立即返回0
而不进行任何实际比较有意义吗? 这样可以节省进行手工比较所需的时间,但不会停止列表元素上的迭代。
public class ProgressMonitoringComparator<T extends Comparable<T>> implements Comparator<T> {
private volatile long invocationCount = 0;
private volatile boolean cancelled = false;
private final long elementCount;
private final ProgressMonitor monitor;
public ProgressMonitoringComparator(long elementCount) {
this.elementCount = elementCount;
this.monitor = null;
}
public ProgressMonitoringComparator(ProgressMonitor monitor) {
this.monitor = monitor;
this.elementCount = -1;
}
public ProgressMonitoringComparator() {
this(0);
}
public boolean isCancelled() {
return cancelled;
}
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
public float getProgress() {
if(elementCount <= 0) {
return -1;
}
long totalInvocationsNeeded = (long)(elementCount * Math.log(elementCount));
return totalInvocationsNeeded / invocationCount;
}
@Override
public int compare(T o1, T o2) {
if(cancelled || (monitor != null && monitor.isCancelled())) {
return 0;
}
invocationCount++;
if(monitor != null) {
monitor.worked();
}
return o1.compareTo(o2);
}
}
如user2717954所建议的,第二种方法可能是:
public class CancellableComparator<T extends Comparable<T>> implements Comparator<T> {
private volatile boolean cancelled = false;
public boolean isCancelled() {
return cancelled;
}
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
@Override
public int compare(T o1, T o2) {
if(cancelled) {
throw new CancelException();
}
return o1.compareTo(o2);
}
}
从评论和类似问题中,我得出以下结论:
public class ProgressMonitoringComparator<T extends Comparable<T>> implements Comparator<T> {
private static final boolean DEFAULT_SOFT = false;
private static final int DEFAULT_ELEMENT_CNT = -1;
private volatile long invocationCount = 0;
private volatile boolean cancelled = false;
private final long elementCount;
private final ProgressMonitor monitor;
private final Comparator<T> delegate;
private final boolean soft;
public boolean isCancelled() {
return cancelled;
}
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
public double getProgress() {
if(elementCount <= 0) {
return -1;
}
long totalInvocationsNeeded = (long)(elementCount * Math.log(elementCount));
return totalInvocationsNeeded / (double)invocationCount;
}
public ProgressMonitoringComparator(ProgressMonitor monitor, Comparator<T> delegate, boolean soft) {
super();
this.elementCount = DEFAULT_ELEMENT_CNT;
this.monitor = monitor;
this.delegate = delegate;
this.soft = soft;
}
public ProgressMonitoringComparator(ProgressMonitor monitor, Comparator<T> delegate) {
this(monitor, delegate, DEFAULT_SOFT);
}
public ProgressMonitoringComparator(ProgressMonitor monitor, boolean soft) {
this(monitor, null, soft);
}
public ProgressMonitoringComparator(ProgressMonitor monitor) {
this(monitor, DEFAULT_SOFT);
}
public ProgressMonitoringComparator(long elementCount, Comparator<T> delegate, boolean soft) {
super();
this.elementCount = elementCount;
this.monitor = null;
this.delegate = delegate;
this.soft = soft;
}
public ProgressMonitoringComparator(long elementCount, boolean soft) {
this(elementCount, null, soft);
}
public ProgressMonitoringComparator(long elementCount) {
this(elementCount, DEFAULT_SOFT);
}
@Override
public int compare(T o1, T o2) {
if(cancelled || (monitor != null && monitor.isCancelled())) {
if(soft) {
return 0;
}
throw new CancelException();
}
invocationCount++;
if(monitor != null) {
monitor.worked();
}
if(delegate != null) {
return delegate.compare(o1, o2);
}
return o1.compareTo(o2);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.