R
Roedy Green
What is the cleanest way to do this conversion?
I have a Set<X>.
I want a Set<Y> where X implements Y.
I have a Set<X>.
I want a Set<Y> where X implements Y.
What is the cleanest way to do this conversion?
I have a Set<X>.
I want a Set<Y> where X implements Y.
Conversion of a Set<X> to a Set<? extends Y>, ensuring the Set contains
objects assignable to Y, but no new elements can be added to it:
Set<X> setOfX = obtainSetOfX();
Set<? extends Y> setOfY = setOfX;
This what I was looking for , that did not require an element by
element copy.
Ah, but this this you created is NOT a Set<Y>. You cannot add
arbirary Y to it, just more X. I feel queasy. How could the compiler
keep track that setOfY could only contain X.
On 02/02/2012 14:32, Roedy Green wrote:
It cannot.
It does not. Instead, it prevents you from any add operation whichThis what I was looking for , that did not require an element by
element copy.
Ah, but this this you created is NOT a Set<Y>. You cannot add
arbirary Y to it, just more X. I feel queasy. How could the compiler
keep track that setOfY could only contain X.
markspace said:I think this is covered in Effective Java. Generics are a compile
time thing. If you want runtime, you have to roll your own (I don't
know of any classes in the API that allow you to have a runtime type
parameter, although I guess there may be some).
class MySet extends SomeSet {
Class type;
MySet( Class type ) { this.type = type; }
void add( Object o ) {
if( o instanceof type ) super.add( o );
}
}
I think is the above is the gist of how EJ handles it. Add a type
token (the "type" variable) and test for types as they are added. If
you want generics too, add them in:
class MySet<T> extends Set<T> {
Class<? extends T> type;
MySet( Class<? extends T> type ) { this.type = type; }
void add( T o ) {
if( o instanceof type ) super.add( o );
}
}
Not compiled, but I think that gives the right idea.
See checkedSet in java.util.Collections.
I think this is covered in Effective Java. Generics are a compile time
thing
That is exactly what Generics are.Everything else in Java eventually seems obvious. Generics on the
other hand get weirder and weirder the more I learn. I think it was a
mistake to try to do generics purely at compile time. It is like
trying to do all types purely at compile time.
The point of generics is to add compile-time type safety. RuntimeHave we gone down that road too far now that Java can never be fixed
with run-time generics info without starting over with some completely
different notation? Then serialisation could work, containers could be
allocated with the precise array type. You would not have so much
under-the-hood casting.
Yes, probably.Perhaps it is time to read-read all the generics docs and see if they
make more sense now with some practical experience under my belt.
This conversion exactly as asked:
Set<X> setOfX = obtainSetOfX();
Set<Y> setOfY = new YourPreferredSetImplementation<Y>(setOfX);
Perhaps it is time to read-read all the generics docs and see if they
make more sense now with some practical experience under my belt.
thanks. I have written an SSCCE to explain this in tedious detail.
See http://mindprod.com/jgloss/generics.html#CONVERTINGSETS
Everything else in Java eventually seems obvious. Generics on the
other hand get weirder and weirder the more I learn. I think it was a
mistake to try to do generics purely at compile time. It is like
trying to do all types purely at compile time.
Have we gone down that road too far now that Java can never be fixed
with run-time generics info without starting over with some completely
different notation? Then serialisation could work, containers could be
allocated with the precise array type. You would not have so much
under-the-hood casting.
Perhaps it is time to read-read all the generics docs and see if they
make more sense now with some practical experience under my belt.
http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html
If you feel seriously that you need more explanation, try O'Reilly's
"Learning Java", third edition. It has one of the better explanations
of generics I've found. It's big book to buy just for the generics
section, but not really that much money in the grand scheme of things.
Can you imagine some other way to pay $40 - $50 and get someone to
explain a difficult programming concept? I can't.
markspace said:I think is the above is the gist of how EJ handles it. Add a type token
(the "type" variable) and test for types as they are added. If you want
generics too, add them in:
The point of generics is to add compile-time type safety. Runtime
"generics" is a contradiction to that.
Yes, I never said anything against that. I said generics were designedThat was the way it was decided to do it for Java.
But it did have to be that way.
And runtime check can certainly add to type safety!
Yes, I never said anything against that. I said generics were designed
to add compile-time type safety. Runtime type safety is a different issue.
Person A: Look at this green grass.Java generics was designed that way. There are nothing in the
generics concept that says it has to be that way.
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.