Java關於List集合去重方案詳細介紹

1 常規去重

碰到List去重的問題,除瞭遍歷去重,我們常常想到利用Set集合不允許重復元素的特點,通過List和Set互轉,來去掉重復元素。

// 遍歷後判斷賦給另一個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 + "]");
}

stream去重

  //利用java8的stream去重
  List uniqueList = list.stream().distinct().collect(Collectors.toList());
  System.out.println(uniqueList.toString());

上面的方法在List元素為基本數據類型及String類型時是可以的,但是如果List集合元素為對象,卻失效瞭!

2 對象去重

可利用for循環遍歷的方式進行判斷去重,很簡單不贅述,看如下方法:

2.1 stream去重

// 根據name屬性去重
List<User> uniqueNameList = userList
        .stream()
        .collect(collectingAndThen(
                toCollection(() -> new TreeSet<>(comparing(User::getName))), ArrayList::new)
        );

System.out.println("根據name去重後集合元素:" + uniqueNameList.toString());

// 根據name,age屬性去重
List<User> uniqueNameAndAgeList = userList
        .stream()
        .collect(collectingAndThen(
                toCollection(() -> new TreeSet<>(comparing(o -> o.getName() + ";" + o.getAge()))), ArrayList::new)
        );

System.out.println("根據name,age去重後集合元素:" + uniqueNameAndAgeList.toString());

輸出結果:

重寫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();
    }

到此這篇關於Java關於List集合去重方案詳細介紹的文章就介紹到這瞭,更多相關Java List集合去重內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: