Empty values in TreeMap - cannnot be removed?

N

news.amnet.net.au

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
 
S

Stefan Waldmann

news.amnet.net.au said:
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
 
C

Chris Smith

news.amnet.net.au said:
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
 
R

Roedy Green

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top