利用set去除list中的重复值

请注意,本文编写于 3069 天前,最后修改于 121 天前,其中某些信息可能已经过时。

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
public class TestSet {
	public static void main(String[] args) {
		List list = new ArrayList();
		list.add("zhaopeng");
		list.add("zhaopeng");
		list.add("zhaopeng");
		list.add("momo");
		list.add("momo");
		list.add("momo");
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			String object = (String) iterator.next();
			System.err.println(object);
		}
		System.err.println("----------------------");
		List newList = TestSet.removeList(list);
		for (Iterator iterator = newList.iterator(); iterator.hasNext();) {
			String object = (String) iterator.next();
			System.err.println(object);
		}
	}
	public static List removeList(List list) {
		Set someList = new HashSet(list);
		List newList = new ArrayList();
		for (Iterator iterator = someList.iterator(); iterator.hasNext();) {
			Object object = (Object) iterator.next();
			newList.add(object);
		}
		return newList;
	}
}
← 上一篇 下一篇 →
Comments

添加新评论