java - Generic method accepts different data type list -


i want make method accepts data type list. accepting same data type list. have done below

@safevarargs public static <e> list<e> mergearray(list<e> ...list) {         list<e> result = new arraylist<e>();          for(list<e> temp : list) {             result.addall(temp);          }         return result;      }      public static void main(string[] args1) {         list<string> l1 = arrays.aslist("hello", "world");          list<double> l2 = arrays.aslist(2.3, 2.2, 4.5);          list<integer> l3 = arrays.aslist(1, 2,3);          list<string> l4 = arrays.aslist("hello1", "world1");          system.out.println(mergearray(l1, l2));     } 

here method mergearray accepts mergearray(l1, l4) //same data type.
if passed different data type mean mergearray(l1,l2) or mergearray(l1,l2,l3) doesn't work.
possible do?

it's possible, i'm not sure it's useful. here's way bounded wildcard type parameters, though, doesn't require return type list<object>:

public static <e> list<e> mergearray(@suppresswarnings("unchecked") list<? extends e> ...list) {     list<e> result = new arraylist<e>();      for(list<? extends e> temp : list) {         result.addall(temp);      }     return result;  } 

Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -