1. 常规元素去重
// 遍历后判断赋给另一个List集合,保持原来顺序
public static void ridRepeat1(List<String> list) {
System.out.println("list = [" + list + "]");
List<String> listNew = new ArrayList<String>();
for (String str : list) {
if (!listNew.contains(str)) {
listNew.add(str);
}
}
System.out.println("listNew = [" + listNew + "]");
}
// Set集合去重,保持原来顺序
public static void ridRepeat2(List<String> list) {
System.out.println("list = [" + list + "]");
List<String> listNew = new ArrayList<String>();
Set set = new HashSet();
for (String str : list) {
if (set.add(str)) {
listNew.add(str);
}
}
System.out.println("listNew = [" + listNew + "]");
}
// Set去重 由于Set(HashSet)的无序性,不会保持原来顺序
public static void ridRepeat3(List<String> list) {
System.out.println("list = [" + list + "]");
Set set = new HashSet();
List<String> listNew = new ArrayList<String>();
set.addAll(list);
listNew.addAll(set);
System.out.println("listNew = [" + listNew + "]");
}
// Set通过HashSet去重(将ridRepeat3方法缩减为一行) 无序
public static void ridRepeat4(List<String> list) {
System.out.println("list = [" + list + "]");
List<String> listNew = new ArrayList<String>(new HashSet(list));
System.out.println("listNew = [" + listNew + "]");
}
// Set通过TreeSet去重 会按字典顺序重排序
public static void ridRepeat5(List<String> list) {
System.out.println("list = [" + list + "]");
List<String> listNew = new ArrayList<String>(new TreeSet<String>(list));
System.out.println("listNew = [" + listNew + "]");
}
// Set通过LinkedHashSet去重 保持原来顺序
public static void ridRepeat6(List<String> list) {
System.out.println("list = [" + list + "]");
List<String> listNew = new ArrayList<String>(new LinkedHashSet<String>(list));
System.out.println("listNew = [" + listNew + "]");
}
使用JDK1.8的去重
//利用java8的stream去重
List uniqueList = list.stream().distinct().collect(Collectors.toList());
System.out.println(uniqueList.toString());
2. 对象去重
解决对象去重,可以利用for循环遍历的方式进行判断去重,但今天我不准备探究这种方法,要使用的是如下两种:
//根据name属性去重
List<User> unique1 = userList.stream().collect(
collectingAndThen(
toCollection(() -> new TreeSet<>(comparing(User::getName))), ArrayList::new));
System.out.println(unique1.toString());
//根据name,age属性去重
List<User> unique2 = userList.stream().collect(
collectingAndThen(
toCollection(() -> new TreeSet<>(comparing(o -> o.getName() + ";" + o.getAge()))), ArrayList::new)
);
System.out.println(unique2.toString());
2.2 对象中重写equals()方法和hashCode()方法
//重写equals方法
@Override
public boolean equals(Object obj) {
User user = (User) obj;
return name.equals(user.getName()) && (age==user.getAge());
}
//重写hashCode方法
@Override
public int hashCode() {
String str = name + age;
return str.hashCode();
}
3. equals()方法和hashCode()方法探究
/**
* Compares this string to the specified object. The result is {@code
* true} if and only if the argument is not {@code null} and is a {@code
* String} object that represents the same sequence of characters as this
* object.
*
* @param anObject
* The object to compare this {@code String} against
*
* @return {@code true} if the given object represents a {@code String}
* equivalent to this string, {@code false} otherwise
*
* @see #compareTo(String)
* @see #equalsIgnoreCase(String)
*/
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}