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批量替换即可。