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

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

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

Comparator.equals介绍

[英]Compares this Comparator with the specified Object and indicates whether they are equal. In order to be equal, object must represent the same object as this instance using a class-specific comparison.

A Comparator never needs to override this method, but may choose so for performance reasons.
[中]将此比较器与指定对象进行比较,并指示它们是否相等。为了相等,对象必须使用特定于类的比较来表示与此实例相同的对象。
比较器从不需要重写此方法,但出于性能原因可能会选择重写。

代码示例

代码示例来源:origin: spring-projects/spring-framework

@Override
@SuppressWarnings("unchecked")
public boolean equals(Object other) {
  if (this == other) {
    return true;
  }
  if (!(other instanceof InvertibleComparator)) {
    return false;
  }
  InvertibleComparator<T> otherComp = (InvertibleComparator<T>) other;
  return (this.comparator.equals(otherComp.comparator) && this.ascending == otherComp.ascending);
}

代码示例来源:origin: spring-projects/spring-framework

@Override
@SuppressWarnings("unchecked")
public boolean equals(Object other) {
  if (this == other) {
    return true;
  }
  if (!(other instanceof NullSafeComparator)) {
    return false;
  }
  NullSafeComparator<T> otherComp = (NullSafeComparator<T>) other;
  return (this.nonNullComparator.equals(otherComp.nonNullComparator) && this.nullsLow == otherComp.nullsLow);
}

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

@SuppressWarnings("unchecked")
static <E> ImmutableSortedMultiset<E> emptyMultiset(Comparator<? super E> comparator) {
 if (Ordering.natural().equals(comparator)) {
  return (ImmutableSortedMultiset<E>) RegularImmutableSortedMultiset.NATURAL_EMPTY_MULTISET;
 } else {
  return new RegularImmutableSortedMultiset<E>(comparator);
 }
}

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

@Override
public boolean equals(@Nullable Object object) {
 if (object == this) {
  return true;
 }
 if (object instanceof ComparatorOrdering) {
  ComparatorOrdering<?> that = (ComparatorOrdering<?>) object;
  return this.comparator.equals(that.comparator);
 }
 return false;
}

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

@Override
public boolean equals(@Nullable Object object) {
 if (object == this) {
  return true;
 }
 if (object instanceof LexicographicalOrdering) {
  LexicographicalOrdering<?> that = (LexicographicalOrdering<?>) object;
  return this.elementOrder.equals(that.elementOrder);
 }
 return false;
}

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

@Override
public boolean equals(@NullableDecl Object object) {
 if (object == this) {
  return true;
 }
 if (object instanceof LexicographicalOrdering) {
  LexicographicalOrdering<?> that = (LexicographicalOrdering<?>) object;
  return this.elementOrder.equals(that.elementOrder);
 }
 return false;
}

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

@Override
public boolean equals(@NullableDecl Object object) {
 if (object == this) {
  return true;
 }
 if (object instanceof ComparatorOrdering) {
  ComparatorOrdering<?> that = (ComparatorOrdering<?>) object;
  return this.comparator.equals(that.comparator);
 }
 return false;
}

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

@Override
public boolean equals(@NullableDecl Object object) {
 if (object == this) {
  return true;
 }
 if (object instanceof LexicographicalOrdering) {
  LexicographicalOrdering<?> that = (LexicographicalOrdering<?>) object;
  return this.elementOrder.equals(that.elementOrder);
 }
 return false;
}

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

@Override
public boolean equals(@NullableDecl Object object) {
 if (object == this) {
  return true;
 }
 if (object instanceof ComparatorOrdering) {
  ComparatorOrdering<?> that = (ComparatorOrdering<?>) object;
  return this.comparator.equals(that.comparator);
 }
 return false;
}

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

/**
 * Returns {@code true} if {@code elements} is a sorted collection using an ordering equivalent to
 * {@code comparator}.
 */
public static boolean hasSameComparator(Comparator<?> comparator, Iterable<?> elements) {
 checkNotNull(comparator);
 checkNotNull(elements);
 Comparator<?> comparator2;
 if (elements instanceof SortedSet) {
  comparator2 = comparator((SortedSet<?>) elements);
 } else if (elements instanceof SortedIterable) {
  comparator2 = ((SortedIterable<?>) elements).comparator();
 } else {
  return false;
 }
 return comparator.equals(comparator2);
}

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

private static <K, V> ImmutableSortedMap<K, V> copyOfInternal(
  Map<? extends K, ? extends V> map, Comparator<? super K> comparator) {
 boolean sameComparator = false;
 if (map instanceof SortedMap) {
  SortedMap<?, ?> sortedMap = (SortedMap<?, ?>) map;
  Comparator<?> comparator2 = sortedMap.comparator();
  sameComparator =
    (comparator2 == null) ? comparator == NATURAL_ORDER : comparator.equals(comparator2);
 }
 if (sameComparator && (map instanceof ImmutableSortedMap)) {
  // TODO(kevinb): Prove that this cast is safe, even though
  // Collections.unmodifiableSortedMap requires the same key type.
  @SuppressWarnings("unchecked")
  ImmutableSortedMap<K, V> kvMap = (ImmutableSortedMap<K, V>) map;
  if (!kvMap.isPartialView()) {
   return kvMap;
  }
 }
 return fromEntries(comparator, sameComparator, map.entrySet());
}

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

void method2()
  {
    boolean flag = true;
    if (flag) {
      System.identityHashCode("heh");
      flag = !flag; } String.CASE_INSENSITIVE_ORDER.
       equals("Xe-xe");
    // it is ok to have rcurly on the same line as previous
    // statement if lcurly on the same line.
    if (flag) { String.CASE_INSENSITIVE_ORDER.equals("it is ok."); }
  }
}

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

void method2()
  {
    boolean flag = true;
    if (flag) {
      System.identityHashCode("heh");
      flag = !flag; } String.CASE_INSENSITIVE_ORDER.
       equals("Xe-xe");
    // it is ok to have rcurly on the same line as previous
    // statement if lcurly on the same line.
    if (flag) { String.CASE_INSENSITIVE_ORDER.equals("it is ok."); }
  }
}

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

private void foo29() {
  boolean flag = true;
  if (flag) {
    System.identityHashCode("heh");
    flag = !flag; } String.CASE_INSENSITIVE_ORDER. //violation
    equals("Xe-xe");
}

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

/**
 * Returns an immutable sorted multiset containing the given elements sorted by the given {@code
 * Comparator}. This method iterates over {@code elements} at most once.
 *
 * <p>Despite the method name, this method attempts to avoid actually copying the data when it is
 * safe to do so. The exact circumstances under which a copy will or will not be performed are
 * undocumented and subject to change.
 *
 * @throws NullPointerException if {@code comparator} or any of {@code elements} is null
 */
public static <E> ImmutableSortedMultiset<E> copyOf(
  Comparator<? super E> comparator, Iterable<? extends E> elements) {
 if (elements instanceof ImmutableSortedMultiset) {
  @SuppressWarnings("unchecked") // immutable collections are always safe for covariant casts
  ImmutableSortedMultiset<E> multiset = (ImmutableSortedMultiset<E>) elements;
  if (comparator.equals(multiset.comparator())) {
   if (multiset.isPartialView()) {
    return copyOfSortedEntries(comparator, multiset.entrySet().asList());
   } else {
    return multiset;
   }
  }
 }
 elements = Lists.newArrayList(elements); // defensive copy
 TreeMultiset<E> sortedCopy = TreeMultiset.create(checkNotNull(comparator));
 Iterables.addAll(sortedCopy, elements);
 return copyOfSortedEntries(comparator, sortedCopy.entrySet());
}

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

@Override
public boolean equals(@Nullable Object obj) {
 if (obj instanceof GeneralRange) {
  GeneralRange<?> r = (GeneralRange<?>) obj;
  return comparator.equals(r.comparator)
    && hasLowerBound == r.hasLowerBound
    && hasUpperBound == r.hasUpperBound
    && getLowerBoundType().equals(r.getLowerBoundType())
    && getUpperBoundType().equals(r.getUpperBoundType())
    && Objects.equal(getLowerEndpoint(), r.getLowerEndpoint())
    && Objects.equal(getUpperEndpoint(), r.getUpperEndpoint());
 }
 return false;
}

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

public void testToImmutableSortedSet_customComparator() {
 Collector<String, ?, ImmutableSortedSet<String>> collector =
   ImmutableSortedSet.toImmutableSortedSet(String.CASE_INSENSITIVE_ORDER);
 BiPredicate<ImmutableSortedSet<String>, ImmutableSortedSet<String>> equivalence =
   (set1, set2) ->
     set1.equals(set2)
       && set1.asList().equals(set2.asList())
       && set1.comparator().equals(set2.comparator());
 ImmutableSortedSet<String> expected =
   ImmutableSortedSet.orderedBy(String.CASE_INSENSITIVE_ORDER).add("a", "B", "c", "d").build();
 CollectorTester.of(collector, equivalence)
   .expectCollects(expected, "a", "B", "a", "c", "b", "b", "d");
}

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

@Override
public boolean equals(@NullableDecl Object obj) {
 if (obj instanceof GeneralRange) {
  GeneralRange<?> r = (GeneralRange<?>) obj;
  return comparator.equals(r.comparator)
    && hasLowerBound == r.hasLowerBound
    && hasUpperBound == r.hasUpperBound
    && getLowerBoundType().equals(r.getLowerBoundType())
    && getUpperBoundType().equals(r.getUpperBoundType())
    && Objects.equal(getLowerEndpoint(), r.getLowerEndpoint())
    && Objects.equal(getUpperEndpoint(), r.getUpperEndpoint());
 }
 return false;
}

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

public void testToImmutableSortedMultiset() {
 BiPredicate<ImmutableSortedMultiset<String>, ImmutableSortedMultiset<String>> equivalence =
   (ms1, ms2) ->
     ms1.equals(ms2)
       && ms1.entrySet().asList().equals(ms2.entrySet().asList())
       && ms1.comparator().equals(ms2.comparator());
 CollectorTester.of(
     ImmutableSortedMultiset.<String>toImmutableSortedMultiset(
       String.CASE_INSENSITIVE_ORDER),
     equivalence)
   .expectCollects(ImmutableSortedMultiset.emptyMultiset(String.CASE_INSENSITIVE_ORDER))
   .expectCollects(
     ImmutableSortedMultiset.orderedBy(String.CASE_INSENSITIVE_ORDER)
       .addCopies("a", 2)
       .addCopies("b", 1)
       .addCopies("c", 3)
       .build(),
     "a",
     "c",
     "b",
     "c",
     "A",
     "C");
}

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

public void testToImmutableSortedMultisetCountFunction() {
 BiPredicate<ImmutableSortedMultiset<String>, ImmutableSortedMultiset<String>> equivalence =
   (ms1, ms2) ->
     ms1.equals(ms2)
       && ms1.entrySet().asList().equals(ms2.entrySet().asList())
       && ms1.comparator().equals(ms2.comparator());
 CollectorTester.of(
     ImmutableSortedMultiset.<String, String>toImmutableSortedMultiset(
       String.CASE_INSENSITIVE_ORDER, e -> e, e -> 1),
     equivalence)
   .expectCollects(ImmutableSortedMultiset.emptyMultiset(String.CASE_INSENSITIVE_ORDER))
   .expectCollects(
     ImmutableSortedMultiset.orderedBy(String.CASE_INSENSITIVE_ORDER)
       .addCopies("a", 2)
       .addCopies("b", 1)
       .addCopies("c", 3)
       .build(),
     "a",
     "c",
     "b",
     "c",
     "A",
     "C");
}

相关文章