1.借助Set的特性进行去重
/**
* 去除重复数据
* 由于Set的无序性,不会保持原来顺序
* @param list
*/
public static List<String> list distinct(List<String> list) {
final boolean sta = null != list && list.size() > 0;
List doubleList= new ArrayList();
if (sta) {
Set set = new HashSet();
set.addAll(list);
doubleList.addAll(set);
}
return doubleList;
}
2.利用set集合特性保持顺序一致去重
// Set去重并保持原先顺序的两种方法
public static void delRepeat(List<String> list) {
//方法一
List<String> listNew = new ArrayList<String>(new TreeSet<String>(list));
//方法二
List<String> listNew2 = new ArrayList<String>(new LinkedHashSet<String>(list));
}
3.set形式删除
public static List removeDuplicate(List list) {
HashSet h = new HashSet(list);
list.clear();
list.addAll(h);
return list;
}