The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Prefer Collections over older classes (???)

I found such tip on "Java Best Practices" site: http://www.javapractices.com/Topic39.cjp

It says that after introducing generics, arrays won't be necessary anymore. I don't like this idea at all. Collections should be used in place of Vectors and Hashtables, that's obvious. But YOU SHOULDN'T automatically replace arrays with Collections. Arrays are significantly smaller and iteration is much faster. What's more important, if you start to use Collections in place of arrays, your designs will start looking awkward. If your method is to return list of objects, and this list is not intended to be modified in any way but just read sequentially, arrays should ALWAYS be preferred over more complex structures.

What do you think?
Radoslaw Maruszewski Send private email
Tuesday, November 23, 2004
 
 
Hard to tell, as far as you know what you are doing, it fine.

Array come with some problem, as the article mentioned. You can't make an array immutable, and it will have error in multithread modification. But if it is used private without any kind of modification from external, it probably ok.

However, the overhead is probably negligible as long as your application is depend on network and database.
Carfield Yim Send private email
Tuesday, November 23, 2004
 
 
IMO, the "Java Best Practices" tip is right. It says:

"In JDK 1.5, a strong case can be made that Collections should almost always be preferred over arrays."

That "almost" word is important - there are some cases where you do need the speed of arrays. But not often. And Collections generally are much easier to work with, more elegant, and less error-prone (as the tip says).
Herr Herr Send private email
Tuesday, November 23, 2004
 
 
If you're reading sequentially, then arrays have negligable advantages over lists.

If random access is common, then arrays become more useful. Still, there aren't many situations where lists cause a noticeable delay.
Nigel Send private email
Tuesday, November 23, 2004
 
 
Each problem calls for a particular kind of data structure.

When data is not correctly structured, you end up writing complex convoluted code. The correct data structures make code simple and effective.
Dino
Tuesday, November 23, 2004
 
 
Well, if you're doing business processing, that may in general be true.  If you are writing a scientific application which may take two weeks to run, making the code look horrible, awful, hacked and hackneyed, for the sake of making it 10% faster, is worth it.  When you run up against efficiency constraints, the program elegance often suffers.
Haertchen
Wednesday, November 24, 2004
 
 
Of course, if you're doing scientific processing, why you would use Java is beyond me . . .
Haertchen
Wednesday, November 24, 2004
 
 
"Collections should be used in place of Vectors and Hashtables, that's obvious."

Please explain, I'm a newbie to java.  Vectors seemed perfectly fine and they are 'serializiable' (if that's a word)
Help, I need somebody
Thursday, November 25, 2004
 
 
Whoop - scratch that serializible comment  - i was thinking of something else.

But, still, how is a collection better?
Help, I need somebody
Thursday, November 25, 2004
 
 
Re: Collections vs.  Vectors and Hashtables

Well, technically Vectors and Hashtables ARE Collections, since Sun retrofit these older classes with the new frameworks.

The main reason these classes are not preferred, though, is that they are *synchronized*, meaning that they do their own internal logic to make them re-entrant.  The newer Collection classes don't do this, making them faster typically.  You can also call a special method to get a synchronized collection from a regular one, in case you really need this option.

Also, with 1.5 and generics, you'll want to use Collections, since now you can have type-specific collections for free.

It's not really that big a deal though if you use Vector and Hashtable, its just that their considered deprecated.
Dave C Send private email
Sunday, November 28, 2004
 
 
Re: Arrays should be unnecessary

I think the point here, like with Vectors, is that generics require the use of Collection classes to make type-specific collections.  So you'll get the dynamic nature of Collections, with the strong typing of arrays, which is probably USUALLY a win-win.

I agree, though, that int[], or byte[] are still useful, from a space and efficiency standpoint.
Dave C Send private email
Sunday, November 28, 2004
 
 
Arrays may be faster than collections...but then _not_ doing bounds-checking on arrays is faster than doing it.  Yet Java does the latter, because given a choice between greater speed and less programmer error, it chooses the latter.

So my conclusion would be that even if arrays are faster than collections, the Java way would be to use the one which is easier to work with and less error-prone rather than the one which is faster.
Kyralessa Send private email
Monday, November 29, 2004
 
 
<quote>
the Java way would be to use the one which is easier to work with and less error-prone rather than the one which is faster
</quote>

That's well put.  As a longtime Java developer (C++ before that, and plain C before that), its definitely about ease of use and maintainability over pure speed.  Hardware speed and virtual machine optimizations have literally made Java code about 100 times faster than when I first started using it.

So arrays, aside from speed, don't offer much to a Java programmer except strong typing.  The new 1.5 generics support gives you this missing piece (although its at compile time is my understanding), so you may as well use Collections with their dynamic sizing capability.
Dave C Send private email
Monday, November 29, 2004
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz