list数据去重复
package list.test; import java.util.*; /** * @author jia * @create */ public class ListTest { final static int _capacity = 100000; final static Random rand = new Random(System.currentTimeMillis() + _capacity); static List<String> list = new ArrayList<String>(); static List<String> newList = new ArrayList<String>(); public static void main(String[] args) { long ts = System.currentTimeMillis(); int modVal = _capacity / 3; for (int i = 0; i < _capacity; i++) { rand.setSeed(i); list.add(Integer.toString(Math.abs(rand.nextInt() % modVal))); } ts = System.currentTimeMillis() - ts; System.out.println("生成十万条数据时间:" + ts); test1(); test2(); } public static void test1() { newList.clear(); int repetition = 0; long ts = System.currentTimeMillis(); for (String temp : list) { if (!newList.contains(temp)) { newList.add(temp); } else { repetition++; } } ts = System.currentTimeMillis() - ts; System.out.println("十万条数据test1方法耗时:" + ts); System.out.println("重复的数据条数:" + repetition); System.out.println("新数组的大小:" + newList.size()); } public static void test2() { Long ts = System.currentTimeMillis(); int repetition = 0; newList.clear(); Collections.sort(list, new Comparator<String>() { // 降序排列 public int compare(String o1, String o2) { return Integer.valueOf(o2) - Integer.valueOf(o1); } }); String str = list.get(0); for (int i = 1; i < list.size(); i++) { if (str.equals(list.get(i))) { repetition++; continue; } else { newList.add(str); str = list.get(i); } } newList.add(str); ts = System.currentTimeMillis() - ts; System.out.println("十万条数据test2方法耗时:" + ts); System.out.println("重复的数据条数:" + repetition); System.out.println("新数组的大小:" + newList.size()); } }
生成十万条数据时间:1112
十万条数据test1方法耗时:30790
重复的数据条数:66667
新数组的大小:33333
十万条数据test2方法耗时:518
重复的数据条数:66667
新数组的大小:33333