ArrayList迭代器去除重复字符串

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@SuppressWarnings({"unchecked","rawtypes"})
public class Test1 {
    public static void main(String[] args) {
        List list=new ArrayList();
        list.add("a");
        list.add("b");
        list.add("b");
        list.add("a");
        list.add("c");
        list.add("c");
        list.add("c");
        list.add("c");
        
        ArrayList newList = getSingle(list);
        System.out.println(newList);
    }

    /**
     * 去重复
     */
    public static ArrayList getSingle(List list) {
        ArrayList tempList = new ArrayList();
        Iterator it = list.iterator(); 

        while (it.hasNext()) { 
            String s = (String) it.next(); 
            if (!tempList.contains(s)) { 
                tempList.add(s); 
            }
        }
        return tempList;
    }

}

打印结果:
[a, b, c]

你可能感兴趣的:(ArrayList迭代器去除重复字符串)