Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

new to generics

Thread view: 
Collins - 06 Nov 2006 20:03 GMT
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
Thomas Hawtin - 06 Nov 2006 20:26 GMT
>   HashSet<foo> fooSet;
>
[quoted text clipped - 6 lines]
>   */
> }

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
Jean-Francois Briere - 06 Nov 2006 20:43 GMT
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.
Thomas Hawtin - 07 Nov 2006 07:34 GMT
> 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
Chris Uppal - 07 Nov 2006 13:29 GMT
> > You could use instead LinkedHashSet for instance.
>
> 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
Thomas Hawtin - 07 Nov 2006 15:40 GMT
>>> You could use instead LinkedHashSet for instance.
>> The obvious implementation would be to copy the set into a list.
[quoted text clipped - 4 lines]
> (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
Chris Uppal - 07 Nov 2006 16:05 GMT
[me:]
> > I'd think an array would be better.  The performance might not be much
> > better but the code would be a lot clearer.
[quoted text clipped - 6 lines]
> 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
Hendrik Maryns - 07 Nov 2006 10:14 GMT
Collins schreef:
> Hello All-
>
[quoted text clipped - 23 lines]
>   */
> }

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.