Search code examples
javarepast-simphony

Collections.max function is returning hashcodes (I think)


I believe my Collections.max function is returning hash codes, but to be honest, I'm not entirely sure what is going on.

I am creating a list of trees with a random value of suitability, implementing a comparator, and then trying to find the highest suitability value (code is below).

public class Tree {
    public double suitability = Math.random();
    public int id;
    public static int count = 1;

    public Tree(double suitability, int id) {
        this.id = count;
        count++;    
    }

    public double getSuit() {
        return suitability;
    }

    public void setSuit(double suitability) {
        this.suitability = suitability;
    }

    public void measureSuit() {
           System.out.println("Tree number " + id + " has a suitability of " + suitability);
    }

}

class SuitComp implements Comparator<Tree> {

    @Override
    public int compare(Tree o1, Tree o2) {
        return Double.compare(o1.getSuit(), o2.getSuit());
    }   
} 


public class EmeraldRunner {

    public static void main(String[] args) {

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

        Tree tree;

        int treeCount = 10;
        for (int i = 0; i < treeCount; i++) {
        tree = new Tree(i, i);

        trees.add(tree)

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

        System.out.println(maxTree);
        tree.measureSuit();

        }

    }

}

My output looks like this:

learnFromCave.Tree@60f32dde

Tree number 1 has a suitability of 0.6114866528786418

learnFromCave.Tree@60f32dde

Tree number 2 has a suitability of 0.28381422309266247

learnFromCave.Tree@3312b1dd

Tree number 3 has a suitability of 0.8441348268153896

learnFromCave.Tree@3312b1dd

Tree number 4 has a suitability of 0.6269071898386682

learnFromCave.Tree@3312b1dd

Tree number 5 has a suitability of 0.08717540188464434

learnFromCave.Tree@3312b1dd

Tree number 6 has a suitability of 0.3810530158434646

learnFromCave.Tree@3312b1dd

Tree number 7 has a suitability of 0.0938353693923476

learnFromCave.Tree@3312b1dd

Tree number 8 has a suitability of 0.3656868216321937

learnFromCave.Tree@105b3e5d

Tree number 9 has a suitability of 0.9717207037612301

learnFromCave.Tree@105b3e5d

Tree number 10 has a suitability of 0.44423960773823645


Solution

  • System.out.println(maxTree);calls toString() on maxTree. Since you did not override it, it calls the method on Object, which just outputs the full class name and some additional information.

    You need to implement toString()yourself to get an more readable output.

    public class Tree {
        public double suitability = Math.random();
        public int id;
        public static int count = 1;
    
        public Tree(double suitability, int id) {
            this.id = count;
            count++;
        }
    
        public double getSuit() {
            return suitability;
        }
    
        public void setSuit(double suitability) {
            this.suitability = suitability;
        }
    
        public void measureSuit() {
            System.out.println("Tree number " + id + " has a suitability of " + suitability);
        }
    
        @Override
        public String toString() {
            return "I am the tree with id " + id;
        }
    }