如何去除List中重复的数据,可以使用如下的方法。
注:引用的包包括:
commons-beanutils(Bean反射工具)
commons-lang(常用工具类,这里用到了String工具类)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| package com.listutil; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.lang.StringUtils; import com.Sort.BeanSort.TestBean;
public class ListUtils { private static final char SEPARATOR = '|';
public static <T> List<T> removeDuplication(List<T> list, String... keys) { if (list == null || list.isEmpty()) { System.err.println("List is empty."); return list; } if (keys == null || keys.length < 1) { System.err.println("Missing parameters."); return list; } for (String key : keys) { if (StringUtils.isBlank(key)) { System.err.println("Key is empty."); return list; } } List<T> newList = new ArrayList<T>(); Set<String> keySet = new HashSet<String>(); for (T t : list) { StringBuffer logicKey = new StringBuffer(); for (String keyField : keys) { try { logicKey.append(BeanUtils.getProperty(t, keyField)); logicKey.append(SEPARATOR); } catch (Exception e) { e.printStackTrace(); return list; } } if (!keySet.contains(logicKey.toString())) { keySet.add(logicKey.toString()); newList.add(t); } else { System.err.println(logicKey + " has duplicated."); } } return newList; } public static void main(String[] args) { List<TestBean> list = new ArrayList<TestBean>(); TestBean tb1 = new TestBean(); tb1.setField1(34); tb1.setField3("aaa"); TestBean tb2 = new TestBean(); tb2.setField1(344); tb2.setField3("ab"); TestBean tb3 = new TestBean(); tb3.setField1(3344); tb3.setField3("ab"); TestBean tb4 = new TestBean(); tb4.setField1(3344); tb4.setField3("ab"); list.add(tb1); list.add(tb2); list.add(tb3); list.add(tb4); List<TestBean> list2 = removeDuplication(list, "field1", "field3"); for (TestBean b : list) { System.out.println(b); } System.out.println(" ---"); for (TestBean b : list2) { System.out.println(b); } } }
|