/**
* 将相同的选项值,改为不相同的选项值,用[]以示区分
* @param colKeys 选项值数组
*/
private static void updNameRepeatOptVal(String[] colKeys){
//找出相同的选项 key为字符串, value为次数, value>1就说明有相同的选项值,要做处理
Map<String,Integer> repeatKeyMap = new HashMap<String,Integer>();
for(int i=0; i<colKeys.length; i++){
if(repeatKeyMap.get(colKeys[i])==null){
repeatKeyMap.put(colKeys[i], new Integer(1));
}else{
repeatKeyMap.put(colKeys[i], new Integer(repeatKeyMap.get(colKeys[i]).intValue()+1));
}
}
//找出重复的key,并放到list中
List<String> repeatKeyList = new ArrayList<String>();
Iterator it = repeatKeyMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry)it.next();
String key = (String)entry.getKey();
Integer value = (Integer)entry.getValue();
if(value!=null){
if(value.intValue()>1){
repeatKeyList.add(key);
}
}
}
//对重复的key进行处理(改名)
for(int i=0; i<repeatKeyList.size(); i++){
int cnt = 0;
//记录重复位置的下标
Map<String,Integer> postMap = new HashMap<String,Integer>();
for(int j=0;j<colKeys.length; j++){
if(repeatKeyList.get(i).equals(colKeys[j])){
postMap.put(""+cnt, j);
cnt++;
}
}
//修改名称
for(int k=0; k<cnt; k++){
Integer post = postMap.get(k+"");
colKeys[post.intValue()] = colKeys[post.intValue()]+"["+k+"]";
}
}
}
处理数组中重复的数据
最新推荐文章于 2020-11-19 11:10:01 发布