Empty values in TreeMap - cannnot be removed?

Discussion in 'Java' started by news.amnet.net.au, Apr 19, 2004.

  1. Hi

    I have some empty values that I get from a TreeMap which it seems I cannot
    remove.

    I have a TreeMap which holds a key and for each key an List with values - I
    get the values from a bean.

    I have concatenated several strings (using # as a separator) in each List
    entry which I would like to make, in my jsp, into individual ArrayList
    entries.

    In my jsp:

    TreeMap relationships = new TreeMap();
    ArrayList mylist - new ArrayList();
    relationships = mb.getAllRelationships(leftwardfile);
    (where mb is a bean which populates the TreeMap with values).

    Set keySet = relationships.keySet();
    Iterator it = keySet.iterator();
    while (it.hasNext()) {
    StringTokenizer separatetokenizer = new StringTokenizer(next =
    relationships.get(it.next()).toString(),"#",false);
    String token = separatetokenizer.nextToken();
    mylist.add(token);
    }

    The problem is that between each Iterator value (i.e between each tokenizing
    event where relationships.get(it.next()).toString() contains something), the
    arrayList puts an empty value in. The strange thing is that I cannot leave
    it out i..e. I tried:

    StringTokenizer separatetokenizer = new StringTokenizer(next =
    relationships.get(it.next()).toString(),"#",false);
    String token = separatetokenizer.nextToken();
    if (!"".equals(token) || !"null".equals(token)) {
    mylist.add(token);
    }

    But the empty value still goes in. The interesting thing is that it seems to
    be an empty value in the TreeSet as this only happens between two values of
    "next" (see above) that contain something.

    My question is: how can I assure no empty values go into the arraylist
    mylist? Someting unusual is happening and it must have something to do with
    the TreeSet, but I am not sure what it is.

    Any help will be most welcome.

    Thanks

    Hugo
     
    news.amnet.net.au, Apr 19, 2004
    #1
    1. Advertising

  2. news.amnet.net.au wrote:
    > Hi
    >
    >[snip]
    >
    > StringTokenizer separatetokenizer = new StringTokenizer(next =
    > relationships.get(it.next()).toString(),"#",false);
    > String token = separatetokenizer.nextToken();
    > if (!"".equals(token) || !"null".equals(token)) {
    > mylist.add(token);
    > }
    >
    > But the empty value still goes in.


    Hello,

    I think you should use a && instead of the ||, because you want to add
    it only when it is both not "" and not "null":

    if (!"".equals(token) && !"null".equals(token)) {
    mylist.add(token);
    }

    or you can use the || and set the appropriate parantheses:

    if (!( "".equals(token) || "null".equals(token) )) {
    mylist.add(token);
    }

    And perhaps a trim() on your token would be good, too:

    String token = separatetokenizer.nextToken().trim();

    HTH - Greetings,
    Stefan
     
    Stefan Waldmann, Apr 19, 2004
    #2
    1. Advertising

  3. news.amnet.net.au

    Chris Smith Guest

    news.amnet.net.au wrote:
    > I have some empty values that I get from a TreeMap which it seems I cannot
    > remove.


    You seem to have a good bit of confusion. Hope you don't mind if I give
    several suggestions besides answering your direct question.

    > I have a TreeMap which holds a key and for each key an List with values - I
    > get the values from a bean.
    >
    > I have concatenated several strings (using # as a separator) in each List
    > entry which I would like to make, in my jsp, into individual ArrayList
    > entries.


    Hmm. Any reason why you don't finish your parsing when you build the
    TreeSet in the first place? It'd certainly be cleaner than parsing data
    inside of a JSP.

    Nevertheless,

    > TreeMap relationships = new TreeMap();
    > ArrayList mylist - new ArrayList();
    > relationships = mb.getAllRelationships(leftwardfile);
    > (where mb is a bean which populates the TreeMap with values).


    Hmm. You've just created and thrown away a TreeMap here. It would be
    better to write:

    ArrayList mylist - new ArrayList();
    TreeMap relationships = mb.getAllRelationships(leftwardfile);

    That way you aren't creating a new blank TreeMap (on the first line of
    your code above) and then throwing it away by overwriting the reference
    variable with a reference to a second TreeMap (in the third line). It
    also avoids spurious initialization of local variables, which is a
    serious code-cleanliness problem in Java.

    > Set keySet = relationships.keySet();
    > Iterator it = keySet.iterator();
    > while (it.hasNext()) {
    > StringTokenizer separatetokenizer = new StringTokenizer(next =
    > relationships.get(it.next()).toString(),"#",false);
    > String token = separatetokenizer.nextToken();
    > mylist.add(token);
    > }


    This doesn't match your description of your data structures above. You
    said you have a TreeMap, and the values of that are Lists, and the
    elements of those lists are Strings, which contain substrings delimited
    by '#'. The code here, though, only works if what you really have is a
    TreeMap, the values of which are Strings, which contain substrings
    delimited by '#'. (But, because you used toString instead of a simple
    cast, if you're wrong about that then you'll get inexplicable strange
    behavior at runtime, rather than a compile-time error.)

    I don't know, of course, whether it's this code or your explanation
    that's wrong. You should check that, though.

    > The problem is that between each Iterator value (i.e between each tokenizing
    > event where relationships.get(it.next()).toString() contains something), the
    > arrayList puts an empty value in.


    You need to define "empty value" here. It would also help to see an
    actual #-delimited String that you pass into this method when you get
    this behavior. (Incidentally, all of this would be easier if you moved
    this code outside of a JSP so it could easily be called with test data
    to isolate the problem; see above comment about finishing your parsing
    before you get to the JSP.)

    > The strange thing is that I cannot leave
    > it out i..e. I tried:
    >
    > StringTokenizer separatetokenizer = new StringTokenizer(next =
    > relationships.get(it.next()).toString(),"#",false);
    > String token = separatetokenizer.nextToken();
    > if (!"".equals(token) || !"null".equals(token)) {
    > mylist.add(token);
    > }


    That if statement will always be true, so it doesn't exclude anything.
    Perhaps you meant && there? Incidentally, are you sure you meant to
    exclude the String "null" -- that is, the four-letter String consisting
    of the letters 'n', 'u', 'l', and 'l'? If you mean to look for a null
    reference, you'd say (token != null) rather than (!token.equals("null"))
    As it is now, a null reference would throw a NullPointerException in
    your code there.

    > My question is: how can I assure no empty values go into the arraylist
    > mylist? Someting unusual is happening and it must have something to do with
    > the TreeSet, but I am not sure what it is.


    The first thing to do, I guess, is to determine exactly what data is in
    that TreeSet and write a reproducible test case that shows this problem
    in code that's easily called (i.e., not in a JSP) with known input.
    Once you're there, it may be easier to figure out the problem; or if
    not, you'll at least have better luck getting help from newsgroups.

    Incidentally, your understanding of many basic aspects of the Java
    language seems to be lacking, and much (if not all) of your trouble
    seems to be coming from there. Some time spent with a good basic Java
    text (such as Bruce Eckel's) would probably pay off tremendously.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Apr 19, 2004
    #3
  4. news.amnet.net.au

    Roedy Green Guest

    On Mon, 19 Apr 2004 17:33:10 +0800, "news.amnet.net.au"
    <> wrote or quoted :

    >I have some empty values that I get from a TreeMap which it seems I cannot
    >remove.


    IIRC you are not supposed to put null keys in there in the first
    place.
    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Apr 19, 2004
    #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. Patricia Kline
    Replies:
    1
    Views:
    583
    Oliver
    Oct 25, 2003
  2. Wendy S
    Replies:
    4
    Views:
    14,236
    Jim McMaster
    Feb 19, 2004
  3. news.amnet.net.au
    Replies:
    2
    Views:
    9,245
    Roedy Green
    Apr 22, 2004
  4. Giovanni Azua
    Replies:
    1
    Views:
    377
    Fred Kleinschmidt
    Oct 31, 2006
  5. Agnes

    [Please help] Cannnot connect ODBC

    Agnes, Jan 31, 2004, in forum: ASP General
    Replies:
    6
    Views:
    267
Loading...

Share This Page