1. StringUtils
1.逗号切割
for (Agentband agentband : agentbands) {
String str = agentband.getListIds();
if ("" != str && null != str) {
String[] list = str.split(",");
if (list.length > 0) {
for (String s : list) {
//if (null != s && "" != s) {
if (null != s && "" != s && !s.isEmpty() && s.length()>0) {
System.out.println("listIds:"+s);
listIds.add(Integer.parseInt(s));
}
}
}
}
}
2. List
1.递归寻找父类
/**
* 递归查询上级分类
* @param catid
* @param result
* @return
*/
private List getCatIds(List catid,List result){
if (CollectionUtils.isNotEmpty(catid)) {
List crmPrdCats = crmPrdCatMapper.selectByCIds(catid);
if (CollectionUtils.isNotEmpty(crmPrdCats)){
result.addAll(crmPrdCats);
List collect = crmPrdCats.stream().map(CrmPrdCat::getPcatId).collect(Collectors.toList());
result = getCatIds(collect, result);
}
}
return result;
}
2.对List集合去重
listIds.stream().distinct().collect(Collectors.toList())
3.遍历集合并且拼接
String xml =
"" +
people.stream()
.map(it -> "" + it.getLastName() + " ")
.reduce("", String::concat)
+ " ";
System.out.println(xml);
4.对集合进行过滤操作
@Test
public void strFilter(){
String str = " 进口红点不锈钢蔬菜刀刀具厨房用品防滑手柄 4034ZW不锈钢";
str = str.trim();
String[] s = str.split(" ");
List strings = Arrays.asList(s);
List collect = strings.stream().filter(blank -> !"".equals(blank)).collect(Collectors.toList());
StringBuffer sb = new StringBuffer();
for (String s2 :collect ) {
sb.append(s2+"_");
}
if (sb.length() > 0){
sb.deleteCharAt(sb.length()-1);
}
String substring = sb.toString();
System.out.println(substring);
}
@Test
public void strFilter2(List stu){
//对 对象进行过滤
stu.stream().filter(str-> "".equals(str.getName)).collect(Collections.toList()).forEach(System.out::print)
}
5.复制List集合
BeanCopierUtil.copyAsList(relationShipMapper.selectByConditionListVo(recordVo), SystemUserVO.class, false));
6.计算集合数值总和
@Test
public void sum() {
List values = Arrays.asList(1, 2, 3, 4, 5);
int sum = values.stream().reduce(0, (l,r) -> l+r);
System.out.println(sum);
}
7.List集合拼接成json对象集合 -> (list集合转json格式对象数值)
7.1实体类中
public static String toJSON(Employee p) {
return
"{" +
"firstName: \"" + p.getName() + "\", " +
"lastName: \"" + p.getSalary() + "\", " +
"age: " + p.age + " " +
"}";
}
7.2 调用
String json =
people.stream()
.map(Employee::toJSON)
.reduce("[", (l, r) -> l + (l.equals("[") ? "" : ",") + r)
+ "]";
System.out.println(json);
结果:
[{firstName: "田七", lastName: "7777.77", age: 27 },{firstName: "王五", lastName: "5555.55", age: 24 },{firstName: "张三", lastName: "3333.33", age: 23 },{firstName: "李四", lastName: "4444.44", age: 24 },{firstName: "赵六", lastName: "6666.66", age: 26 }]
8. 连接指定元素
/**
* 通过joining连接指定字段
*/
@Test
public void joining() {
List people = init();
String joined = people.stream()
.map(Employee::toJSON)
.collect(Collectors.joining(", "));
System.out.println("[" + joined + "]");
}
3.Map
1.map集合遍历
for (Map.Entry> entry : map.entrySet()) {
}
statisticGroup.entrySet().forEach(entry -> {})
2. 从集合中获取某个参数,单独组成一个集合
allCats.stream().map(CrmPrdCat::getCatId).collect(Collectors.toList());
3.将Map集合里面的值,同时添加到多个集合之中
List
4. 将list集合转换为Map集合
Map> goodsByGoodsCode = goodsList.stream().collect(Collectors.groupingBy(Goods::getGoodsCode));