The Set Class in C++ and Java

C

coosa

Dear all,

I'm familiar with the Set Class in C++ and Java; however, i have been
searching in C# for a similar container as in c++ or collection as in
java, but couldn't find one.
Could some one tell me about the the Set implementation inside .NET?

Best regards
 
K

Kevin Spencer

I am able to download the file. I don't know where you got the URL for the
file that you posted, because the link I used was an ASP.Net Control on the
page which performs a PostBack to retrieve the file. If you look on the page
near the top of the article, you will see the following text:

Download This Tool: Set.cs

The text "Set.cs" is the hyperlink to download the code. I would post it
myself, but I'm not sure that would be kosher, since I was not the author.

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

A watched clock never boils.
 
C

coosa

Kevin said:
I am able to download the file. I don't know where you got the URL for the
file that you posted, because the link I used was an ASP.Net Control on the
page which performs a PostBack to retrieve the file. If you look on the page
near the top of the article, you will see the following text:

Download This Tool: Set.cs

The text "Set.cs" is the hyperlink to download the code. I would post it
myself, but I'm not sure that would be kosher, since I was not the author.

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

A watched clock never boils.

My bad, my mozilla firefox prevented the scripts for the page, so i
couldn't download it! :)
any way, i have checked the file; it doesn't first implement generics
and doesn't really consider strong typing.
Any way; i really hope that Microsoft comes out with a Set
implementation in their future releases.
As an alternative, i see the HashTable, but considering a constantly
sorted range complexity of the tree, a HashTable is not always the
better choice since the fast lookup is what makes it very powerful.
 
K

Kevin Spencer

any way, i have checked the file; it doesn't first implement generics
and doesn't really consider strong typing.
Any way; i really hope that Microsoft comes out with a Set
implementation in their future releases.
As an alternative, i see the HashTable, but considering a constantly
sorted range complexity of the tree, a HashTable is not always the
better choice since the fast lookup is what makes it very powerful.

The Java Set Interface is not strongly-typed either. In fact, it can contain
any combination of data in it, not just one type. See
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Set.html. I saw quite a
number of other C# Set Class implementations (see
http://www.google.com/search?hl=en&q=C#+Set+Class), but the one that I
pointed you to was the closest one to the Java Set Interface, which I
thought was what you were looking for.

In any case, you are free to create your own Collection classes, derive from
any existing Collection class, and implement whatever functionality you
desire. Perhaps, considering your desire for strong typing, you might want
to start from one of the System.Collections.ObjectModel
(http://msdn2.microsoft.com/en-us/library/system.collections.objectmodel.aspx)
or System.Collections.Generic
(http://msdn2.microsoft.com/en-us/library/system.collections.generic.aspx)
namespace classes.

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

A watched clock never boils.
 
K

Kevin Spencer

Understood, Jon. However, I do think that the ability to include instances
of multiple types is useful, and of course, this is still possible with the
generic Set class if you define the type as object.

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

A watched clock never boils.
 
C

coosa

Kevin said:
The Java Set Interface is not strongly-typed either. In fact, it can contain
any combination of data in it, not just one type. See
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Set.html. I saw quite a
number of other C# Set Class implementations (see
http://www.google.com/search?hl=en&q=C#+Set+Class), but the one that I
pointed you to was the closest one to the Java Set Interface, which I
thought was what you were looking for.

In any case, you are free to create your own Collection classes, derive from
any existing Collection class, and implement whatever functionality you
desire. Perhaps, considering your desire for strong typing, you might want
to start from one of the System.Collections.ObjectModel
(http://msdn2.microsoft.com/en-us/library/system.collections.objectmodel.aspx)
or System.Collections.Generic
(http://msdn2.microsoft.com/en-us/library/system.collections.generic.aspx)
namespace classes.

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

A watched clock never boils.

That is not true at all,

Since JDK 1.5 Update 2, Java came up with alot of stong typed generics
including the vector , set, ...etc. classes.
Earlier you could not for instance use a statement in Java like this:
java.util.Vector <SomeClass> v; It took every thing as object and u
needed to cast. However, as from Update 2 i guess of 1.5 jdk, they
adapted the STL implementation of strong typing.
 
C

coosa

Kevin said:
The Java Set Interface is not strongly-typed either. In fact, it can contain
any combination of data in it, not just one type. See
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Set.html. I saw quite a
number of other C# Set Class implementations (see
http://www.google.com/search?hl=en&q=C#+Set+Class), but the one that I
pointed you to was the closest one to the Java Set Interface, which I
thought was what you were looking for.

In any case, you are free to create your own Collection classes, derive from
any existing Collection class, and implement whatever functionality you
desire. Perhaps, considering your desire for strong typing, you might want
to start from one of the System.Collections.ObjectModel
(http://msdn2.microsoft.com/en-us/library/system.collections.objectmodel.aspx)
or System.Collections.Generic
(http://msdn2.microsoft.com/en-us/library/system.collections.generic.aspx)
namespace classes.

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

A watched clock never boils.

That is not true at all,

Since JDK 1.5 Update 2, Java came up with alot of stong typed generics
including the vector , set, ...etc. classes.
Earlier you could not for instance use a statement in Java like this:
java.util.Vector <SomeClass> v; It took every thing as object and u
needed to cast. However, as from Update 2 i guess of 1.5 jdk, they
adapted the STL implementation of strong typing.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

coosa said:
Since JDK 1.5 Update 2, Java came up with alot of stong typed generics
including the vector , set, ...etc. classes.
Earlier you could not for instance use a statement in Java like this:
java.util.Vector <SomeClass> v; It took every thing as object and u
needed to cast. However, as from Update 2 i guess of 1.5 jdk, they
adapted the STL implementation of strong typing.

Generics were in Java 1.5 from the start. It was not added
in update 2.

Java generics and C++ STL are not the same.

Arne
 
C

coosa

Arne said:
Generics were in Java 1.5 from the start. It was not added
in update 2.

Java generics and C++ STL are not the same.

Arne

I'm aware they are not the same, but so far i recall the adding of a
template class for the Vectors in java by update 2 <> since i by 1.5 i
couldn't find such in the api until i downloaded update 2.
But whether update 1, 2 or from the star tof 1.5, i just wanted to say
that Java supports generics
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

coosa said:
I'm aware they are not the same, but so far i recall the adding of a
template class for the Vectors in java by update 2 <> since i by 1.5 i
couldn't find such in the api until i downloaded update 2.

I still have the first release of 1.5 installed.

Vector is generic.

They do not add new functionality to updates, they only
fix bugs.
But whether update 1, 2 or from the star tof 1.5, i just wanted to say
that Java supports generics

That is true !

Arne
 
N

nick_nw

Arne said:
I still have the first release of 1.5 installed.

Vector is generic.

They do not add new functionality to updates, they only
fix bugs.


That is true !

Arne

As far as I'm aware Java generics are just syntactic sugar enforced at
compile time, but the resultant code will still be a collection of
Objects.

C# generics result in a collection of the correct type, so I generic
list specified as <int> will actually be a list of ints with no
boxing/unboxing required.

(I am not saying that one language is better than the other, just
pointing out a difference)

Nick
http://seecharp.blogspot.com/
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

As far as I'm aware Java generics are just syntactic sugar enforced at
compile time, but the resultant code will still be a collection of
Objects.

Java and C# has almost the same syntax for generics, but
the implementation in Java byte code and MSIL is very
different.

Java generics basically disappear in the Java byte code. In
most cases it is not significant. But in some cases it
can cause unexpected results for the programmer who have
not understood the concept.
C# generics result in a collection of the correct type, so I generic
list specified as <int> will actually be a list of ints with no
boxing/unboxing required.

I can not see any connection between type erasure and
boxing/unboxing.

ArrayList<int> is not valid in Java. Accessing an
ArrayList<Integer> does not cause any boxing/unboxing.

Boxing/unboxing between Integer and int obviously
causes boxing/unboxing.

Arne
 
J

Jon Skeet [C# MVP]

Arne Vajhøj said:
Java and C# has almost the same syntax for generics, but
the implementation in Java byte code and MSIL is very
different.

Well, they have similar syntax for a small part of it - the way a
generic type name is written, eg List<String>. They differ in:

o How constraints are expressed
o How generic methods are declared
o Covarance/contravariance
Java generics basically disappear in the Java byte code. In
most cases it is not significant. But in some cases it
can cause unexpected results for the programmer who have
not understood the concept.

Unfortunately it makes things very limiting in some ways, because the
information isn't available at runtime. It becomes unsafe (in unobvious
ways) to cast to a generic type, whereas in .NET the cast can be
checked properly at runtime.

Of course, Java has covariance and contravariance which C# doesn't
support, so it's not all one-way...
I can not see any connection between type erasure and
boxing/unboxing.

ArrayList<int> is not valid in Java. Accessing an
ArrayList<Integer> does not cause any boxing/unboxing.

Boxing/unboxing between Integer and int obviously
causes boxing/unboxing.

It's worth remembering that another feature of Java 5 is auto-boxing
and auto-unboxing though, so if you do:

List<Integer> list = new ArrayList<Integer>();
list.add(5);
int x = list.get(0);

that's okay.

What's slightly surprising is that boxing doesn't always create a new
object:

Integer a1 = 5;
Integer a2 = 5; // Now a1==a2

but

Integer a3 = 4000;
Integer a4 = 4000; // a3!=a4

(Obviously a3.equals(a4), but the references aren't equal.)

Just a little bit of trivia...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top