1.String projectids="";
2.
if (StringHelper.isNotEmpty(gatheringinfo.getCol1()) && gatheringinfo.getCol1().length()>0){
projectids+=gatheringinfo.getCol1()+",";
}
3.
if (StringHelper.isNotEmpty(projectids)){
projectids = projectids.substring(0, projectids.length() - 1);
String delimiter = ",";
projectids = deduplicateIds(projectids, delimiter);
}
4.
public static String deduplicateIds(String idsString, String delimiter) {标签:java,String,HashSet,idsString,StringBuilder,projectids,id,中去 From: https://www.cnblogs.com/jk200/p/18354525
// 使用HashSet来去重,因为HashSet不允许重复元素
Set<String> uniqueIds = new HashSet<>();
// 假设idsString不为null且格式正确
if (idsString != null && !idsString.isEmpty()) {
// 使用split方法按分隔符分割字符串
String[] ids = idsString.split(delimiter);
// 将分割后的ID添加到HashSet中自动去重
for (String id : ids) {
// 去除可能的空白字符(如空格、换行等)
uniqueIds.add(id.trim());
}
}
// 使用StringBuilder来构建最终的字符串
StringBuilder result = new StringBuilder();
// 遍历HashSet中的元素,将它们添加到StringBuilder中
// 并在每个元素后添加分隔符(除了最后一个元素)
boolean first = true;
for (String id : uniqueIds) {
if (!first) {
result.append(delimiter);
}
result.append(id);
first = false;
}
// 将StringBuilder转换为字符串并返回
return result.toString();
}