Home > Archive > Java Help > January 2008 > Question on Generics Syntax
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Question on Generics Syntax
|
|
| Ricardo Palomares Martínez 2008-01-26, 8:24 am |
| Hi,
I'm going crazy with the proper syntax of Collections.binarySearch():
http://java.sun.com/javase/6/docs/a...ml#binarySearch(java.util.List,%20T,%20java.util.Comparator)
I've been searching Google and Google Groups for similar questions,
but, while this question has been asked before, the answers don't seem
to apply to my case. This is my code:
import java.util.Collections;
public class Glossary {
final private List<GlossaryTerm> gList;
public Glossary() {
this.gList = new ArrayList<GlossaryTerm>();
}
public void addGlossaryTerm(String original, String comment) {
GlossaryTerm gt = new GlossaryTerm(original, comment);
if (Collections.binarySearch(gList, gt) < 0) {
gList.add(gt);
}
}
}
On compiling, I get this error:
....Glossary.java:107: cannot find symbol
symbol : method
binarySearch(java.util.List<net.localizethat.datamodel.GlossaryTerm>,net.localizethat.datamodel.GlossaryTerm)
location: class java.util.Collections
if (Collections.binarySearch(gList, gt) < 0) {
and I fail to see where is the error. During search, I've visited:
http://mindprod.com/jgloss/binarysearch.html
and I can't see the difference between the example Roedy put there
(BTW, thanks, Roedy!) and my code. Can anyone help me, please?
TIA
| |
|
| Ricardo Palomares MartÃ_nez wrote:
> Hi,
>
> I'm going crazy with the proper syntax of Collections.binarySearch():
>
> http://java.sun.com/javase/6/docs/a...ml#binarySearch(java.util.List,%20T,%20java.util.Comparator)
>
> I've been searching Google and Google Groups for similar questions,
> but, while this question has been asked before, the answers don't seem
> to apply to my case. This is my code:
>
> import java.util.Collections;
>
> public class Glossary {
> final private List<GlossaryTerm> gList;
>
> public Glossary() {
> this.gList = new ArrayList<GlossaryTerm>();
> }
>
> public void addGlossaryTerm(String original, String comment) {
> GlossaryTerm gt = new GlossaryTerm(original, comment);
>
> if (Collections.binarySearch(gList, gt) < 0) {
> gList.add(gt);
> }
> }
> }
>
> On compiling, I get this error:
>
> ....Glossary.java:107: cannot find symbol
> symbol : method
> binarySearch(java.util.List<net.localizethat.datamodel.GlossaryTerm>,net.localizethat.datamodel.GlossaryTerm)
> location: class java.util.Collections
> if (Collections.binarySearch(gList, gt) < 0) {
Start with what the error says, namely that it cannot find the method. (BTW,
there is never any 107 lines of code in your Usenet post. Often it's better
to send complete examples
<http://www.physci.org/codes/sscce.html>
but I think we can make do here.)
Is net.localizethat.datamodel the package for class Glossary, too? It'd
better be.
The Javadocs
<http://java.sun.com/javase/6/docs/a...ml#binarySearch(java.util.List,%20T)>
say that the first binarySearch() argument has to be of type
List<? extends Comparable<? super T>>
That means GlossaryTerm has to implement Comparable<? super GlossaryTerm>.
<http://java.sun.com/javase/6/docs/a...Comparable.html>
Does it?
And once you fix that, how well is binarySearch() going to work with an
unsorted list? (Hint: This issue is mentioned in the Javadocs, too.)
Always, always, always turn to the Javadocs for insight on API issues.
--
Lew
| |
| Eric Sosman 2008-01-26, 8:24 am |
| Ricardo Palomares Martínez wrote:
> Hi,
>
> I'm going crazy with the proper syntax of Collections.binarySearch():
>
> http://java.sun.com/javase/6/docs/a...ml#binarySearch(java.util.List,%20T,%20java.util.Comparator)
>
> I've been searching Google and Google Groups for similar questions,
> but, while this question has been asked before, the answers don't seem
> to apply to my case. This is my code:
>
> import java.util.Collections;
>
> public class Glossary {
> final private List<GlossaryTerm> gList;
>
> public Glossary() {
> this.gList = new ArrayList<GlossaryTerm>();
> }
>
> public void addGlossaryTerm(String original, String comment) {
> GlossaryTerm gt = new GlossaryTerm(original, comment);
>
> if (Collections.binarySearch(gList, gt) < 0) {
> gList.add(gt);
> }
> }
> }
>
> On compiling, I get this error:
>
> ...Glossary.java:107: cannot find symbol
> symbol : method
> binarySearch(java.util.List<net.localizethat.datamodel.GlossaryTerm>,net.localizethat.datamodel.GlossaryTerm)
> location: class java.util.Collections
> if (Collections.binarySearch(gList, gt) < 0) {
>
> and I fail to see where is the error. During search, I've visited:
>
> http://mindprod.com/jgloss/binarysearch.html
>
> and I can't see the difference between the example Roedy put there
> (BTW, thanks, Roedy!) and my code. Can anyone help me, please?
Just a hunch: Is it possible that GlossaryTerm does not
implement the Comparable<GlossaryTerm> interface?
--
Eric Sosman
esosman@ieee-dot-org.invalid
| |
|
| Eric Sosman wrote:
> Just a hunch: Is it possible that GlossaryTerm does not
> implement the Comparable<GlossaryTerm> interface?
I'm going to make a guess about the structure of GlossaryTerm, with the
Comparable interface:
package net.localizethat.datamodel;
public class GlossaryTerm implements Comparable <GlossaryTerm>
{
private final String term;
public GlossaryTerm( String t )
{
if ( t == null )
{
throw new IllegalArgumentException( new NullPointerException(
"term cannot be null" ));
}
term = t;
}
public final String getTerm()
{
assert term != null;
return term;
}
public final int compareTo( GlossaryTerm other )
{
assert term != null;
return term.compareTo( other.term );
}
}
--
Lew
| |
| Ricardo Palomares Martínez 2008-01-26, 7:27 pm |
| On 26 ene, 15:10, Lew <l...@lewscanon.com> wrote:
> Start with what the error says, namely that it cannot find the method. (BTW,
> there is never any 107 lines of code in your Usenet post. Often it's better
> to send complete examples
> <http://www.physci.org/codes/sscce.html>
> but I think we can make do here.)
I hoped it was enough, that's why I tried to relieve you from the
burden of
additional code. If I had prepared a SSCE, I probably would have
missed the
reason, too, because I would have never thought where the error was.
> Is net.localizethat.datamodel the package for class Glossary, too? It'd
> better be.
Yes, it is. :-)
> The Javadocs
> <http://java.sun.com/javase/6/docs/a...ns.html#bina...)>
>
> say that the first binarySearch() argument has to be of type
> List<? extends Comparable<? super T>>
>
> That means GlossaryTerm has to implement Comparable<? super GlossaryTerm>.
> <http://java.sun.com/javase/6/docs/a...Comparable.html>
>
> Does it?
This was the problem, I didn't quite understand the whole meaning of
that
syntax, and I got by the initial part:
public static <T> int binarySearch(...)
So, I was defining GlossaryTerm like this:
public class GlossaryTerm extends Comparable {
}
instead of:
public class GlossaryTerm extends Comparable<GlossaryTerm> {
}
>
> And once you fix that, how well is binarySearch() going to work with an
> unsorted list? (Hint: This issue is mentioned in the Javadocs, too.)
I know it, the plan is to keep the list sorted at every moment; but
thanks
for reminding. :-)
> Always, always, always turn to the Javadocs for insight on API issues.
I really started with it (it would be really dumb on my part to put a
link to
Javadocs without having reviewed it myself). :-) As I've said above, I
failed to
understand the real meaning of the nested generics, and I went over
Comparable
interface too quickly to realize it already accepts generics.
Thank you to both Eric and Lew for your promptly and helpful answers.
Sorry to have
bothered you with a so-newbie question. :-(
| |
|
| Ricardo Palomares MartÃ_nez wrote:
> Sorry to have
> bothered you with a so-newbie question. :-(
No bother. Glad the answers were useful. You were right about the Javadocs,
of course. Sorry about that.
You could also use TreeSet
<http://java.sun.com/javase/6/docs/a...il/TreeSet.html>
as an implementing class. It's not a one-for-one match, but its contains()
method
<http://java.sun.com/javase/6/docs/a...t.html#contains(java.lang.Object)>
works similarly to binarySearch(). You could wrap the one with the other, if
you wanted.
Good news about contains() is that
> This implementation provides guaranteed
> log(n) time cost for the basic operations (add, remove and contains).
These Javadocs don't seem to have completely caught up with generics.
--
Lew
| |
|
|
|
|
|