您的位置:首页 > 编程语言 > Java开发

java对重复数据的处理操作(加上标识区分重复)

2013-10-09 10:13 281 查看
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class Main {

/**
* @param args
*/
public static void main(String[] args) {
Map map1 = new HashMap();
Map<String,Integer> map2 = new HashMap<String,Integer>();//存相同的value出现了几次
Map map3 = new HashMap();
map1.put("info0", "info1");
map1.put("info1", "info1");
map1.put("info2", "info3");
map1.put("info3", "info4");
map1.put("info4", "info5");
map1.put("info5", "info6");

Set key = map1.keySet();
Iterator it = key.iterator();
String[] a = new String[map1.size()];
int b = 0;
int count = 0;
while(it.hasNext()){
a[b++] = (String) map1.get(it.next());
}
for(int i = 0; i < a.length; i++){
String str = a[i];
for(int j = 0; j < a.length; j++){
if(str.equals(a[j])){
count++;
}
}
map2.put(str, count);//map里如果key相同 会自动覆盖之前的value
count = 0;//统计一次归一次0
}
Set key1 = map2.keySet();
Iterator it1 = key1.iterator();
while(it1.hasNext()){
String temp = (String) it1.next();
int mapCount = (int) map2.get(temp);
if(mapCount != 1){
for(int i = 1; i <= mapCount; i++){
map3.put(temp + "_" + i,temp + "("+i+")" );
}
}else{
map3.put(temp, temp);
}
}
System.out.println(map3);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐