当前位置:网站首页>将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;
}
运行结果
边栏推荐
- Overview of redis/mysql knowledge
- Table table expansion internal row switching effect
- Troubleshooting error: NPM install emojis list failed
- @Scheduled源码解析
- Programmers can't SQL? Ashes Engineer: all waiting to be eliminated! This is a must skill!
- leetcode-238.除自身以外数组的乘积
- 酷炫canvas动画冲击波js特效
- 艺术 NFT 的发展之路
- PHP date() function does not support processing numbers greater than 2147483648? "Suggested collection"
- Comparison between symmetric encryption and asymmetric encryption
猜你喜欢

实现简单的RESTful API服务器

【npm】 无法将“npm”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。

Live broadcast preview | how to build an enterprise cloud management platform in the cloudy era?

Nacos启动报错Unable to start web server

『每日一问』LockSupport怎么实现线程等待、唤醒

activemq--死信队列

Illustration leetcode - 1184. Distance between bus stops (difficulty: simple)

全网最简约的sklearn环境配置教程(百分百成功)

ActiveMQ -- JDBC code of persistent mechanism

Guangzhou has carried out in-depth "100 day action" to check the safety of self built commercial houses, and more than 2 million houses have been checked in two months
随机推荐
activemq--可持久化机制
Composition of the interview must ask items
JDBC quick start
flink sql怎么持久化?
JS pop-up City filtering component matches mobile terminal
Redis-哨兵,主从部署详细篇
[arm] Xintang nuc977 transplants wk2124 drive
JS small game source code magic tower breakthrough Download
c语言中的六个存储类型:auto register static extern const volatile
Six storage types in C language: Auto register static extern const volatile
[buuctf-n1book][Chapter 2 advanced web]ssrf training
JDBC的API解析
有误差的字符串型时间比较方法String.compareTo
[C language] dynamic memory management, flexible array
OpenCV实现简单的人脸追踪
实现简单的RESTful API服务器
[deep learning] overview | the latest progress of deep learning
[deep learning] mask Dino Trilogy - the correct way to open Detr Pandora's box
Sort out Huawei ap-3010dn_ V2 configuration create WiFi
Unity ugui interaction (new ideas)