Java泛型

泛型方法举例

  1. /**
  2. * 根据产品代码、基金代码获取基金信息
  3. *
  4. * @param queryList 查询列表
  5. * @return
  6. */
  7. public List<? extends ProdNameBO> getProdName(List<? extends ProdNameBO> queryList) {
  8. return queryList;
  9. }
  10. /**
  11. * 分页查询
  12. *
  13. * @param list 列表
  14. * @param startRow 起始行数 从0开始
  15. * @param limit 每次查询的行数
  16. * @return 子列表
  17. */
  18. public static <T> List<T> pageQuery(List<T> list, Integer startRow, Integer limit) {
  19. if (startRow < 0 || limit < 0) {
  20. return Collections.emptyList();
  21. }
  22. if (list.isEmpty() || startRow > list.size()) {
  23. return Collections.emptyList();
  24. }
  25. return list.size() > startRow + limit ? list.subList(startRow, startRow + limit) : list.subList(startRow, list.size());
  26. }
  27. /**
  28. * 源对象属性拷贝生成目标类型对象
  29. *
  30. * @param source 源对象
  31. * @param target 目标class对象
  32. * @param <T> 目标对象
  33. * @return
  34. */
  35. public static <T> List<T> convertList(List<?> source, Class<T> target) {
  36. if (source == null || target == null) {
  37. return null;
  38. }
  39. return source.stream().map(t-> BeanCovertUtils.convert(t, target)).collect(Collectors.toList()) ;
  40. }