通过set的方式去重
针对String[]数组中第二各元素的去重
public void analysisArrayList(ArrayList<String[]> arrayList) { Set<String[]> set = new TreeSet<String[]>(new Comparator<String[]>() { @Override public int compare(String[] o1, String[] o2) { return o1[1].compareTo(o2[1]); //第二个元素去重 } }); set.addAll(arrayList); List<String[]> list = new ArrayList<>(set); }
通过LinkedHashSet方式去重
对象中的某个元素去重,并提取该元素组成新的List
public List<String> analysisArrayList(ArrayList<String[]> arrayList) { List<String> list = Lists.newArrayList(); arrayList.forEach(e -> list.add(e[1])); LinkedHashSet<String> set = new LinkedHashSet<String>(list); list.clear(); list.addAll(set); return list; }