当前位置:网站首页>将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;
}
运行结果
边栏推荐
- 这家十年内容产业基建公司,竟是隐形的Web3先行者
- activemq--可持久化机制之JDBC代码
- 『怎么用』代理模式
- Neural network learning (1) Introduction
- ActiveMQ -- kahadb of persistent mechanism
- Silicon Valley classroom lesson 12 - official account on demand course and live broadcast management module
- PL/SQL工具导出sql文件所使用的命令是什么?
- activemq--可持久化机制之JDBC的journal
- How does Youxuan database encrypt data?
- Anti shake and throttling
猜你喜欢

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

分布式一致性协议之Raft

有误差的字符串型时间比较方法String.compareTo

Silicon Valley classroom lesson 15 - Tencent cloud deployment

Arrange the array into the smallest number

js弹出式城市筛选组件匹配手机移动端
![[SCADA case] myscada helps VIB company realize the modernization and upgrading of production line](/img/67/b8c397d78a675014b5e08ceefc88dc.png)
[SCADA case] myscada helps VIB company realize the modernization and upgrading of production line

What are the commands used by pl/sql tools to export SQL files?
![[deep learning] mask Dino Trilogy - the correct way to open Detr Pandora's box](/img/5c/52ccc0583451eba15fec18adb1499e.png)
[deep learning] mask Dino Trilogy - the correct way to open Detr Pandora's box

Network principle (2) -- network development
随机推荐
API parsing of JDBC
Arrange the array into the smallest number
【线程知识点】-- 自旋锁
[deep learning] mask Dino Trilogy - the correct way to open Detr Pandora's box
Collection of common algorithm questions in test post interview
Cool canvas animation shock wave JS special effect
mysql中的数据结果排名
Asp. Net core CMD common instructions
The annualization of financial products is 4%. How much profit can you get from buying 10000 yuan a month?
table表格展开内部行切换效果
图解LeetCode——1184. 公交站间的距离(难度:简单)
黑马程序员JDBC
360 degree drag panorama plug-in tpanorama.js
sql注入
sticksy.js页面滚动div固定位置插件
[buuctf-n1book][Chapter 2 advanced web]ssrf training
Labview--- signal generator
Activemq-- asynchronous delivery
Silicon Valley classroom lesson 15 - Tencent cloud deployment
How does Youxuan database encrypt data?