java.util.Comparator.compare()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(8.5k)|赞(0)|评价(0)|浏览(219)

本文整理了Java中java.util.Comparator.compare()方法的一些代码示例,展示了Comparator.compare()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Comparator.compare()方法的具体详情如下:
包路径:java.util.Comparator
类名称:Comparator
方法名:compare

Comparator.compare介绍

[英]Compares the two specified objects to determine their relative ordering. The ordering implied by the return value of this method for all possible pairs of (lhs, rhs) should form an equivalence relation. This means that

  • compare(a,a) returns zero for all a
  • the sign of compare(a,b) must be the opposite of the sign of compare(b,a) for all pairs of (a,b)
  • From compare(a,b) > 0 and compare(b,c) > 0 it must follow compare(a,c) > 0 for all possible combinations of (a,b,c)
    [中]比较两个指定对象以确定它们的相对顺序。此方法的返回值对所有可能的(lhs,rhs)对暗示的顺序应形成等价关系。这意味着
    *比较(a,a)对所有a返回零
    *对于(a,b)的所有对,比较符号(a,b)必须与比较符号(b,a)相反
    *从比较(a,b)>0和比较(b,c)>0中,对于(a,b,c)的所有可能组合,必须遵循比较(a,c)>0

代码示例

代码示例来源:origin: google/guava

int findNextJ() {
 for (int k = nextPermutation.size() - 2; k >= 0; k--) {
  if (comparator.compare(nextPermutation.get(k), nextPermutation.get(k + 1)) < 0) {
   return k;
  }
 }
 return -1;
}

代码示例来源:origin: google/guava

@Override
 <E> int resultIndex(
   Comparator<? super E> comparator, E key, List<? extends E> list, int foundIndex) {
  // Of course, we have to use binary search to find the precise
  // breakpoint...
  int lower = 0;
  int upper = foundIndex;
  // Of course, we have to use binary search to find the precise breakpoint...
  // Everything between lower and upper inclusive compares at <= 0.
  while (lower < upper) {
   int middle = (lower + upper) >>> 1;
   int c = comparator.compare(list.get(middle), key);
   if (c < 0) {
    lower = middle + 1;
   } else { // c == 0
    upper = middle;
   }
  }
  return lower;
 }
},

代码示例来源:origin: square/okhttp

/**
 * Returns an array containing only elements found in {@code first} and also in {@code
 * second}. The returned elements are in the same order as in {@code first}.
 */
public static String[] intersect(
  Comparator<? super String> comparator, String[] first, String[] second) {
 List<String> result = new ArrayList<>();
 for (String a : first) {
  for (String b : second) {
   if (comparator.compare(a, b) == 0) {
    result.add(a);
    break;
   }
  }
 }
 return result.toArray(new String[result.size()]);
}

代码示例来源:origin: google/guava

@Override
public int compare(Iterable<T> leftIterable, Iterable<T> rightIterable) {
 Iterator<T> left = leftIterable.iterator();
 Iterator<T> right = rightIterable.iterator();
 while (left.hasNext()) {
  if (!right.hasNext()) {
   return LEFT_IS_GREATER; // because it's longer
  }
  int result = elementOrder.compare(left.next(), right.next());
  if (result != 0) {
   return result;
  }
 }
 if (right.hasNext()) {
  return RIGHT_IS_GREATER; // because it's longer
 }
 return 0;
}

代码示例来源:origin: ReactiveX/RxJava

@Override
public List<T> apply(List<T> a, List<T> b) throws Exception {
  int n = a.size() + b.size();
  if (n == 0) {
    return new ArrayList<T>();
  Iterator<T> bt = b.iterator();
  T s1 = at.hasNext() ? at.next() : null;
  T s2 = bt.hasNext() ? bt.next() : null;
    if (comparator.compare(s1, s2) < 0) { // s1 comes before s2
      both.add(s1);
      s1 = at.hasNext() ? at.next() : null;
    } else {
      both.add(s2);

代码示例来源:origin: apache/incubator-druid

VersionedIntervalTimeline<String, DataSegment> timeline,
Period skipOffset,
@Nullable List<Interval> skipIntervals
  .map(PartitionChunk::getObject)
  .filter(segment -> lookupInterval.contains(segment.getInterval()))
  .sorted((s1, s2) -> Comparators.intervalsByStartThenEnd().compare(s1.getInterval(), s2.getInterval()))
  .collect(Collectors.toList());
 searchIntervals.add(
   new Interval(
     segments.get(0).getInterval().getStart(),
     segments.get(segments.size() - 1).getInterval().getEnd()

代码示例来源:origin: org.assertj/assertj-core

private boolean compareElementsOf(Iterable<T> actual, Iterable<T> other) {
 if (sizeOf(actual) != sizeOf(other)) return false;
 // compare their elements with elementComparator
 Iterator<T> iterator = other.iterator();
 for (T actualElement : actual) {
  T otherElement = iterator.next();
  if (elementComparator.compare(actualElement, otherElement) != 0) return false;
 }
 return true;
}

代码示例来源:origin: apache/incubator-druid

@Override
public int compareUnsortedEncodedKeyComponents(@Nullable Long lhs, @Nullable Long rhs)
{
 return LONG_COMPARATOR.compare(lhs, rhs);
}

代码示例来源:origin: google/guava

int findNextL(int j) {
  E ak = nextPermutation.get(j);
  for (int l = nextPermutation.size() - 1; l > j; l--) {
   if (comparator.compare(ak, nextPermutation.get(l)) < 0) {
    return l;
   }
  }
  throw new AssertionError("this statement should be unreachable");
 }
}

代码示例来源:origin: google/guava

/**
 * Returns {@code true} if each element in {@code iterable} after the first is greater than or
 * equal to the element that preceded it, according to the specified comparator. Note that this is
 * always true when the iterable has fewer than two elements.
 */
public static <T> boolean isInOrder(Iterable<? extends T> iterable, Comparator<T> comparator) {
 checkNotNull(comparator);
 Iterator<? extends T> it = iterable.iterator();
 if (it.hasNext()) {
  T prev = it.next();
  while (it.hasNext()) {
   T next = it.next();
   if (comparator.compare(prev, next) > 0) {
    return false;
   }
   prev = next;
  }
 }
 return true;
}

代码示例来源:origin: thinkaurelius/titan

public static<E> List<E> mergeSort(Collection<E> a, Collection<E> b, Comparator<E> comp) {
  Iterator<E> itera = a.iterator(), iterb = b.iterator();
  E heada = itera.hasNext()?itera.next():null;
  E headb = iterb.hasNext()?iterb.next():null;
  List<E> result = new ArrayList(a.size()+b.size());
  while (heada!=null || headb!=null) {
    E next;
    if (heada==null) {
      next=headb;
      headb = null;
    } else if (headb==null) {
      next=heada;
      heada=null;
    } else if (comp.compare(heada,headb)<=0) {
      next=heada;
      heada=null;
    } else {
      next=headb;
      headb=null;
    }
    assert next!=null;
    Preconditions.checkArgument(result.isEmpty() || comp.compare(result.get(result.size()-1),next)<=0,
        "The input collections are not sorted");
    result.add(next);
    if (heada==null) heada=itera.hasNext()?itera.next():null;
    if (headb==null) headb=iterb.hasNext()?iterb.next():null;
  }
  return result;
}

代码示例来源:origin: redisson/redisson

@Override
@SuppressWarnings( {"unchecked"})
protected int compare(int index, T element) {
  return comparator.compare(list.get(index), element);
}

代码示例来源:origin: prestodb/presto

/**
 * Returns an array containing only elements found in {@code first} and also in {@code
 * second}. The returned elements are in the same order as in {@code first}.
 */
@SuppressWarnings("unchecked")
public static String[] intersect(
  Comparator<? super String> comparator, String[] first, String[] second) {
 List<String> result = new ArrayList<>();
 for (String a : first) {
  for (String b : second) {
   if (comparator.compare(a, b) == 0) {
    result.add(a);
    break;
   }
  }
 }
 return result.toArray(new String[result.size()]);
}

代码示例来源:origin: joel-costigliola/assertj-core

private boolean compareElementsOf(Iterable<T> actual, Iterable<T> other) {
 if (sizeOf(actual) != sizeOf(other)) return false;
 // compare their elements with elementComparator
 Iterator<T> iterator = other.iterator();
 for (T actualElement : actual) {
  T otherElement = iterator.next();
  if (elementComparator.compare(actualElement, otherElement) != 0) return false;
 }
 return true;
}

代码示例来源:origin: apache/incubator-druid

@Override
public int compareUnsortedEncodedKeyComponents(@Nullable Float lhs, @Nullable Float rhs)
{
 return FLOAT_COMPARATOR.compare(lhs, rhs);
}

代码示例来源:origin: google/guava

@Override
 <E> int resultIndex(
   Comparator<? super E> comparator, E key, List<? extends E> list, int foundIndex) {
  // Of course, we have to use binary search to find the precise
  // breakpoint...
  int lower = foundIndex;
  int upper = list.size() - 1;
  // Everything between lower and upper inclusive compares at >= 0.
  while (lower < upper) {
   int middle = (lower + upper + 1) >>> 1;
   int c = comparator.compare(list.get(middle), key);
   if (c > 0) {
    upper = middle - 1;
   } else { // c == 0
    lower = middle;
   }
  }
  return lower;
 }
},

代码示例来源:origin: google/guava

@Override
 protected C computeNext() {
  while (merged.hasNext()) {
   C next = merged.next();
   boolean duplicate = lastValue != null && comparator.compare(next, lastValue) == 0;
   // Keep looping till we find a non-duplicate value.
   if (!duplicate) {
    lastValue = next;
    return lastValue;
   }
  }
  lastValue = null; // clear reference to unused data
  return endOfData();
 }
};

代码示例来源:origin: redisson/redisson

@Override
public List<T> apply(List<T> a, List<T> b) throws Exception {
  int n = a.size() + b.size();
  if (n == 0) {
    return new ArrayList<T>();
  Iterator<T> bt = b.iterator();
  T s1 = at.hasNext() ? at.next() : null;
  T s2 = bt.hasNext() ? bt.next() : null;
    if (comparator.compare(s1, s2) < 0) { // s1 comes before s2
      both.add(s1);
      s1 = at.hasNext() ? at.next() : null;
    } else {
      both.add(s2);

代码示例来源:origin: oblac/jodd

@Override
@SuppressWarnings( {"unchecked"})
protected int compare(final int index, final T element) {
  return comparator.compare(list.get(index), element);
}

代码示例来源:origin: MovingBlocks/Terasology

private T dequeue() {
  if (elements.size() == 0) {
    return null;
  }
  T smallest = elements.remove(0);
  ListIterator<T> iterator = elements.listIterator();
  while (iterator.hasNext()) {
    T next = iterator.next();
    if (comparator.compare(smallest, next) > 0) {
      iterator.set(smallest);
      smallest = next;
    }
  }
  return smallest;
}

相关文章