Compilation error - generics

12 views
Skip to first unread message

Bryan Buchanan

unread,
Dec 22, 2016, 8:58:04 PM12/22/16
to CodenameOne Discussions
Maybe I'm missing something obvious, but why does this code:

public class FirstCodenameOneLibrary {

    /**
     * Method javadoc information
     */
    public void hello() {
        System.out.println("Hello");
    }

    public void addObjects(Map<Integer, Object> objects) {
        int maxObjNumber = Collections.max(objects.keySet());
    }
}


give this error:

Compiling 1 source file to /home/bryanb/NetBeansProjects/PDFjet/build/tmp
/home/bryanb/NetBeansProjects/PDFjet/src/com/codename1/hello/FirstCodenameOneLibrary.java:24: error: no suitable method found for max(Set<Integer>)
        int maxObjNumber = Collections.max(objects.keySet());
    method Collections.<T#1>max(Collection<? extends T#1>) is not applicable
      (inferred type does not conform to upper bound(s)
        inferred: Integer
        upper bound(s): Comparable<? super Integer>,Object)
    method Collections.<T#2>max(Collection<? extends T#2>,Comparator<? super T#2>) is not applicable
      (cannot infer type-variable(s) T#2
        (actual and formal argument lists differ in length))
  where T#1,T#2 are type-variables:
    T#1 extends Object,Comparable<? super T#1> declared in method <T#1>max(Collection<? extends T#1>)
    T#2 extends Object declared in method <T#2>max(Collection<? extends T#2>,Comparator<? super T#2>)
1 error

The Netbeans project is JDK8. The same code compiles fine with JavaSE. The method signatures for Collections.max() are the same for CN1 and regular Java.

Steve Hannah

unread,
Dec 22, 2016, 9:01:38 PM12/22/16
to codenameone...@googlegroups.com
Currently Integer doesn't implement Comparable.  Its on the to do list so hopefully we'll have this soon.  In the mean time, use the 2-arg version of Collections.max(Collection, Comparable), and pass it a comparable that can compare integers.

Steve

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsub...@googlegroups.com.
Visit this group at https://groups.google.com/group/codenameone-discussions.
To view this discussion on the web visit https://groups.google.com/d/msgid/codenameone-discussions/17ba622f-463d-4aa7-a61f-a09aa0b0ef35%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Steve Hannah
Software Developer
Codename One

Bryan Buchanan

unread,
Dec 22, 2016, 9:04:57 PM12/22/16
to CodenameOne Discussions
OK - thanks Steve - I didn't think to look at Integer class.
Reply all
Reply to author
Forward
0 new messages