当前位置:网站首页>将list集合的某一字段拼接单个String
将list集合的某一字段拼接单个String
2022-07-25 09:16:00 【赞赞儿_】
将list集合的某一字段拼接单个String
有2种方法
- 循环(lambad表达式方法)
- 递归的方式将list集合的某一字段拼接单个String
List<Employee> emps = Arrays.asList(
new Employee(102, "李四", 59, 6666.66, Employee.Status.BUSY),
new Employee(101, "张三", 18, 9999.99, Employee.Status.FREE),
new Employee(103, "王五", 28, 3333.33, Employee.Status.VOCATION)
);
//将list集合的某一字段拼接单个String
@Test
public void testList(){
if(emps != null && emps.size() >=0){
//第1种 lambad表达式方法
String s= emps.stream().map(p->p.getName()).collect(Collectors.joining(","));
System.out.println(s);
//第2种 递归的方式将list集合的某一字段拼接单个String
String loopJoint = loopJointASC(emps, emps.size());
System.out.println(loopJoint);
}
}
public static String loopJointASC(List<Employee> list, int size) {
if(list.size()<=0) return null;
if(size==1) return list.get(list.size()-1).getName();
String joinStr = ",";
String name = list.get(list.size()-size).getName();
size--;//下标值和递归次数减少1
String loopJoint = loopJointASC(list, size);
return name+joinStr+loopJoint;
}
运行结果
边栏推荐
- Feiling ok1028a core board adapts to rtl8192cu WiFi module
- Sort out Huawei ap-3010dn_ V2 configuration create WiFi
- 酷炫canvas动画冲击波js特效
- 【线程知识点】-- 自旋锁
- (self drawn ugly picture) simple understanding tcp/ip three handshakes and four waves
- [STL]list模拟实现
- ActiveMQ -- JDBC code of persistent mechanism
- activemq--延迟投递和定时投递
- Ctfhub skill tree Web
- What version of Oracle10g single instance database is better to upgrade to? Ask for suggestions
猜你喜欢

Table table expansion internal row switching effect

js小游戏源码魔塔闯关下载

Disable module (attribute node) in LabVIEW

【Nacos】NacosClient在服务注册时做了什么

Feiling ok1028a core board adapts to rtl8192cu WiFi module

PL/SQL工具导出sql文件所使用的命令是什么?

超赞的yolo目标检测训练所用垃圾分类数据集共享——标注好的约3000张

Oracle10g单实例数据库升级到哪个版本好,求建议

OpenCV实现简单的人脸追踪

Programmers can't SQL? Ashes Engineer: all waiting to be eliminated! This is a must skill!
随机推荐
Disable module (attribute node) in LabVIEW
PL/SQL工具导出sql文件所使用的命令是什么?
redis操作利用游标代替keys
[BUUCTF-n1book][第二章 web进阶]SSRF Training
JDBC快速入门
js小游戏源码魔塔闯关下载
(self drawn ugly picture) simple understanding tcp/ip three handshakes and four waves
Probe into Druid query timeout configuration → who is the querytimeout of datasource and jdbctemplate effective?
Labview--- signal generator
Neural network learning (1) Introduction
Sticky.js page scrolling div fixed position plug-in
Django4.0 + web + MySQL 5.7 realize simple login operation
[STL]list模拟实现
Write two channel (stereo) immediately Wav file
Illustration leetcode - 919. Complete binary tree inserter (difficulty: medium)
Shell脚本
ActiveMQ -- JDBC code of persistent mechanism
OmniPeek packet capturing tool
Oracle10g单实例数据库升级到哪个版本好,求建议
Additional: SQL statement area / county in the lower half (data table)