简体   繁体   中英

Why cant I use the Collections.max() function for this code? - java

I have 10 trees. Each tree has a few instance variables (space, grid, suitability, and id); a place in geographic space and on a grid, a value representing habitat suitability from 0 to 1, and an ID number respectively.

I have put these trees and their associated data into a ArrayList called trees. And then I have the system print out their ID and associated value of suitability. The code is below.

At the end, I want to print out the highest suitability value, but I keep getting an error under max saying:

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

I cant seem to figure out what this means. Please let me know if you need to see more code.

Does anyone know how I can get the highest suitability value (the value closest to 1)?

int treeCount = 10;
for (int i = 0; i < treeCount; i++) {
    double suitability = RandomHelper.nextDoubleFromTo(0, 1);
    int id = i;
    context.add(new Trees(space, grid, suitability, id));

    ArrayList<Trees> trees = new ArrayList<Trees>();

    Trees tree;

    tree = new Trees(space, grid, suitability, id);
    trees.add(tree);
    System.out.println("Tree " + id + " has a suitability of " + suitability);

    Object obj = Collections.max(trees);
    System.out.println(obj);
}

You might have forgotten to implement Comparable<T> .. Without doing so, there is no way of telling which tree is the "biggest"..

You might want to do it something like that:

public class Tree implements Comparable<Tree> {
    @Override
    public int compareTo(Tree o) {
        // use the value which should be used for comparison instead of getSuitability().
        // remember: here you have private access to object o. if your value is not a
        // double, there is also a Integer.compare(..) function, but you could also just
        // return value - other.value..
        return Double.compare(getSuitability(), o.getSuitability());
    }
    // .. code ..
}

If you can't modify the Tree class, you can always implement a Comparator<Tree>

public class TreeComparator implements Comparator<Tree> {
    @Override
    public int compare(Tree o1, Tree o2) {
        return Double.compare(o1.getSuitability(), o2.getSuitability());
    }
}

And use it along with the Collections.max(Collection, Comparator) function:

Tree maxTree = Collections.max(trees, new TreeComparator());

If you are calling this code often, consider extracting the new TreeComparator() call to a constant.

It looks like Trees is not comparable.

Think - how does Collections.max(trees) determine what the max tree is?

You have two options:

  1. Trees implements Comparable

  2. Implement a Comparator and call Collections.max(trees, comparator)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM