Java 8 Comparator - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 Comparator class.

This code example shows how to use the following methods:compare
    public static boolean equals(Object o1, Object o2) {
        if (o1 instanceof JSONObject && o2 instanceof JSONObject) {
            return JSON_OBJECT_COMPARATOR.compare((JSONObject) o1, (JSONObject) o2) == 0;
        } 
        if (o1 instanceof JSONArray && o2 instanceof JSONArray) {
            return JSON_ARRAY_COMPARATOR.compare((JSONArray) o1, (JSONArray) o2) == 0;
        } 
        return o1 == null ? o2 == null : o1.equals(o2);
    } 
 
    public static JSONObject makeRandomJson() {
        try { 
            Random random = new Random();
            JSONObject json = new JSONObject();
            final int size = random.nextInt(20) + 5;
            for (int i = 0; i < size; i++) {
                json.put("id:" + i, random.nextInt(100000) + 500);
            } 
            return json;
        } catch (JSONException e) {
7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:compare
    private R next;
 
    public ResultMergeSortIterator(Iterator<R> first, Iterator<R> second, Comparator<R> comparator, boolean filterDuplicates) {
        Preconditions.checkNotNull(first);
        Preconditions.checkNotNull(second);
        Preconditions.checkNotNull(comparator);
        this.first = first;
        this.second = second;
        this.comp = comparator;
        this.filterDuplicates = filterDuplicates;
 
        nextFirst = null;
        nextSecond = null;
        next = nextInternal();
    } 
 
    @Override 
    public boolean hasNext() { 
        return next != null;
    } 

7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:compare
    agg.aggregate();
    agg.aggregate();
    metricValues.put(agg.getName(), agg.get());
    Object after = arithmeticPostAggregator.compute(metricValues);
 
    Assert.assertEquals(-1, comp.compare(before, after));
    Assert.assertEquals(0, comp.compare(before, before));
    Assert.assertEquals(0, comp.compare(after, after));
    Assert.assertEquals(1, comp.compare(after, before));
  } 
} 
6
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:compare
  public int compareTo(PartitionChunk<T> chunk)
  { 
    if (chunk instanceof LinearPartitionChunk) {
      LinearPartitionChunk<T> linearChunk = (LinearPartitionChunk<T>) chunk;
 
      return comparator.compare(chunkNumber, linearChunk.chunkNumber);
    } 
    throw new IllegalArgumentException("Cannot compare against something that is not a LinearPartitionChunk.");
  } 
 
  @Override 
  @SuppressWarnings("unchecked") 
  public boolean equals(Object o)
  { 
    if (this == o) {
      return true; 
    } 
    if (o == null || getClass() != o.getClass()) {
      return false; 
    } 
7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:compare
  public int compareTo(PartitionChunk<T> chunk)
  { 
    if (chunk instanceof StringPartitionChunk) {
      StringPartitionChunk<T> stringChunk = (StringPartitionChunk<T>) chunk;
 
      return comparator.compare(chunkNumber, stringChunk.chunkNumber);
    } 
    throw new IllegalArgumentException("Cannot compare against something that is not a StringPartitionChunk.");
  } 
 
  @Override 
  @SuppressWarnings("unchecked") 
  public boolean equals(Object o)
  { 
    if (this == o) {
      return true; 
    } 
    if (o == null || getClass() != o.getClass()) {
      return false; 
    } 
7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:compare
        String rPath = rhs.getFilename();
        final int rSlash = rPath.lastIndexOf('/');
        if (rSlash != -1)
            rPath = rPath.substring(rSlash + 1);
 
        return CASE_INSENSITIVE_ORDER.compare(lPath, rPath);
    } 
} 
6
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:compare
  @Override 
  public int compareTo(PartitionChunk<T> chunk)
  { 
    if (chunk instanceof IntegerPartitionChunk) {
      IntegerPartitionChunk<T> intChunk = (IntegerPartitionChunk<T>) chunk;
      return comparator.compare(chunkNumber, intChunk.chunkNumber);
    } else { 
      throw new IllegalArgumentException("Cannot compare against something that is not an IntegerPartitionChunk.");
    } 
  } 
 
  @Override 
  public boolean equals(Object o)
  { 
    if (this == o) {
      return true; 
    } 
    if (o == null || getClass() != o.getClass()) {
      return false; 
    } 
7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:equals
		if (m.size() == 0 && c.size() > 0 && c instanceof SortedSet && m instanceof OMVRBTree) {
			SortedSet<? extends E> set = (SortedSet<? extends E>) c;
			OMVRBTree<E, Object> map = (OMVRBTree<E, Object>) m;
			Comparator<? super E> cc = (Comparator<? super E>) set.comparator();
			Comparator<? super E> mc = map.comparator();
			if (cc == mc || (cc != null && cc.equals(mc))) {
				map.addAllForOTreeSet(set, PRESENT);
				return true; 
			} 
		} 
		return super.addAll(c);
	} 
 
	/** 
	 * @throws ClassCastException 
	 *           {@inheritDoc} 
	 * @throws NullPointerException 
	 *           if {@code fromElement} or {@code toElement} is null and this set uses natural ordering, or its comparator does not 
	 *           permit null elements 
	 * @throws IllegalArgumentException 
6
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:compare
            return rhsLogin.equals(login) ? 0 : -1;
 
        if (rhsLogin.equals(login))
            return lhsLogin.equals(login) ? 0 : 1;
 
        return CASE_INSENSITIVE_ORDER.compare(lhsLogin, rhsLogin);
    } 
} 
6
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:equals
		} 
		if (!(obj instanceof NullSafeComparator)) {
			return false; 
		} 
		NullSafeComparator<T> other = (NullSafeComparator<T>) obj;
		return (this.nonNullComparator.equals(other.nonNullComparator) && this.nullsLow == other.nullsLow);
	} 
 
	@Override 
	public int hashCode() { 
		return (this.nullsLow ? -1 : 1) * this.nonNullComparator.hashCode();
	} 
 
	@Override 
	public String toString() {
		return "NullSafeComparator: non-null comparator [" + this.nonNullComparator + "]; " +
				(this.nullsLow ? "nulls low" : "nulls high");
	} 
 
} 
6
CodeRank
Experience pair programming with AI  Get Codota for Java

Related Java 8 Comparator Questions & Answers:

See Code Examples for Java 8 Comparator Methods: