Results 1 to 5 of 5

Thread: How can I make this unchecked method invocation checked?

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Resolved How can I make this unchecked method invocation checked?

    This is driving me crazy. As it stands below the compiler complains that the method invocation is unchecked. If I add a type parameter getMax(Collection<Num> c) the compiler moans that it canno't find symbol method max(java.util.Collection<Num>).
    Code:
    public static Num getMax(Collection c){
     return Collections.max(c); 
    }
    The method prototype simply suggests that any type can be passed in that extends the type that the Collection is marked to hold.
    Code:
    public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll)

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: How can I make this unchecked method invocation checked?

    Yes, but Java's not smart enough to deduce the type, it seems. Try:

    return Collections.max<Num>(c);
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: How can I make this unchecked method invocation checked?

    This is very confusing. The type param <Num> is necessary within or else im playing with unchecked method calls,
    Code:
    public static Num getMax(Collection<Num> c){
     return Collections.max(c); 
    }
    but then the compiler seems to not be able to find a version of
    Code:
    max(java.util.Collections<Num>)
    Code:
    return Collections.max<Num>(c);
    Simple dosen't work.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: How can I make this unchecked method invocation checked?

    Does your Num class implement Comparable?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: How can I make this unchecked method invocation checked?

    Posted by CornedBee

    Does your Num class implement Comparable?
    Now it does. Thanks for the help.
    Code:
    import java.util.Set;
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.Collections; 
    
    public class Max{
     public static void main(String[] args){
      Collection<Num> numl = new ArrayList<Num>(); 
      numl.add(new Num("2"));
      numl.add(new Num("6"));
      numl.add(new Num("3"));
      numl.add(new Num("1"));
      numl.add(new Num("5"));
      numl.add(new Num("4"));
      System.out.print("Max element is " + getMax(numl).getNum());   
     }
     public static Num getMax(Collection<Num> c){
       return Collections.max(c); 
     }
    }
    class Num implements Comparable<Num>{
     private String i; 
     public Num(String i){
      this.i = i; 
     }
     public int compareTo(Num num){
      int x = i.compareTo(num.i);
      return x; 
     }
     public String getNum(){
      return i; 
     }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width