List去重/排序的通用方法
package com.ck.test.util;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
/**
* List去重/排序的通用方法
*
* @author CK
*
* @param T对象
*/
public class ListSort {
/**
*
* @param list对象 要排序的集合
* @param method 要排序的实体的属性所对应的get方法
* @param sort desc 倒序、asc升序、""为原样
*/
public void Sort(List list, final String method, final String sort) {
// 用内部类实现排序
Collections.sort(list, new Comparator() {
public int compare(T a, T b) {
int ret = 0;
try {
// 获取m1的方法名
Method m1 = a.getClass().getMethod(method, null);
// 获取m2的方法名
Method m2 = b.getClass().getMethod(method, null);
if(sort != null && "desc".equals(sort)) {
ret = m2.invoke(((T)b), null).toString().compareTo(m1.invoke(((T)a),null).toString());
} else {
// 正序排序
ret = m1.invoke(((T)a), null).toString().compareTo(m2.invoke(((T)b), null).toString());
}
} catch (NoSuchMethodException ne) {
System.out.println(ne);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return ret;
}
});
}
/**
* 重复的选中其一以及其他不重复的对象保留,并进行排序
* @param list 要处理的list对象
* @param method 排序对象字段的getxxx()方法名,即getxxx
* @param sort desc/DESC 倒序、asc/ASC升序、""为原样
* @return
*/
public List sortObjDistinctList(List list, final String method, final String sort) {
List listWithoutDup = null;
listWithoutDup = distinctListValue(list);
if(sort != null && ("desc".equals(sort) || "DESC".equals(sort) || "ASC".equals(sort) || "asc".equals(sort))) {
// 排序
Sort(listWithoutDup, method, sort);
} else {
return listWithoutDup;
}
return listWithoutDup;
}
/**
* 去重,返回重复的数据
* @param list
* @return
*/
public List sortDistinct2Other(List list) {
// 重复数据
List list2 = new ArrayList<>();
// 使用hashset去重复,set为重复的集合,可以通过new ArrayList(set)转换成list
HashSet set = new HashSet<>();
// 去掉重复的值,只保留其他不重复的对象
for (T t : list) {
boolean add = set.add(t);
if (!add) {
list2.add(t);
}
}
return list2;
}
/**
* 除重 去掉重复的值,并保留重复值中一个值及其他不重复的值
* @param list 处理list
* @return
*/
public List distinctListValue(List list) {
List listWithoutDup = new ArrayList(new HashSet(list));
return listWithoutDup;
}
/**
*
* @param list 处理list
* @param sort desc/DESC 倒序、asc/ASC升序、""为原样
* @param flag true-除重;false-不除重
* @return
*/
public List distinctListASCorDesc(List list, final String sort, boolean flag) {
List listWithoutDup = null;
if(flag) {
listWithoutDup = distinctListValue(list);
} else {
listWithoutDup = list;
}
if(sort != null && ("ASC".equals(sort) || "asc".equals(sort))) {
// 升序
Collections.sort(listWithoutDup,Collator.getInstance(java.util.Locale.CHINA));//注意:是根据的汉字的拼音的字母排序的,而不是根据汉字一般的排序方法
} else if(sort != null && ("desc".equals(sort) || "DESC".equals(sort))) {
// 降序
Collections.reverse(listWithoutDup);//不指定排序规则时,也是按照字母的来排序的
} else {
return listWithoutDup;
}
return listWithoutDup;
}
}
package com.ck.test.modal;
/**
* 这里id,name,age相同则Student相同,
* 若有其他相同
* @author Administrator
*
*/
public class Student {
int id;
String name;
int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
public Student(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "(" + id + ", " + name + ", " + age + ")";
// return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
package com.ck.test;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import org.junit.Assert;
import org.junit.Test;
import com.ck.test.modal.Student;
import com.ck.test.util.ListSort;
/*
* 对list去重
*
*/
public class TestList {
//去除set中重复数据的方法
private static Set removeDuplicate(Set set) {
Map map = new HashMap();
Set tempSet = new HashSet();
for(Student p : set) {
if(map.get(p.getName()) == null ) {
map.put(p.getName(), p);
} else {
tempSet.add(p);
}
}
set.removeAll(tempSet);
return set;
}
public static String outCollection(Collection coll) {
StringBuffer sb = new StringBuffer();
for (Object obj : coll) {
sb.append(obj + "," );
}
System.out.println(sb.toString());
return sb.toString();
}
/*
* 对list去重 list with dup:[1, 2, 3, 1] list without dup:[3, 2, 1]
*/
@Test
public void distinctListValue() {
List listWithDup = new ArrayList();
listWithDup.add("1");
listWithDup.add("2");
listWithDup.add("4");
listWithDup.add("3");
listWithDup.add("1");
List listWithoutDup = new ArrayList(new HashSet(listWithDup));
System.out.println("list with dup:" + listWithDup);
System.out.println("list without dup:" + listWithoutDup);
System.out.println("list去重后(去掉重复的值,并保留重复值中一个值及其他不重复的值/排序) :\n" + (new ListSort()).distinctListValue(listWithDup));
Collections.sort(listWithoutDup);
Collections.reverse(listWithoutDup); //按照age降序 23,22
System.out.println(listWithoutDup);
}
@Test
public void test2() {
List list = new ArrayList();
list.add("王硕");
list.add("刘媛媛");
list.add("李明1");
list.add("李明2");
list.add("李明");
list.add("刘迪");
list.add("刘布2");
list.add("李明");
list.add("刘布8");
list.add("刘布");
list.add("刘布");
list.add("3");
list.add("4");
list.add("4");
list.add("2");
list.add("4");
list.add("1");
System.out.println((new ListSort()).distinctListValue(list));
for(int i=0;i list = new ArrayList<>();
// 重复数据
List list2 = new ArrayList<>();
// 填充
for (int i = 0; i < 10; i++) {
list.add(new Student(i, "_" + i, 18 + i));
Random random = new Random();
if (random.nextBoolean()) {
list.add(new Student(i, "_" + i, 18 + i));
}
}
// 使用hashset去重复,set为重复的集合,可以通过new ArrayList(set)转换成list
HashSet set = new HashSet<>();
// 去掉重复的值,只保留其他不重复的对象
for (Student student : list) {
boolean add = set.add(student);
if (!add) {
list2.add(student);
}
}
List listWithoutDup = new ArrayList(new HashSet(list));
System.out.println("list初始化:\n" + list);
System.out.println("list去重后(重复的对象):\n" + list2);
System.out.println("list去重后(去掉重复的值,并保留重复值中一个值及其他不重复的值):\n" + listWithoutDup);
System.out.println("list去重后(去掉重复的值,并保留重复值中一个值及其他不重复的值/排序) :\n" + (new ListSort()).sortObjDistinctList(list, "getId", "desc"));
ListSort listSort= new ListSort();
// 排序
listSort.Sort(listWithoutDup, "getId", "");
// listSort.Sort(listWithoutDup, "getId", "");
System.out.println("list去重后(去掉重复的值,并保留重复值中一个值及其他不重复的值) 排序:\n" + listWithoutDup);
Set set2 = new HashSet(list);
System.out.println("list without dup:" + removeDuplicate(set2));
// 比较
Assert.assertEquals(list.size(), list2.size() + set.size());
}
}
结果:
[刘布2, 刘布8, 刘媛媛, 王硕, 3, 2, 1, 4, 刘布, 李明, 刘迪, 李明1, 李明2]
王硕,刘媛媛,李明1,李明2,李明,刘迪,刘布2,李明,刘布8,刘布,刘布,3,4,4,2,4,1,
1,2,3,4,4,4,李明,李明,李明1,李明2,刘布,刘布,刘布2,刘布8,刘迪,刘媛媛,王硕,
王硕,刘媛媛,刘迪,刘布8,刘布2,刘布,刘布,李明2,李明1,李明,李明,4,4,4,3,2,1,list初始化:
[(0, _0, 18), (1, _1, 19), (1, _1, 19), (2, _2, 20), (3, _3, 21), (4, _4, 22), (4, _4, 22), (5, _5, 23), (5, _5, 23), (6, _6, 24), (6, _6, 24), (7, _7, 25), (7, _7, 25), (8, _8, 26), (9, _9, 27), (9, _9, 27)]
list去重后(重复的对象):
[(1, _1, 19), (4, _4, 22), (5, _5, 23), (6, _6, 24), (7, _7, 25), (9, _9, 27)]
list去重后(去掉重复的值,并保留重复值中一个值及其他不重复的值):
[(9, _9, 27), (3, _3, 21), (2, _2, 20), (8, _8, 26), (0, _0, 18), (5, _5, 23), (7, _7, 25), (1, _1, 19), (6, _6, 24), (4, _4, 22)]
list去重后(去掉重复的值,并保留重复值中一个值及其他不重复的值/排序) :
[(9, _9, 27), (8, _8, 26), (7, _7, 25), (6, _6, 24), (5, _5, 23), (4, _4, 22), (3, _3, 21), (2, _2, 20), (1, _1, 19), (0, _0, 18)]
list去重后(去掉重复的值,并保留重复值中一个值及其他不重复的值) 排序:
[(0, _0, 18), (1, _1, 19), (2, _2, 20), (3, _3, 21), (4, _4, 22), (5, _5, 23), (6, _6, 24), (7, _7, 25), (8, _8, 26), (9, _9, 27)]
list without dup:[(9, _9, 27), (3, _3, 21), (2, _2, 20), (8, _8, 26), (0, _0, 18), (5, _5, 23), (7, _7, 25), (1, _1, 19), (6, _6, 24), (4, _4, 22)]
list with dup:[1, 2, 4, 3, 1]
list without dup:[3, 2, 1, 4]
list去重后(去掉重复的值,并保留重复值中一个值及其他不重复的值/排序) :
[3, 2, 1, 4]
[4, 3, 2, 1]