zl程序教程

您现在的位置是:首页 >  后端

当前栏目

逗号拼接字符串转list[通俗易懂]

List 通俗易懂 字符串 拼接 逗号
2023-06-13 09:11:32 时间

大家好,又见面了,我是你们的朋友全栈君。

将一个用逗号分隔的字符串转为list,代码如下:
//普通拼接方式
String str="aa,aaaa,ddd,ads,sasd";
Type type = new TypeToken<List<String>>() {}.getType();
List<String> list= new Gson().fromJson(str, type);

//java8流式拼接
//1.
//comRecInfoList是对象list    filter()中是筛选条件
comRecInfoList.stream().filter(rec -> rec.getOrgid().equals(orgId)).collect(Collectors.toList());
//2.
Object[] objects = list.stream().map(OrgInternal::getId).toArray();

orgId = StringUtils.join(objects, ",");


//mybatis中使用
AND r.orgid in
<foreach close=")" collection="list" index="index" item="item" open="(" separator=",">
  #{item}
</foreach>

个人记录常用代码。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/146596.html原文链接:https://javaforall.cn