亲宝软件园·资讯

展开

Java List集合去重 Java关于List集合去重方案仔细讲解

JavaEdge. 人气:0
想了解Java关于List集合去重方案仔细讲解的相关内容吗,JavaEdge.在本文为您仔细讲解Java List集合去重的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Java,List集合去重,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();
    }

加载全部内容

相关教程
猜你喜欢
用户评论