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

CodePudding user response:

what is a bank reference 1 floor 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
I put Class KClass passed as a parameter, called put here is Long. The class, but in the end is still unable to get a collection of Long expected

CodePudding user response:

You look at my code can run normally

Public class adsadas {
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=collectListByValueMethod (list, "getId", the Person class);

System.out.println(result);

}
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) {
e.printStackTrace();
}
}
return result;
}
Static class Person {
Private long id;
Public Person (long I, String li, int i1, Boolean b) {
Id=I;
}

Public long getId () {
return id;
}
}
}

CodePudding user response:

refer to the blue sky, 4/f, ywj response:
you can see my code under the normal operation of the

Public class adsadas {
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=collectListByValueMethod (list, "getId", the Person class);

System.out.println(result);

}
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) {
e.printStackTrace();
}
}
return result;
}
Static class Person {
Private long id;
Public Person (long I, String li, int i1, Boolean b) {
Id=I;
}

Public long getId () {
return id;
}
}
}


My getId (), the return value is the Integer


Public class Person {
private Integer id;
private String name;
Private int the age;
Private Boolean home;

Public Person () {
}

Public Person (Integer id, String name, int the age, Boolean home) {
This. Id=id;
this.name=name;
this.age=age;
This. Home=home;
}

Public Integer getId () {
return id;
}

Public void setId (Integer id) {
This. Id=id;
}

Public String getName () {
return name;
}

Public void elegantly-named setName (String name) {
this.name=name;
}

Public int getAge () {
return age;
}

Public void setAge (int age) {
this.age=age;
}

Public Boolean isHome () {
Return home. nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related