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

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);
	}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值