import java.util.ArrayList;
import java.util.LinkedHashSet;
public class Test {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("AAA");
arrayList.add("BBB");
arrayList.add("CCC");
arrayList.add("DDD");
arrayList.add("BBB");
arrayList.add("AAA");
System.out.println("去重前:" + arrayList);
ArrayList<String> arrayList2 = new ArrayList<>(new LinkedHashSet<>(arrayList));
System.out.println("去重后:" + arrayList2);
}
}
运行结果:
去重前:[AAA, BBB, CCC, DDD, BBB, AAA]
去重后:[AAA, BBB, CCC, DDD]