aspose file tools*
The moose likes Performance and the fly likes Creating auxiliary objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "Creating auxiliary objects" Watch "Creating auxiliary objects" New topic
Author

Creating auxiliary objects

Alex Teslin
Ranch Hand

Joined: Jan 16, 2006
Posts: 49
Hi,

I am trying to test HashMap collection. In order to do this I need to create key-value pairs to fill the collection. For keys i thought to create strings that must be unique and for values i can either use the same strings or get some random number and cast it to Integer object.

I can not, however, think of a method that will quickly generate unique strings by say using a 'for' loop.

Does anyone has an idea or a simple technique?

Thank you in advance.
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Can you simply stringize your for loop index to make it unique? Will it work for you?


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Alex Teslin
Ranch Hand

Joined: Jan 16, 2006
Posts: 49
I don't understand what do you mean by stringizing for loop. I only mentioned for loop to create as many strings or any objects as possible by looping - thats all. Or did you mean to use an 'i' value of the loop and call toString method?
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
I meant this -

Alex Teslin
Ranch Hand

Joined: Jan 16, 2006
Posts: 49
Yes, this is even simpler because i was thinking of creating a text file with manualy typing key-value pairs and reading that file. But your suggestion makes it all simple.

Thanks for that
Alex Teslin
Ranch Hand

Joined: Jan 16, 2006
Posts: 49
No, i am afraid that did not work. It compiles without an error. But produces runtime error: ClassCastException
Alex Teslin
Ranch Hand

Joined: Jan 16, 2006
Posts: 49
I am trying to convert 'i's value to string using toString method which takes an int type as a parameter and returns string but would not compile.
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Show us the code.

toString () normally doesn't take any parameter, if we're talking about the same toString.


is all you need - no toString (), no casting.


http://home.arcor.de/hirnstrom/bewerbung
Alex Teslin
Ranch Hand

Joined: Jan 16, 2006
Posts: 49
Yes, sorry, it does work. But it gave me the error because i was testing several collections. So it does work for ArrayList, LinkedList, HashSet, but it does not work for TreeSet and i don't know why. So i excluded TreeSet test for the string objects.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I'm sure this could be made to work with a TreeSet too - can't say why it's not working for you, without seeing the code.

[Alex]: I am trying to convert 'i's value to string using toString method which takes an int type as a parameter and returns string but would not compile.

Should be less work than Stefan's String concatenation here. Note that this is a static method, so you don't need (or even want) an Integer object to use it. Assuming that what you want are Strings.

Note that it might well make sense for you to use Integers instead - especially if you're looking at things like TreeSet, which will sort the contents. With Strings, "10" comes before "2", but with Integers, 2 is before 10. So you might use

or (with JDK 5):

[ January 31, 2006: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Jim Yingst:

Should be less work than Stefan's String concatenation here.


Or



(Has the advantage that it doesn't need to be adjusted should the type of i change...)


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Alex Teslin
Ranch Hand

Joined: Jan 16, 2006
Posts: 49
Thank you all guys,
I will try all the cases and see the difference.
The reason i want string object is because i am testing collections with several types. Objects, ints, Strings, Integer object - for now. And trying to test the performance in terms of time.
I am also tempted to try to test a garbage collector, but i think that will be much trickier.

Thanks again
Alex Teslin
Ranch Hand

Joined: Jan 16, 2006
Posts: 49
In this collections test i am printing the results using a seperate print methods. I can use getClass() method to print the current collection under the test. Is there any way i can use some kind of method to print a type the collection takes. At the moment i have several print methods that are redundant because of this.

Thank you
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating auxiliary objects