Home > Back-end > Method how to pass the return value of the generic?
Method how to pass the return value of the generic?
Time:12-07
Methods: Public static & lt; K, V> List CollectListByValueMethod (List The list of String keyMethodName Class Clazz) { List Result=new ArrayList (a); If (a list!=null) { Try { Method keyMethod=clazz. GetMethod (keyMethodName); For (V item: the list) { Result. The add ((K) keyMethod. Invoke (item)); } } the catch (Exception e) { The log. The error (um participant etMessage ()); } } return result; } Call:
Public class demo19 {
Public static void main (String [] args) { The Person person1=new Person (1, "li", 20, false); The Person person4=new Person (2, "ol", 25, false); The Person person7=new Person (3, "zhang", 30, true); List List=new ArrayList<> (a); List. The add (person1); List. The add (person4); List. The add (person7);
List Result=CollectionUtils. CollectListByValueMethod (list, "getId", the Person class);
System.out.println(result);
} }
GetId the return value is an Integer, the result expected return value is a collection of Long, but the reality is of type Integer, should be how to modify the result of actual generic for Long?
CodePudding user response:
If you want to set the type of the return value is the K on the parameters of the inside also to call can declare the