java - Insert object into List wildcard extending interface -
so have interface
public interface identity { abstract void setkey(string key); abstract string getkey(); }
that want use insert sort function
public static void insertidentity(identity identity, arraylist<? extends identity> list) { int = 0; identity id; while (i < list.size()) { if ((id = list.get(i)).getkey().compareto(identity.getkey()) < 0) { i++; }else{break;} } list.add(i, identity); }
but cannot add identity arraylist - ? extends identity
i wondering if there way around this?
i know java has other ways of doing task function work.
you make method generic:
public static <t extends identity> void insertidentity(t identity, arraylist<t> list) { //...
you may need make other methods generic well, if use approach.