Help With TreeMap Warning (Noob?)

Discussion in 'Java' started by Luc The Perverse, Dec 28, 2006.

  1. Sorry if I am asking a dumb question - but I am having a problem, an usual
    warning is coming up when I compile my simple program.

    When I compile

    ....\Desktop>javac edit_dist.java
    Note: edit_dist.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.

    Recompiling

    E:\Documents and Settings\Luc\Desktop>javac edit_dist.java -Xlint:unchecked
    edit_dist.java:41: warning: [unchecked] unchecked call to put(K,V) as a
    member of the raw type java.util.TreeMap
    mtm.put(myCurKey, new Integer(min));
    ^

    Here is my code

    import java.util.*;
    import java.lang.*;
    import java.math.*;

    public class edit_dist{
    TreeMap mtm = null;
    edit_dist(){
    mtm = new TreeMap<String, Integer>();
    }
    public static int naive_editDistance(String Source, String Dest){
    if(Source.equals("") || Dest.equals(""))
    return Dest.length() + Source.length();
    if(Source.charAt(0) == Dest.charAt(0))
    return naive_editDistance(Source.substring(1), Dest.substring(1));
    int min = 536870911; //arbitrarily large number above max length of
    string
    for(int i=0;i<2;i++)
    for(int j=(i!=0?0:1);j<2;j++)
    min = Math.min (min,
    1 + naive_editDistance(Source.substring(i),

    Dest.substring(j)));
    return min; // :)
    }
    private static String makeLen(String in, int OSL){
    int a = OSL - in.length();
    for(int i=0;i<a;i++)
    in+=(char) 1;
    return in;
    }
    int memoize_editDistance(String Source, String Dest, int OSL){
    if(Source.equals("") || Dest.equals(""))
    return Dest.length() + Source.length();
    String myCurKey = makeLen(Source, OSL) + Dest;
    if(mtm.containsValue(myCurKey))
    return ((Integer)mtm.get(myCurKey)).intValue();
    if(Source.charAt(0) == Dest.charAt(0))
    return naive_editDistance(Source.substring(1), Dest.substring(1));
    int min = 536870911; //arbitrarily large number above max length of
    string
    for(int i=0;i<2;i++)
    for(int j=(i!=0?0:1);j<2;j++)
    min = Math.min (min,
    1 + naive_editDistance(Source.substring(i),

    Dest.substring(j)));
    mtm.put(myCurKey, new Integer(min));
    return min; // :)
    }

    public static int memoize_editDistance(String Source, String Dest){
    return (new edit_dist()).memoize_editDistance(Source, Dest,
    Source.length());


    }
    public static void main(String[] xs){
    String[] x = {"accept", "allow", "ask", "believe", "borrow", "break",
    "bring",

    "buy", "can/beable", "cancel", "change", "clean", "comb", "complain",
    "cough", "count", "cut"};
    int[] res = {0, 5, 5, 6, 6, 5, 6, 6, 8, 4, 6, 4, 5, 7, 5, 4, 4, 5, 0, 4,
    6, 4, 5,

    5, 5, 9, 5, 6, 4, 5, 7, 5, 5, 5, 5, 4, 0, 7, 6, 4, 5, 3, 9, 5, 5, 5, 4, 7,
    5, 5, 3, 6, 6, 7, 0,

    6, 5, 5, 6, 8, 6, 6, 5, 7, 8, 7, 7, 7, 6, 4, 6, 6, 0, 4, 4, 5, 9, 6, 6, 6,
    5, 7, 5, 5, 6, 5, 5,

    4, 5, 4, 0, 3, 4, 8, 6, 6, 3, 5, 7, 5, 5, 5, 6, 5, 5, 5, 4, 3, 0, 4, 9, 6,
    4, 5, 5, 7, 5, 4, 5,

    6, 5, 3, 6, 5, 4, 4, 0, 9, 6, 6, 5, 4, 8, 4, 4, 2, 8, 9, 9, 8, 9, 8, 9, 9,
    0, 5, 7, 7, 8, 8, 9,

    9, 9, 4, 5, 5, 6, 6, 6, 6, 6, 5, 0, 3, 5, 5, 7, 5, 5, 5, 6, 6, 5, 6, 6, 6,
    4, 6, 7, 3, 0, 4, 5,

    7, 4, 4, 5, 4, 4, 5, 5, 6, 3, 5, 5, 7, 5, 4, 0, 4, 5, 4, 4, 4, 5, 5, 4, 7,
    5, 5, 5, 4, 8, 5, 5,

    4, 0, 5, 3, 3, 3, 7, 7, 7, 8, 7, 7, 7, 8, 8, 7, 7, 5, 5, 0, 6, 6, 7, 5, 5,
    5, 7, 5, 5, 5, 4, 9,

    5, 4, 4, 3, 6, 0, 2, 3, 4, 5, 5, 7, 5, 5, 4, 4, 9, 5, 4, 4, 3, 6, 2, 0, 2,
    4, 5, 3, 7, 6, 5, 5,

    2, 9, 5, 5, 4, 3, 7, 3, 2, 0}; //results from naive_editDistance assumed
    correct
    int nc = 0;
    int lv = 0;
    for(String C : x)
    for(String Y : x)
    if((lv=memoize_editDistance(C, Y))!=res[nc++])
    System.out.println("Failed on " + C +" to " + Y + " got "

    +lv + " expecting " + res[nc-1]);


    }


    }
    --
    LTP

    :)
    Luc The Perverse, Dec 28, 2006
    #1
    1. Advertising

  2. Luc The Perverse

    Lew Guest

    Luc The Perverse wrote:
    > ...\Desktop>javac edit_dist.java
    > Note: edit_dist.java uses unchecked or unsafe operations.
    > Note: Recompile with -Xlint:unchecked for details.

    This warning alerts you that your TreeMap declaration was not generic.

    > E:\Documents and Settings\Luc\Desktop>javac edit_dist.java -Xlint:unchecked
    > edit_dist.java:41: warning: [unchecked] unchecked call to put(K,V) as a
    > member of the raw type java.util.TreeMap
    > mtm.put(myCurKey, new Integer(min));
    > ^
    >
    > Here is my code
    >
    > import java.util.*;
    > import java.lang.*;

    You never need to import java.lang. It is the language itself, so to speak.

    > import java.math.*;
    >
    > public class edit_dist{
    > TreeMap mtm = null;

    The initialization to null is both redundant and superfluous.

    You did not use a generic type in the Map declaration.

    > edit_dist(){

    You should consider making the constructor public, or omitting it altogether.

    > mtm = new TreeMap<String, Integer>();
    > }


    By convention, you should name classes with an initial upper-case letter, each
    word part capitalized, and eschew underscores: EditDist.

    In most scenarios you should prefer to declare variables with the interface
    type rather than the concrete class type:

    Map<String, Integer> mtm = new TreeMap<String, Integer>();

    > public static int naive_editDistance(String Source, String Dest){


    By convention, you should name variables and methods with an initial
    lower-case letter, each word part capitalized, and eschew underscores:

    public static int naiveEditDistance( String source, String dest )
    ..

    They call this "camelCase".

    - Lew
    Lew, Dec 28, 2006
    #2
    1. Advertising

  3. Luc The Perverse

    Oliver Wong Guest

    "Lew" <> wrote in message
    news:...
    >
    > You should consider making the constructor public, or omitting it
    > altogether.


    There are reasons for having a non-public constructor. It's frequently
    used in the singleton pattern, for example.

    - Oliver
    Oliver Wong, Dec 28, 2006
    #3
  4. "Oliver Wong" <> wrote in message
    news:EBVkh.4120$...
    > "Lew" <> wrote in message
    > news:...
    >>
    >> You should consider making the constructor public, or omitting it
    >> altogether.

    >
    > There are reasons for having a non-public constructor. It's frequently
    > used in the singleton pattern, for example.


    I did not actually deliberately make it private - but as you can see, every
    invocation of the memoized edit distance function required its own copy of
    the class for it's own mapping.

    In general this is probably poor programming practice though.

    I do not know if there is an equivalent in Java - but in C++ I found
    protected constructors very useful for classes which were solely designated
    as helper classes. Likely Java takes care of this through the use of non
    public classes that can't be seen outside the file.

    --
    LTP

    :)
    Luc The Perverse, Dec 28, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ahmed Moustafa

    HashMap vs TreeMap

    Ahmed Moustafa, Aug 9, 2003, in forum: Java
    Replies:
    2
    Views:
    46,413
    Roedy Green
    Aug 10, 2003
  2. Harry

    TreeMap problem...

    Harry, Sep 22, 2003, in forum: Java
    Replies:
    3
    Views:
    469
    Harry
    Sep 23, 2003
  3. Wendy S
    Replies:
    4
    Views:
    14,232
    Jim McMaster
    Feb 19, 2004
  4. Miguel Angel
    Replies:
    9
    Views:
    13,830
    Christian Kaufhold
    Apr 15, 2004
  5. news.amnet.net.au

    Empty values in TreeMap - cannnot be removed?

    news.amnet.net.au, Apr 19, 2004, in forum: Java
    Replies:
    3
    Views:
    922
    Roedy Green
    Apr 19, 2004
Loading...

Share This Page