业务场景1: 现在有一个list 的集合 ,需要根据list 里面的主键id 作为参考去掉重复的列
使用map 去重复,map<key,value> 存储方式,其中key不能为重复列
List<CustInfo> custinfoList = list(一个集合);
Map<Long ,CustInfo> map = new HashMap<Long ,CustInfo>(); for(int i= 0;i<custinfoList.size();i++){
CustInfo custinfo = custinfoList.get(i);
map.put(custinfo.getId(),custinfo);
}
业务场景2:现有一个String 字符串contact=[{"linkmanname":"小明","linkmobilenumber":"1234567","linkposition":"经理"},{"linkmanname":"小红","linkmobilenumber":"1234567","linkposition":"主管"},{"linkmanname":"小蓝","linkmobilenumber":"1234567","linkposition":"主管"}]参数 要求联系人手机号去重
public String addSeeLog(Map<String, String> formMap) {
String message = "";
if (null != formMap.get("contact")) {
message += checkContact(formMap.get("contact"), message);
}
if (StringUtils.isNotBlank(message)) {
throw new BizException(CrmErrorCode.INPUT_PARAMETER_IS_ERROR, message);
}
}
/** 校验*/
private String checkContact(String contact, String message) {
Type contactType = new TypeToken<List<Map<String, String>>>() {
}.getType();
List<Map<String, String>> contactList = JsonUtils.GSON.fromJson(contact, contactType);
Map<String, Map<String, String>> map = new HashMap<String, Map<String, String>>();
//联系人职称,联系人姓名,联系人手机号不可为空
for (Map<String, String> contactParam : contactList) {
message += StringUtils.isNotBlank(contactParam.get("linkposition"), "联系人职称");
message += StringUtils.isNotBlank(contactParam.get("linkmanname"), "联系人姓名");
message += StringUtils.isNotBlank(contactParam.get("linkmobilenumber"), "联系人手机号");
}
//联系人手机号去重
for (int i = 0; i < contactList.size(); i++) {
Map<String, String> map0 = contactList.get(i);
map.put(map0.get("linkmobilenumber"), map0);
message = "联系人手机号(" + map0.get("linkmobilenumber") + ")重复!";
}
return message;
}
业务场景3【hashset去重】现有一个String 字符串contact=[{"linkmanname":"小明","linkmobilenumber":"1234567","linkposition":"经理"},{"linkmanname":"小红","linkmobilenumber":"1234567","linkposition":"主管"},{"linkmanname":"小蓝","linkmobilenumber":"1234567","linkposition":"主管"}]参数 要求联系人手机号去重
public String addSeeLog(Map<String, String> formMap) {
String message = "";
if (null != formMap.get("contact")) {
message += checkContact(formMap.get("contact"), message);
}
if (StringUtils.isNotBlank(message)) {
throw new BizException(CrmErrorCode.INPUT_PARAMETER_IS_ERROR, message);
}
}
/** 校验*/
private String checkContact(String contact, String message) {
转换成list map
Type contactType = new TypeToken<List<Map<String, String>>>() {
}.getType();
List<Map<String, String>> contactList = JsonUtils.GSON.fromJson(contact, contactType);
//new一个hashset
HashSet<String> set = new HashSet<String>();
for (Map<String, String> contactParam : contactList) {
message += StringUtils.isNotBlank(contactParam.get("linkposition"), "联系人职称");
message += StringUtils.isNotBlank(contactParam.get("linkmanname"), "联系人姓名");
message += StringUtils.isNotBlank(contactParam.get("linkmobilenumber"), "联系人手机号");
//联系人手机号去重
if (!set.add(contactParam.get("linkmobilenumber"))) {
message = "联系人手机号(" + contactParam.get("linkmobilenumber") + ")重复!";
}
}
return message;
}