Newsgroup: it.comp.java
Titolo: Is there STL for Java?

Hi,
I'm just looking at getting into Java and was discussing it with a friend
who told me there is STL for Java.  This news made my day ... but everything
I've been reading implies that there are no templates in Java.
I don't know if I misunderstood my friend or the book is too old (Mar 2002).
So ... Is there STL for Java?  Or not?
Thanks in advance,
John


The Collections Framework is sort-of an equivalent for STL. But current and
prior releases of Java don't have templates (generics are coming in Java
1.5). Therefore, the Collections Framework is defined in terms of the class
Object (every class in Java is implicitly a subclass of Object).
Net effect: you get a nicely predefined set of collection classes. You have
to cast the objects you put into the collections back to the right type when
you get them back out. (But this is safe to do in Java, as all user-defined
types are reference types and every object knows its actual class.) It isn't
quite as typesafe as STL, but it's pretty good.
-- Adam Maass

):

There is no Templates within Java and there is not a really reason for it 
like there was in C++.
Since every object in Java comes from a base object called Object.  So the 
Collections framework (Maps, Lists, Sets, Bags) all handle the Object 
objects.  So the large reasoning behind using templates has been fixed with 
java.  but it is not completely fixed until java 1.5 comes out.
J

 java 1.5 comes out.
The existence of a singly-rooted class hierarchy does not obviate
collection and container classes based on parametric polymorphism or
some other mechanism for providing static type safety.
It is this latter, static typing, that one cannot get in the Java
collection classes or any classes written in Java short of writing a
separate class to impose type constraints on the objects entering and
leaving a collection or container.
To do this with Java 2 <= 1.4 requires creating a "gate-keeper" class
that delegates to (_not_ subclasses) the underlying class from the
Java Collections Framework. Subclassing doesn't work because in Java
subclasses cannot restrict access to public methods inhertied from the
base class (that would break the Liskov Substitution Principle, for
one thing, and the language designers deliberately forbade it).
Similarly, there is no private derivation. Nor can subclasses create
method overrides that differ from the overriden method only in the
return type.
With generics (and the helpful addition of covariant return types),
Java will be able to come much closer to having C++-like, type-safe
collections and containers. I, for one, will welcome this addition,
though I don't relish the thought of having lots of code that will
have to be painstakingly converted to the new language and library
facilities (or just remain an ugly reminder of what will then be the
not-so-good old days).
Randall Schulz

, 
Ple...@reply.to.group.thx says...

If you want a collection framework that is more like STL than the 
standard Collections API from Sun, see JGL at:
http://www.recursionsw.com/products/jgl/jgl.asp
Note that JGL is not free however.
-- 
Dale King


Access to pre-releases requires registration with Sun but no fee.
Sun is not yet making any statements or commitments (or, to my
knowledge, hints) about a release date for J2SDK 1.5, so this avenue
is only viable for those whose need is not immediate and / or those
who are willing to base their development on a facility that is not
final (or even beta) quality and which will probalby undergo changes
that will not be backward compatible and will hence force changes in
the programs that use it.
Personally, I'm tempted, but cannot afford to take the plunge just yet.
Randall Schulz
