当前位置:网站首页>list类型转String类型
list类型转String类型
2022-07-20 05:30:00 【活学编程思想】
list类型转String类型
提出问题:
在做视频批量删除的时候,以List的形式把多个视频id传进来的时候,
其中这个方法request.setVideoIds(id);中的参数可以接受多个:request.setVideoIds(id1,id2,id3,…);
那么问题来了,传进来的List参数是这样的:[id1,id2,id3] ,怎么把List转成 id1,id2,id3 的形式
方法一:使用StringUtils.join()方法
1、思路:
根据 import org.apache.commons.lang3.StringUtils; 这个包下的 StringUtils.join() 方法,不是传统spring 中的
2、代码展示
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("11");
list.add("22");
list.add("33");
String join = StringUtils.join(list.toArray(), ",");
System.out.println(join);
}
结果:
11,22,33
活学编程思想
3、源码分析
不能只使用 StringUtils.join() 方法,而且要明白其中的原理,那么 ctrl+左键(点击join方法)看方法源码:
public static String join(Object[] array, String separator) {
return array == null ? null : join((Object[])array, separator, 0, array.length);
}
public static String join(Object[] array, String separator, int startIndex, int endIndex) {
if (array == null) {
return null;
} else {
if (separator == null) {
separator = "";
}
int noOfItems = endIndex - startIndex;
if (noOfItems <= 0) {
return "";
} else {
StringBuilder buf = newStringBuilder(noOfItems);
if (array[startIndex] != null) {
buf.append(array[startIndex]);
}
for(int i = startIndex + 1; i < endIndex; ++i) {
buf.append(separator);
if (array[i] != null) {
buf.append(array[i]);
}
}
return buf.toString();
}
}
}
源码具体说明
1、join() 方法参数两个,一个是数组,一个是
边栏推荐
- Graduation season -- use technology of various application scenarios
- A. Subtle Substring Subtraction
- 记一次笔记:Oracle条件语句
- js 密码组合规则-8-16位数字和字符组合,不能纯数字、纯英文
- B. Difference of GCDs
- Oracle按中文排序
- vscode拷贝 同步插件 拓展
- P1020 [noip1999 popularization group] missile interception problem solution
- ZigBee safety overview
- Oracle implements the limit effect: the use of rownum
猜你喜欢
【学习笔记之菜Dog学C】初识常见关键字、#define定义常量和宏
从输入URL到页面展示
SIGIR‘22 推荐系统论文之对比学习篇
[dish of learning notes dog learning C] chain access, function declaration and definition, goto statement
Ctfshow web entry information collection WP (1-20) (detailed explanation)
冒泡排序/堆排序
The practical operation of multi category risk scoring data helps you stabilize your small, medium and micro businesses
【学习笔记之菜Dog学C】循环语句
【个人总结】2022.7.17周结
【学习笔记之菜Dog学C】链式访问、函数的声明和定义、goto语句
随机推荐
Apache Doris Oracle ODBC appearance User Guide
Matplotlib教程(一)【初识Matplotlib】
Learn about spark project on nebulagraph
easyExcel设置最后一行的样式【可以拓展为每一行】
问题 B: 蓝桥杯2020年第十一届省赛真题-回文日期
【个人总结】2022.7.17周结
Jupyter notebook如何更换主题、更改字体大小?
mknod命令:创建设备文件
冒泡排序/堆排序
Matlab digital image processing experiment 5: morphological image processing
Mknod command: creating device files
【学习笔记之菜Dog学C】数据存储
试题 D: 修剪灌木
B. All Distinct
【学习笔记之菜Dog学C】数组
Ctfshow web entry information collection WP (1-20) (detailed explanation)
[scientific literature measurement] keyword mining and visualization
[dish of learning notes dog learning C] if branch statement and switch branch statement
使用GO语言通过Stream Load实现Doris数据导入
一致性哈希,虚拟节点,布隆过滤器