package url; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class Ambalance { /* * @Description: 根据配置的参数删除在list中留取重复的数据个数,比如查出重复数据有6个,动态配置了只留3个, * 那么就在这重复的数中只保留3个,默认是只保留一个去重。 * @Author: huangdx3 * @CreateDate: 2018/10/26 11:41 * @UpdateRemark: 修改内容 * @Version: 1.0 * @ClassName:url.Ambalance * @param:[args] * @Return: void */ public static void main(String[] args) throws Exception { List list = new ArrayList(); Map busiMap = new HashMap(); busiMap.put("RECEIVE_NO","15177296752"); list.add(busiMap); Map busiMap1 = new HashMap(); busiMap1.put("RECEIVE_NO","15177296752"); list.add(busiMap1); Map busiMap2 = new HashMap(); busiMap2.put("RECEIVE_NO","15978172494"); list.add(busiMap2); Map busiMap3 = new HashMap(); busiMap3.put("RECEIVE_NO","15177296752"); list.add(busiMap3); Map busiMap4 = new HashMap(); busiMap4.put("RECEIVE_NO","15177296752"); list.add(busiMap4); Map busiMap5 = new HashMap(); busiMap5.put("RECEIVE_NO","15277129548"); list.add(busiMap5); Map busiMap6 = new HashMap(); busiMap6.put("RECEIVE_NO","15177296752"); list.add(busiMap6); Map busiMap7 = new HashMap(); busiMap7.put("RECEIVE_NO","15177296752"); list.add(busiMap7); Map busiMap8 = new HashMap(); busiMap8.put("RECEIVE_NO","15977723710"); list.add(busiMap8); Map busiMap9 = new HashMap(); busiMap9.put("RECEIVE_NO","15978172494"); list.add(busiMap9); int times = 5; //配置同一个号码一天可以发送多少次短信调查 if (times>1){ for (int i = 0; i<list.size()-1;i++){ int count = 1; //统计重复数总数 int temp = 0; //重复数key的下标 Map indexMap = new HashMap(); List indexList = new ArrayList(); for (int j = list.size()-1; j>i;j--){ String receiveNo1 = ((Map) (list.get(i))).get("RECEIVE_NO").toString(); String receiveNo2 = ((Map)(list.get(j))).get("RECEIVE_NO").toString(); if (receiveNo1.equals(receiveNo2)){ count++; indexMap.put(temp,j); //把最大的下标索引放在第一个位置 indexMap.put(++temp,i); } } indexList.add(indexMap); if (count>times){ //统计重复的数据多过配置要留的数据,把剩余的删除 if (indexList.size()>0 && indexList != null){ for (int k=0;k<(count-times);k++){ int index= Integer.parseInt (((Map) (indexList.get(0))).get(k).toString()); list.remove(index); //因为list是内存地址是堆栈存放,删除要从最大的下标索引删除 } } } } }else { //默认只保留一个去重 for (int i = 0; i<list.size()-1;i++){ for (int j = list.size()-1; j>i;j--){ String receiveNo1 = ((Map) (list.get(i))).get("RECEIVE_NO").toString(); String receiveNo2 = ((Map)(list.get(j))).get("RECEIVE_NO").toString(); if (receiveNo1.equals(receiveNo2)){ list.remove(j); } } } } System.out.println("配置只允许留"+times+"个重复数据:"); for (int i = 0 ;i<list.size();i++){ System.out.println("第"+(i+1)+"行:"+((Map) (list.get(i))).get("RECEIVE_NO").toString()); } } }