0

I'm trying to find the minimum and the maximum of a

ArrayList<Entry>

For example my ArrayList looks like this:

ArrayList<Entry> test = new ArrayList<Entry>();
test.add(new Entry(20, 0));
test.add(new Entry(5, 0));
test.add(new Entry(15, 0));

now I want the minimum(5) and the maximum(20) of this list.

I tried it with:

Collections.min(test);

But it says:

Bound mismatch: The generic method min(Collection<? extends T>) of type Collections is not applicable for the arguments (ArrayList<Entry>). The inferred type Entry is not a valid substitute for the bounded parameter <T extends Object & Comparable<? super T>>

I also tried:

test.length()

so I could do a for loop. But it also failed with this kind of ArrayList.

2
  • Sort it. Take the first and the last elements, then. Commented Nov 25, 2015 at 11:13
  • Use test.size() instead of test.length(). And don't hesitate to read docs Commented Nov 25, 2015 at 11:31

2 Answers 2

1

First, define a Comparator<Entry> which defines an ordering for Entry:

class EntryComparator implements Comparator<Entry> {
  @Override public int compare(Entry a, Entry b) {
    // ... whatever logic to compare entries.
    // Must return a negative number if a is "less than" b
    // Must return zero if a is "equal to" b
    // Must return a positive number if a is "greater than" b
  }
}

Then just iterate through the list, comparing each element to the current minimum and maximum elements:

Comparator<Entry> comparator = new EntryComparator();
Iterator<Entry> it = list.iterator();
Entry min, max;
// Assumes that the list is not empty
// (in which case min and max aren't defined anyway).

// Any element in the list is an upper bound on the min
// and a lower bound on the max.
min = max = it.next();

// Go through all of the other elements...
while (it.hasNext()) {
  Entry next = it.next();
  if (comparator.compare(next, min) < 0) {
    // Next is "less than" the current min, so take it as the new min.
    min = next;
  }
  if (comparator.compare(next, max) > 0) {
    // Next is "greater than" the current max, so take it as the new max.
    max = next;
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

What's the point of creating a custom comparator and explicit iterating through collection and calling it? At first, you're having tough time trying not to mess all these -1, 0 and 1, and then... you're doing a reverse conversion from -1,0,1 to "smaller" or "larger" relations. Isn't it better to just call Collections.max(list, comparator) and Collections.max(list, comparator)?
@VladimirS. (I'm guessing you mean min and max) Well, sure you can. This solution is a) more flexible, in that it works on Iterable as well as Collection; b) potentially more efficient since it only iterates once; c) more illustrative, since it shows how to actually implement it without using library methods. <shrug> YMMV :) You are right that using Collections methods would be "better", if conciseness is your metric.
I tried to implement the Class EntryComparator but I can't use the less than and greater than operators in combination with a Entry. public class EntryComparator implements Comparator<Entry> { @Override public int compare(Entry a, Entry b) { if(a == b){ return 0; } else if(a < b){ return -1; } else if(a > b){ return 1; } else{ return 0; } } }
@roux. You can only compare primitive values with <, > etc. If you could write comparisons like this, you'd have no need for the comparator :) Your code should look more like if (a.getField() == b.getField()) { return 0; } ... Please add the definition of Entry to the question, then I can suggest an implementation.
0

Entry has to implement the Comparator interface and provide an implementation for compare(T o1, T o2).

compare returns 0 if o1 and o2 are equals, a positive value if o1 is less then o2, a negative value otherwise

1 Comment

It is not usual for a class to implement Comparator for itself; it might implement Comparable, however, if there is a natural ordering for elements of that class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.