List<String> providerApps = providerService.findApplications();
// 要在providerApps中去掉重复的
List<String> tmp = new ArrayList<String>();
for (String provider : providerApps) {
if (!tmp.contains(provider)) {
tmp.add(provider);
// 这样就好了,以下该干嘛干嘛
......
......
}
}
更好的方法:直接将List放到Set中
public static void main(String[] args) {
List<String> apps = new ArrayList<String>();
apps.add("app1");
apps.add("app1");
apps.add("app2");
apps.add("app3");
Set<String> appset = new TreeSet<String>();
appset.addAll(apps);
List<String> tmp = new ArrayList<String>();
tmp.addAll(appset);
for(String s : tmp) {
System.out.println("###### " + s);
}
}
结果:
###### app1
###### app2
###### app3