Problem with <T extends Comparable<? super T>>>


 
Thread Tools Search this Thread
Top Forums Programming Problem with <T extends Comparable<? super T>>>
# 1  
Old 07-14-2011
Problem with <T extends Comparable<? super T>>>

Hi guys,

I have a three class:
1. Class Algorithm having max() finding maximum value in a Collection
Code:
public class Algorithm {
    public static <T extends Comparable<T>> T max(Collection<? extends T> coll) {
        T max = coll.iterator().next();

        for (T elm : coll) {
            if (max.compareTo(elm) < 0)
                max = elm;
        }

        return max;
}

2. Class Fruit
Code:
public class Fruit implements Comparable<Fruit> {
    private String name;
    private int size;

    public Fruit(String name, int size) {
        this.name = name;
        this.size = size;
    }
    
    public int compareTo(Fruit that) {
        if (size < that.size)
            return -1;
        else if (size == that.size)
            return 0;
        else
            return 1;
    }
}

3. class Apple extending Fruit
Code:
public class Apple extends Fruit {
    public Apple(int size) {
    super("Apple", size);
    }
}

Now the question is this:
Code:
public class Main
{
    public static void main(String[] args) {        
        Apple a1 = new Apple(10);
        Apple a2 = new Apple(34);
        
        List<Apple> apples = Arrays.<Apple>asList(a1, a2);
        System.out.println(Collections.max(apples).name);
        }
}

according to the Collection.max() function I should write
Code:
public static <T extends Comparable<? super T>> T max(Collection<? extends T> coll)

Because class Apple does not implement Comparable. But the one I have wrote is working fine without any problems.
Login or Register to Ask a Question

Previous Thread | Next Thread

4 More Discussions You Might Find Interesting

1. Programming

FORTRAN Extends

I am writing some code in fortran that defines certain shapes. Currently I have a cuboid and a prism. Now a parallelepiped is both a cuboid and a prism. Is there a way to code this? Currently I only have Type, Extends (Cuboid) :: Parallelepipied How can I do so that Parallelepipied is... (3 Replies)
Discussion started by: kristinu
3 Replies

2. HP-UX

BAD SUPER BLOCK - Run fsck with alternate super block number

Error received when I tried to restore a blank disk with an 'auto recovery' DDS tape via HP-UX recovery system 2.0 onto a 1Gb SCSI. I assumed it would do the setup, wrong. Could someone tell me the procedure to initial disk for recovering files using cpio. The system is a HP-UX 9.04 version on a... (1 Reply)
Discussion started by: admin wanabee
1 Replies

3. BSD

comparable command in OpenBSD for linux's "passwd -l (name)"

i'm trying to get a samba server up and running. however, there is no argument in OpenBSD's "passwd" command that i know of that will allow the following: -l This option is used to lock the specified account and it is available to root only. The locking is performed by... (1 Reply)
Discussion started by: xyyz
1 Replies

4. UNIX for Dummies Questions & Answers

Comparable registry file in UNIX

Hi all, I am totally new to UNIX. I am currently using converting an existing software package from MFC C++ to wxwindow which allows the software to run under Windows and UNIX. My problem is my existing software package writes to and from the registry. How is this accomplished in UNIX? ... (2 Replies)
Discussion started by: DavidCountry
2 Replies
Login or Register to Ask a Question