参考 ianly梁炎 url:https://blog.youkuaiyun.com/ianly123/article/details/82658622
1、String中某个字符出现次数:
public static int strCount(String str,String findByStr){
String[] split = str.split("");
return Arrays.asList(split).stream().filter(s -> s.equals(findByStr)).collect(Collectors.toList()).size();
}
2、List<String>去除重复元素:
public static List<String> removeRepEle(List<String> list){
return list.stream().distinct().collect(Collectors.toList());
}
eg:Object 对象
package com.ouyeel.platform.components.rzcore.foundation.service.csy.vo;
import java.io.Serializable;
/**Object对象*/
public class PaFlowInfo implements Serializable {
private static final long serialVersionUID = 1L;
private String serialNo;
private String remark;
public String getSerialNo() {
return serialNo;
}
public void setSerialNo(String serialNo) {
this.serialNo = serialNo;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
}
3、List<Object>筛选符合条件的对象
public static List<PaFlowInfo> getConditionList(List<PaFlowInfo>list){
return list.stream().filter(paFlowInfo->paFlowInfo.getSerialNo().equals("serialNo_1")).collect(Collectors.toList());
}
4、List<Object>根据对象属性(一个),去除重复对象
public static List<PaFlowInfo> removeRepEleByOne(List<PaFlowInfo> list){
return list.stream().collect(
Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(PaFlowInfo::getSerialNo))), ArrayList::new)
);
}
5、List<Object>根据对象属性(两个),去除重复对象
public static List<PaFlowInfo> removeRepEleByTwo(List<PaFlowInfo> list){
return list.stream().collect(
Collectors. collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(paFlowInfo -> paFlowInfo.getSerialNo() + ";" + paFlowInfo.getRemark()))), ArrayList::new)
);
}
6、List<Object>根据两个属性转换为Map集合(作为key的属性不可重复)
public static Map<String,String> transformMap(List<PaFlowInfo> list){
return list.stream().collect(Collectors.toMap(paFlowInfo -> paFlowInfo.getSerialNo(), paFlowInfo -> paFlowInfo.getRemark()));
}
7、从 List<Object> 中取出某个属性的组成 list 集合
public static List<String> assemblyNewList(List<PaFlowInfo>list){
return list.stream().map(PaFlowInfo::getSerialNo).collect(Collectors.toList());
}
8、从 List<Object> 中取出某个属性的组成 list 集合 并去重
public static List<String> assemblyNewListRemoveRep(List<PaFlowInfo>list){
return list.stream().map(PaFlowInfo::getSerialNo).distinct().collect(Collectors.toList());
}
9、删除map中value 为null的键值
public static void removeNull(Map <String,Object>map){
map.entrySet().removeIf(entry -> entry.getValue() == null);
}