Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
gibli
Posts: 44
Registered: ‎03-10-2009
My Device: Not Specified
Accepted Solution

Questioning the "don't use java.util.Enumeration" advice

http://na.blackberry.com/eng/deliverables/5827/Avoiding_javautilEnumeration_447043_11.jsp

 

Is this advice encouraging us to shift away from using Vectors/Hashtables ?

How else would you step throught a Vector ?

 

Regards,

 

Developer
simon_hain
Posts: 16,364
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Questioning the "don't use java.util.Enumeration" advice

as the link provided samples: use a for-loop (or any other, as you wish).
never crossed my mind to use the Enumeration interface on the BB.
On the other hand i implemented the "real" (j2se) enumeration interface in some classes as vector is not always the most convinient choice.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
marchywka
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: Questioning the "don't use java.util.Enumeration" advice

Not sure what the speed is for vector.elementAt() but normally I've been trying to convert

vectors to arrays once their sizes are known. Only time hashes come up is when you need

fast access and can tolerate some wasted memory.I'm not sure if there are any tricks to

enumerating the entries but if you have to go check each one then it will take some time...

I've only used enumeration with these for

debugging and some housekeeping code but if you determine you need to enumerate

a lot you may want to consider a different approach. If you have static data RIM offers

some array sorting facilities to let you put everything into an array and even index if for searching.

 

BTW, anyone know about memory caching on the RIM phones- does locality loss hurt much? 

I've given up caring in java in most cases since I can't control memory but it is mentioned here. 

 

http://en.wikipedia.org/wiki/Hash_table

 

I guess I hadn't worried much about hash key generation since I hope it is implemented in native

code but compared to a native binary search you would worry.

 

I guess if you need enumeration and think you need a hash table, consider a linked list approach such

that each table entry has a pointer to some prior and next item as then any item gives you an

automatic enumeration.

 

 

 

 

 

 

 

Developer
gibli
Posts: 44
Registered: ‎03-10-2009
My Device: Not Specified

Re: Questioning the "don't use java.util.Enumeration" advice

@simon,

 

On the other hand i implemented the "real" (j2se) enumeration interface in some classes as vector is not always the most convinient choice.

Just curious, when is Vector not a convenient choice ? A Vector is basically a dynamically size-adjusting list.

 

I'm spoiled by things like Android, which provides you so much power.

Having to reinvent all these things makes development a bit slower.

And BB does not have proper MVC either.  

 

Developer
gibli
Posts: 44
Registered: ‎03-10-2009
My Device: Not Specified

Re: Questioning the "don't use java.util.Enumeration" advice

@

 

..convert vectors to arrays once their sizes are known. 

 

That's a good practice. 

 

BB provides the IntHashtable and IntIntHashtable which are alleged to be more memory efficient than the vanilla one.

 

+1 on the linked list approach. 

Developer
gibli
Posts: 44
Registered: ‎03-10-2009
My Device: Not Specified

Re: Questioning the "don't use java.util.Enumeration" advice

[ Edited ]

This is the best practice to follow:

  1. If you're writing a portable Midlet, you've no choice but use Vectors. But arrays are faster substitutes.
  2. If you're writing a CLDC app, then by all means avoid Vectors, use net.rim.device.api.collection.* for your dynamic lists data structures.

 

Message Edited by gibli on 03-24-2009 10:40 PM
Developer
Developer
bo
Posts: 167
Registered: ‎11-12-2008
My Device: Not Specified

Re: Questioning the "don't use java.util.Enumeration" advice

[ Edited ]

gibli can you elaborate on your second point?  Are you saying that net.rim.device.api.collection.List is more efficient than java.util.Vector?

 

Thanks.

Message Edited by bo on 04-09-2009 03:39 PM