1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- package com.lqbz.common.utils;
- import cn.hutool.core.collection.CollectionUtil;
- import cn.hutool.core.util.StrUtil;
- import java.util.List;
- /**
- * 集合工具类
- *
- * @author zuoyou
- * @date 2024/02/26 10:04
- */
- public class CollUtil extends CollectionUtil {
- /**
- * 获取指定值的下标
- *
- * @param list 集合
- * @param target 目标值
- * @return
- */
- public static int getIndex(List<String> list, String target) {
- for (int i = 0; i < list.size(); i++) {
- if (list.get(i).equals(target)) {
- return i;
- }
- }
- return -1;
- }
- /**
- * 获取指定值的下标(倒叙)
- *
- * @param list 集合
- * @param target 目标值
- * @return
- */
- public static int getListIndex(List<String> list, String target) {
- for (int i = list.size() - 1; i >= 0; i--) {
- if (list.get(i).equals(target)) {
- return i;
- }
- }
- return -1;
- }
- /**
- * 集合转字符串
- *
- * @param list 集合
- * @param sep 分隔符
- * @return
- */
- public static String list2Str(List<String> list, String sep) {
- StringBuffer stringBuffer = new StringBuffer();
- for (String s : list) {
- stringBuffer.append(s).append(sep);
- }
- String str = stringBuffer.toString();
- int i = StrUtil.lastIndexOf(str, sep, str.length(), false);
- return StrUtil.sub(str, 0, i);
- }
- }
|