一个List<String>去重的demo

package com.string.test;
import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class TTTT {

    public static void main(String[] args) {
        
        List<String> dnatIp=new ArrayList<String>();
        List<String> eipIp=new ArrayList<String>();
        List<String> snatIp=new ArrayList<String>();
        dnatIp.add("10.163.162.72");
        dnatIp.add("10.163.162.72");
        dnatIp.add("10.163.162.86");
        dnatIp.add("10.163.162.86");
        dnatIp.add("10.163.162.86");
        dnatIp.add("10.163.162.74");
        dnatIp.add("10.163.162.74");
        dnatIp.add("10.163.162.86");
        dnatIp.add("10.163.162.81");
        dnatIp.add("10.163.162.81");
        dnatIp.add("10.163.162.80");
        dnatIp.add("10.163.162.80");
        dnatIp.add("10.163.162.83");
        dnatIp.add("10.163.162.83");
        dnatIp.add("10.163.162.83");
        
        eipIp.add("10.163.162.72");
        eipIp.add("10.163.162.81");
        eipIp.add("10.163.162.86");
        
        snatIp.add("10.163.162.74");
        
        //若dnat有多个,只保留一个
        dnatIp=removeSelf(dnatIp);
        //若同时有eip和dnat,那么删除dnat里面的ip
        dnatIp=removeDuplicate(dnatIp, eipIp);
        //若同时有dnat和snat,删除dnat里面的ip
        dnatIp=removeDuplicate(dnatIp, snatIp);
        
        System.out.println("dnatIp="+dnatIp);
        System.out.println("eipIp="+eipIp);
        System.out.println("snatIp="+snatIp);
    }
    
    
    public static List<String> removeDuplicate(List<String> dupList,List<String> nomalList){
        //若存在相同的元素,删除dupList里面的
        if(nomalList!=null&&nomalList.size()!=0&&dupList!=null&&dupList.size()!=0){
            for(int i=0;i<nomalList.size();i++){
                for(int j=0;j<dupList.size();j++){
                    if(dupList.get(j).equals(nomalList.get(i))){
                        dupList.set(j, "");
                    }
                }
            }
        }
        
        while(dupList.indexOf("")!=-1){
            dupList.remove(dupList.indexOf(""));
        }
        
        return dupList;
    }
    
    public static List<String> removeSelf(List<String> dupList){
        //若存在相同的元素,删除
        if(dupList!=null&&dupList.size()!=0){
            for(int i=0;i<dupList.size();i++){
                for(int j=i+1;j<dupList.size();j++){
                    if(dupList.get(j).equals(dupList.get(i))){
                        dupList.set(j, "");
                    }
                }
            }
        }
        while(dupList.indexOf("")!=-1){
            dupList.remove(dupList.indexOf(""));
        }
        return dupList;
    }
}

 

注意:去处重复不要用remove方法,这样会导致代码复杂度上升。直接替换为“”,后续pili批量替换即可。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值