3

I'm really stuck on an assignment for school. We are learning about Generic Types, and maybe this is me not just understanding them fully, but as part of one of the first methods we have to implement:

We have:

public static <T> T min(Collection<T> c, Comparator<T> comp) {
        return null
}

And the requirements:

Selects the minimum value from the Collection c, as defined by the supplied Comparator comp. This method throws an IllegalArgumentException if either c or comp is null, and it throws a NoSuchElementException if c is empty. The Collection c is not changed by this method.

So I've gotten to here:

public static <T> T min(Collection<T> c, Comparator<T> comp)
            throws IllegalArgumentException, NoSuchElementException {
        if (c != null && comp != null) {
            if (!c.isEmpty()) {

            } else {
                throw new NoSuchElementException();
            }
        } else {
            throw new IllegalArgumentException();
        }
    }

We have to sort using a comparator, but CANNOT use the Collections class. I really just need some direction to start, I'm not asking you to do the assignment for me!

8
  • Arrays.sort()? docs.oracle.com/javase/7/docs/api/java/util/…
    – robert
    Feb 4, 2015 at 15:55
  • 2
    @robert Working around the use of Collections.sort by using the Arrays version is probably not what the instructor had in mind
    – blgt
    Feb 4, 2015 at 15:56
  • 1
    So thinking abstractly, if you had a list fill of something... I dunno, toys(?) how would you go about finding the least heavy of the toys? Feb 4, 2015 at 15:57
  • 1
    Oh right, to implement yourself, just iterate over the collection. Invoke the compareTo and keep the instance that evaluates less than. At the end of iteration return the instance that remains.
    – robert
    Feb 4, 2015 at 15:58
  • 1
    @robert T extends Comparable is not needed. Do not confuse Comparable with Comparator
    – gtgaxiola
    Feb 4, 2015 at 16:04

2 Answers 2

6

It should be easy. Comparator is used to compare two elements not for sorting. Sorting is not necessary here. You don't want to change the collection, just find the minimum value. You can just take first one and iterate over the collection comparing choosen element to others, switching choosen element to another when it's bigger. This is the way to find the minimum.

7
  • 1
    Thank you very much for following the 'just a hint' directions haha
    – acupofjose
    Feb 4, 2015 at 16:04
  • I think it's a good thinking. When you get the hint you'll learn more. Feb 4, 2015 at 16:05
  • 1
    You can cast null to any type like return (T) null; Feb 4, 2015 at 16:09
  • 1
    @SCHULTZ Probably is just an Eclipse Warning that you may disable. NetBeans seems to ignore it when doing T val = null;
    – gtgaxiola
    Feb 4, 2015 at 16:10
  • 2
    An alternative to iteration is to use a fold with Java8. Most courses cover folds in their functional language part, so the instructor probably won't object
    – blgt
    Feb 4, 2015 at 16:11
2

Pseudocode of what you need to add to your example:

  • Save the first element in a variable call "minimum". As it is declared generic the type of the object is T.
  • Iterate the collection (Clue: every collection has an iterator)
  • Compare each element with the "minimum" found. If lower than assign it to the minimum.
  • Return the variable minimum.

I won't put the code as it is homework and you need to familiarize with the syntax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.