CollUtil.java 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package com.lqbz.common.utils;
  2. import cn.hutool.core.collection.CollectionUtil;
  3. import cn.hutool.core.util.StrUtil;
  4. import java.util.List;
  5. /**
  6. * 集合工具类
  7. *
  8. * @author zuoyou
  9. * @date 2024/02/26 10:04
  10. */
  11. public class CollUtil extends CollectionUtil {
  12. /**
  13. * 获取指定值的下标
  14. *
  15. * @param list 集合
  16. * @param target 目标值
  17. * @return
  18. */
  19. public static int getIndex(List<String> list, String target) {
  20. for (int i = 0; i < list.size(); i++) {
  21. if (list.get(i).equals(target)) {
  22. return i;
  23. }
  24. }
  25. return -1;
  26. }
  27. /**
  28. * 获取指定值的下标(倒叙)
  29. *
  30. * @param list 集合
  31. * @param target 目标值
  32. * @return
  33. */
  34. public static int getListIndex(List<String> list, String target) {
  35. for (int i = list.size() - 1; i >= 0; i--) {
  36. if (list.get(i).equals(target)) {
  37. return i;
  38. }
  39. }
  40. return -1;
  41. }
  42. /**
  43. * 集合转字符串
  44. *
  45. * @param list 集合
  46. * @param sep 分隔符
  47. * @return
  48. */
  49. public static String list2Str(List<String> list, String sep) {
  50. StringBuffer stringBuffer = new StringBuffer();
  51. for (String s : list) {
  52. stringBuffer.append(s).append(sep);
  53. }
  54. String str = stringBuffer.toString();
  55. int i = StrUtil.lastIndexOf(str, sep, str.length(), false);
  56. return StrUtil.sub(str, 0, i);
  57. }
  58. }