【java8】获取指定方法名的结果集

public String getFieldData(List<?> list, String declaredMethod) {
  List<String> result = list.stream().map(r -> {
    try{
      return r.getClass().getDeclaredMethod(declaredMethodName).invoke(r);
    } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
      log.error("获取方法失败", e);
      return null;
    }
  }).filter(Objects::nonNull).map(r -> (String) r).collect(Colletors.toList());
  return String.join(",", list);
}
posted @ 2021-10-26 17:49  沐晨烟雨  阅读(71)  评论(0)    收藏  举报