• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating auxiliary objects

 
Alex Teslin
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you simply stringize your for loop index to make it unique? Will it work for you?
 
Alex Teslin
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I meant this -

 
Alex Teslin
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, i am afraid that did not work. It compiles without an error. But produces runtime error: ClassCastException
 
Alex Teslin
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Alex Teslin
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...)
 
Alex Teslin
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic