new to generics

C

Collins

Hello All-

In the past I have used java pre generics for some programs related
to my hobbies. I have come back to it after a couple of years and am
biting the bullet and moving to 1.5(5.0)...

In the past I have made some type specific packages to generate some
basic combinatoric structures (permutations, combinations, partitions)

For my own use and education, I'm trying to figure out how to make this
more usfully with generics.

Specifically:

class foo {...}

class otherClass{
HashSet<foo> fooSet;

// now comes the tricky (to me) part

Iterator<???> perm = new permutation(fooSet);
/*
I'd like to have iterator return one permuation of the elements in
fooSet at a time
*/
}

So... Does this type of thing already exist somewhere? If so where?
If not how do I go about using generics to do such a thing?

Thanks in advance

Collins
 
T

Thomas Hawtin

Collins said:
HashSet<foo> fooSet;

// now comes the tricky (to me) part

Iterator<???> perm = new permutation(fooSet);
/*
I'd like to have iterator return one permuation of the elements in
fooSet at a time
*/
}

An iteration of sets of Foo?

Iterator<Set<Foo>> perm = new Permutation<Foo>();

Or preferably

Iterable<Set<Foo>> perm = new Permutation<Foo>();

where

class Permutation<T> implements Iterable<Set<T>> {
Permutation(Set<? extends T> domain) {
...

Using ? extends just means that you can use Set typed with a subtype of
T. So you could write class Bar extends Foo {} ... new
Permutation<Foo>(new HashSet<Bar>()).

Tom Hawtin
 
J

Jean-Francois Briere

By the way since HashSet makes no guarantees as to the iteration order
of the set, it will be hard to do permutations on the set don't you
think?

You could use instead LinkedHashSet for instance.
 
T

Thomas Hawtin

Jean-Francois Briere said:
By the way since HashSet makes no guarantees as to the iteration order
of the set, it will be hard to do permutations on the set don't you
think?

You could use instead LinkedHashSet for instance.

The obvious implementation would be to copy the set into a list.

Tom Hawtin
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Collins schreef:
Hello All-

In the past I have used java pre generics for some programs related
to my hobbies. I have come back to it after a couple of years and am
biting the bullet and moving to 1.5(5.0)...

In the past I have made some type specific packages to generate some
basic combinatoric structures (permutations, combinations, partitions)

For my own use and education, I'm trying to figure out how to make this
more usfully with generics.

Specifically:

class foo {...}

class otherClass{
HashSet<foo> fooSet;

// now comes the tricky (to me) part

Iterator<???> perm = new permutation(fooSet);
/*
I'd like to have iterator return one permuation of the elements in
fooSet at a time
*/
}

Before you start implementing this, have a look here:
http://mindprod.com/jgloss/permutation.html. You might want to change
the BigIntegers to ints if you care for efficiency rather than infinite
correctness.

H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFFUFx7e+7xMGD3itQRAloxAJ9TVdTOwBBqJ8cOEzACkPVbd5MRwwCeI+3u
dsxOm3l/HIeeAvTRZiv+18k=
=RMyD
-----END PGP SIGNATURE-----
 
C

Chris Uppal

Thomas said:
The obvious implementation would be to copy the set into a list.

I'd think an array would be better. The performance might not be much better
but the code would be a lot clearer.

(I speak from too much sad experience -- coding even moderately complicated
array-oriented algorithms without the benefit of array syntax is painful.)

-- chris
 
T

Thomas Hawtin

Chris said:
I'd think an array would be better. The performance might not be much better
but the code would be a lot clearer.

(I speak from too much sad experience -- coding even moderately complicated
array-oriented algorithms without the benefit of array syntax is painful.)

Certainly lack of operator overloading can make code look really nasty.
The other week I wrote some code using BigIntegers. Yuck. However, I
don't think it should really be much of a problem here.

The real problem is ye olde arrays don't mix well with generics. It
needs either an Object array and (T)set.get(i), or (T[])set.toArray().
Stick with Dr Bloch's collections, and everything will be fine.

Tom Hawtin
 
C

Chris Uppal

Thomas Hawtin wrote:

[me:]
Certainly lack of operator overloading can make code look really nasty.
The other week I wrote some code using BigIntegers. Yuck. However, I
don't think it should really be much of a problem here.

It depends on the algorithms you are trying to express. My own code in this
sort of area (permutations, combinations, and whatnot) is rather array heavy.
I know -- 'cos it's written in a language which lacks C-style array access
syntax (albeit not as bad as Java Collections) -- that the lack of array syntax
hurts badly.

The real problem is ye olde arrays don't mix well with generics. It
needs either an Object array and (T)set.get(i), or (T[])set.toArray().
Stick with Dr Bloch's collections, and everything will be fine.

If it comes down to a choice between writing code of adequate clarity, and
using generics, then I'm afraid that generics will loose every time -- no
contest at all. If Dr Bloch's Collections are so tangled up with generics that
they have to be chucked out too, then so be it.

There are plenty of cases where ArrayLists are to be preferred to raw arrays, I
agree, but this is most definitely not one of them.

-- chris
 

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

Similar Threads

generics puzzle 57
can this be done with generics? 32
Generics and for each 12
Generic generics help 7
subclassing and generics 19
EMPTY_SET.iterator() and generics 12
Generics and reflection 2
Generics question 1

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top