mix generic type variables to implement a type-safe map function in Java -
I want to type in a type-safe map
method in Java that gives the same archive logic Type in the form of logic (i.e. Arrayist, Linkedist, Treceet, etc.), but with another normal type (between the Anglaise bracket), the other parameters set by the generic type (as a result of the usual mapping function) is done. / P>
will be used as a code:
Public Interface transformfactory & lt; S, R & gt; {Public R applicable (S source object); } Transformfactory i2s = New Transform Factor & lt; Integer, string & gt; () {String applied (integer i) {return i.toString ();}; ArrayList & LT; Integer & gt; Ali = New Arrestist & lt; Integer & gt; (Arrays.asList (1, 2, 3, 4)); ArrayList & LT; String & gt; Als = map (ali, i2s); TreeSet & LT; Integer & gt; Tsi = new tree set & lt; Integer & gt; (Arrays.asList (1, 2, 3, 4)); TreeSet & LT; String & gt; Tss = map (TSI, I2S);
This idea will be something like this:
public stable ) collection.getClass (). NewInstance (); For (s i: archive) {result.add (f.apply (i)); } Return results; }
But that does not work because the compiler is not expecting normal type variables and more special (I think).
How to make any idea this work?
AFAIK Java has no way to do this, so that both of them (A) compile type-safe And (b) the caller of map
is not required to replicate the source and target collection type. Java does not support what you are asking for can be found in Scala 2.8, but there are also some complicated implementation details.
Comments
Post a Comment