自己写了一个方法
[code="java"]private void listFilter(List list) {
for (int i = 0; i < list.size() - 1; i++) {
for (int j = i + 1; j < list.size();) {
//list.get(i)和list.get(j)取得两个对象
if (/*判断两个对象的某些值*/) {
list.remove(j);
} else {
j++;
}
}
}
}[/code]
有其他更好的方法么?
[code="java"]
还有一个办法就是···
重写 hashcode equals(Object obj)
假如当对象2个属性想等时候 hashcode返回 一样 equals返回true
然后遍历list 放入hashset
就没的重复的了。。。。
[/code]
这样就无需嵌套循环。。。。可能效率高点。
List接口有一个removeAll的方法,只要实现了List接口的对象都可以使用,你可以看一下:
[code="java"]
Removes from this list all of its elements that are contained in the specified collection (optional operation).
Specified by: removeAll(...) in Collection
Parameters:
c collection containing elements to be removed from this list
Returns:
true if this list changed as a result of the call
Throws:
UnsupportedOperationException - if the removeAll operation is not supported by this list
ClassCastException - if the class of an element of this list is incompatible with the specified collection (optional)
NullPointerException - if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null
See Also:
remove(Object)
contains(Object)
[/code]
[code="java"]
为什么不直接remove
boolean remove(Object o)
Removes the first occurrence in this list of the specified element (optional operation).
[/code]
[code="java"]你的意思是 你要去掉LIST中重复的记录???[/code]
[quote]问题补充:
renpeng301 写道
Java代码
你的意思是 你要去掉LIST中重复的记录???
是的,比如List中两个对象中有两个属性值相同就去掉其中一个。[/quote]
从写对象的equals方法
[code="java"]
LIST去掉相视的对象。。。
这个相视 你可能有自己的规则
当2个对象那些属性相等时 才认为这2个对象相视 对吗?
public class Student implements Comparable{
private int id;
private String name;
private String mark;
public Student (int id, String name,String mark) {
this.id = id;
this.name = name;
}
public void setId (int id) {
this.id = id;
}
public void setName (String name) {
this.name = name;
}
public int getId () {
return id;
}
public String getName () {
return name;
}
public String getMark () {
return id;
}
public void setMark (String mark) {
this.mark= mark;
}
//当 ID NAME 都一样是2个对象相等
public int compareTo (Student arg) {
if (id > arg.id)
return 1;
else if (id == arg.id&&name==arg.name)
return 0;
else
return -1;
}
public static void main (String args[]) {
List test=new ArrayList();
tset.add(new Student(5, "Tom"));
tset.add(new Student(3, "John","John 1"));
tset.add(new Student(9, "David","David 1"));
tset.add(new Student(3, "John",,"John 2"));
//然后按照你的方法啊
for (int i = 0; i < test.size() - 1; i++) {
for (int j = i + 1; j < test.size();) {
//list.get(i)和list.get(j)取得两个对象
if (test.get(i).compareTo(test.get(j))==0) {
list.remove(j);
} else {
j++;
}
}
}
}
}
[/code]
还有一个办法就是···
重写 hashcode equals(Object obj)
假如当对象2个属性想等时候 hashcode返回 一样 equals返回true
然后遍历list 放入hashset
就没的重复的了。。。。
下面的代码希望对你能有所帮助,可以根据属性过滤重复
[code="java"]
/**
* 直接读取对象属性值,无视private/protected修饰符,不经过getter函数.
*
* @author Jacky
* @param object
* @param fieldName
* @return
*/
public static Object getFieldValue(final Object object, final String fieldName) {
Field field = getDeclaredField(object, fieldName);
if (field == null)
throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + object + "]");
makeAccessible(field);
Object result = null;
try {
result = field.get(object);
} catch (IllegalAccessException e) {
}
return result;
}
/**
* 循环向上转型,获取对象的DeclaredField.
*
* @param field
* @author Jacky
*/
protected static void makeAccessible(final Field field) {
if (!Modifier.isPublic(field.getModifiers()) || !Modifier.isPublic(field.getDeclaringClass
().getModifiers())) {
field.setAccessible(true);
}
}
/**
* 循环向上转型,获取对象的DeclaredField.
*
* @param object
* @param fieldName
* @author Jacky
*/
protected static Field getDeclaredField(final Object object, final String fieldName) {
for (Class<?> superClass = object.getClass(); superClass != Object.class; superClass = superClass.getSuperclass()) {
try {
return superClass.getDeclaredField(fieldName);
} catch (NoSuchFieldException e) {
// Field不在当前类定义,继续向上转型
}
}
return null;
}
/**
* 根据属性过滤重复数据
*
* @param list
* @param filterParams
* 属性名称
* @return
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws IllegalAccessException
*/
@SuppressWarnings("unchecked")
public static <T> List<T> removeIteranceAndSortList(final List<T> list, final String... filterParams) {
Map<String, Object> map = new HashMap<String, Object>();
List<T> resultList = new ArrayList<T>();
if (null == filterParams) {
return list;
}
if (list != null && list.size() > 0) {
Iterator<T> it = list.iterator();
while (it.hasNext()) {
Object obj = it.next();
StringBuffer key = new StringBuffer();
for (String property : filterParams) {
Object value = null;
try {
value = BaokuGenericsUtil.getFieldValue(obj, property);
} catch (Exception e) {
value = "";
}
key.append(value);
}
if (null == map.get(key.toString())) {
map.put(key.toString(), obj);
resultList.add((T) obj);
} else {
it.remove();
}
}
}
return resultList;
}
[/code]
对象实现Comparator接口,重写compareTo方法后是不是我把List放入HashSet中,HashSet判断对象相等使用compareTo方法,就不需重写equals()和hashCode()方法了?是不是去重就直接
Java代码 收藏代码
HashSet h = new HashSet(list);
list.clear();
list.addAll(h);
这样不行啊。。。
你如果要把list中的对象 放入 hashset 去掉你规则的重复,必须要重写 hashcode 和equals
你试下 重写 hashcode 和equals
你必须要 把hashcode 和equals 重写对啊·。。