Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2004

Tip: Looking for answers? Try searching our database.

Simple Collection add problem

Thread view: 
Jeffrey Spoon - 19 Mar 2004 17:51 GMT
Hello, I have a problem with the following code:

public void collectString(String header) {

               //Set xhd = new HashSet(); declared in superclass

xhd.add(header);
Iterator iter = xhd.iterator();

       while(iter.hasNext()) {
               iter.next();
                       System.out.println("String: " + xhd);
}

                                       }// method collectString

What happens is collectString is called by it's superclass which passes
a String. The String should be different each time and then added to the
xhd HashSet. However when I do this I only get the last string passed to
the method, they are not appended to the previous string. Could the
problem be that the superclass is also called from another class which
is also in a loop, thus the new HashSet declaration each time is
confusing it? Adding other strings (as a test) within the method seems
to work ok.

Thanks in advance.

Signature

Jeffrey Spoon

Virgil Green - 19 Mar 2004 17:54 GMT
> Hello, I have a problem with the following code:
>
[quoted text clipped - 20 lines]
> confusing it? Adding other strings (as a test) within the method seems
> to work ok.

Post a *complete* working example and perhaps we can better help you find
the error. We can only guess at what the calling code might look like or be
doing.

- Virgil
Roedy Green - 20 Mar 2004 03:31 GMT
>The String should be different each time and then added to the
>xhd HashSet. However when I do this I only get the last string passed to
>the method, they are not appended to the previous string.

Java's hash collections allow only one entry per key.  If you want to
support duplicate keys, you have to create unique objects that track
the duplicates and put those unique objects with the common key in the
hash collection.

I can't think of any plausible scenario where you would want to
concatenate keys.
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Jeffrey Spoon - 20 Mar 2004 14:47 GMT
>>The String should be different each time and then added to the
>>xhd HashSet. However when I do this I only get the last string passed to
[quoted text clipped - 7 lines]
>I can't think of any plausible scenario where you would want to
>concatenate keys.

Sorry I didn't explain that properly. I shouldn't have said "they are
not appended to the previous String", what I meant was they are not
appended to the Set. I got this working by simply adding it within my
superclass, rather than calling the subclass I was using. Not quite sure
why it wouldn't work, possibly because there are several loops within
loops which might have caused problems. Anyway, I have another question,
which is how do I output the Set a string at a time? At the moment
printing xhd just prints the whole Set. Eventually I want to compare
them (using compareTo I'd guess), but I'd like to know how to Iterate
through them properly an element at a time and then print the result. Do
I have to override toString to do this or is it relatively
straightforward?

Signature

Jeffrey Spoon

Andrew Harker - 20 Mar 2004 15:49 GMT
> loops which might have caused problems. Anyway, I have another question,
> which is how do I output the Set a string at a time? At the moment
> printing xhd just prints the whole Set. Eventually I want to compare
> them (using compareTo I'd guess), but I'd like to know how to Iterate
> through them properly an element at a time and then print the result. Do
> I have to override toString to do this or is it relatively straightforward?

Use the Object returned by iter.next() which you
throw away in the code you originally posted, ie replace
xhd by iter.next()

    Iterator iter = xhd.iterator();
    while(iter.hasNext()) {
      System.out.println("> " + iter.next() + " <");
    }

Suggest you look at api docs
<http://java.sun.com/j2se/1.4.2/docs/api/index.html>
<http://java.sun.com/j2se/1.4.2/docs/api/java/util/Iterator.html>
and tutorials
<http://java.sun.com/docs/books/tutorial/collections/index.html>
<http://java.sun.com/docs/books/tutorial/collections/interfaces/collection.html>

cheers
Signature

Andrew

Jeffrey Spoon - 20 Mar 2004 16:22 GMT
>> loops which might have caused problems. Anyway, I have another
>>question,  which is how do I output the Set a string at a time? At the
[quoted text clipped - 12 lines]
>      System.out.println("> " + iter.next() + " <");
>    }

Ah, that's it. Thanks. I have looked at those other links, believe it or
not.

Signature

Jeffrey Spoon



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.