list去重

List 去重有以下 3 种实现思路:

 

自定义方法去重,通过循环判断当前的元素是否存在多个,如果存在多个,则删除此重复项,循环整个集合最终得到的就是一个没有重复元素的 List;

使用 Set 集合去重,利用 Set 集合自身自带去重功能的特性,实现 List 的去重;

使用 JDK 8 中 Stream 流的去重功能。

自定义去重的实现方法有两种,首先我们可以创建一个新集合,通过循环原集合判断循环的元素,是否已存在于新集合,如果不存在则插入,否则就忽略,这样循环完,最终得到的新集合就是一个没有重复元素的集合.import lombok.Data;

 

import java.util.ArrayList;

import java.util.List;

 

public class DistinctExample {

    public static void main(String[] args) {

        // 创建并给 List 赋值

        List<Person> list = new ArrayList<>();

        list.add(new Person("李四", "123456", 20));

        list.add(new Person("张三", "123456", 18));

        list.add(new Person("王五", "123456", 22));

        list.add(new Person("张三", "123456", 18));

        // 去重操作

        List<Person> newList = new ArrayList<>(list.size());

        list.forEach(i -> {

            if (!newList.contains(i)) { // 如果新集合中不存在则插入newList.add(i);

            }

        });

        // 打印集合newList.forEach(p -> System.out.println(p));

    }

}

 

@Data

class Person {

    private String name;

    private String password;

    private int age;

 

    public Person(String name, String password, int age) {

        this.name = name;

        this.password = password;

        this.age = age;

    }

}

自定义去重功能实现方法二,使用迭代器循环并判断当前元素首次出现的位置(indexOf)是否等于最后出现的位置(lastIndexOf),如果不等于则说明此元素为重复元素,删除当前元素即可,这样循环完就能得到一个没有重复元素的集合,实现代码如下:

import lombok.Data;

 

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

 

public class DistinctExample {

    public static void main(String[] args) {

        // 创建并给 List 赋值

        List<Person> list = new ArrayList<>();

        list.add(new Person("李四", "123456", 20));

        list.add(new Person("张三", "123456", 18));

        list.add(new Person("王五", "123456", 22));

        list.add(new Person("张三", "123456", 18));

        // 去重操作

        Iterator<Person> iterator = list.iterator();

        while (iterator.hasNext()) {

            // 获取循环的值

            Person item = iterator.next();

            // 如果存在两个相同的值if (list.indexOf(item) != list.lastIndexOf(item)) {

                // 移除相同的值

                iterator.remove();

            }

        }

        // 打印集合信息

        list.forEach(p -> System.out.println(p));

    }

}

 

@Data

class Person {

    private String name;

    private String password;

    private int age;

 

    public Person(String name, String password, int age) {

        this.name = name;

        this.password = password;

        this.age = age;

    }

}

Set 集合天生具备去重特性,在创建 Set 集合时可以传递一个 List 集合,这样就能实现数据转移和去重的功能了,具体实现代码如下:import lombok.Data;

 

import java.util.ArrayList;

import java.util.HashSet;

import java.util.List;

 

public class DistinctExample {

    public static void main(String[] args) {

        // 创建并给 List 赋值

        List<Person> list = new ArrayList<>();

        list.add(new Person("李四", "123456", 20));

        list.add(new Person("张三", "123456", 18));

        list.add(new Person("王五", "123456", 22));

        list.add(new Person("张三", "123456", 18));

        // 去重操作

        HashSet<Person> set = new HashSet<>(list);

        // 打印集合信息set.forEach(p -> System.out.println(p));

    }

}

 

@Data

class Person {

    private String name;

    private String password;

    private int age;

 

    public Person(String name, String password, int age) {

        this.name = name;

        this.password = password;

        this.age = age;

    }

}

最后一种也是最简单的一种去重方式,我们可以使用 JDK 8 中提供的 Stream 进行去重,Stream 中包含了一个去重方法:distinct,可以直接实现集合的去重功能,具体实现代码如下:import lombok.Data;

 

import java.util.ArrayList;

import java.util.List;

import java.util.stream.Collectors;

 

public class DistinctExample {

    public static void main(String[] args) {

        // 创建并给 List 赋值

        List<Person> list = new ArrayList<>();

        list.add(new Person("李四", "123456", 20));

        list.add(new Person("张三", "123456", 18));

        list.add(new Person("王五", "123456", 22));

        list.add(new Person("张三", "123456", 18));

        // 去重操作

        list = list.stream().distinct().collect(Collectors.toList());

        // 打印集合信息

        list.forEach(p -> System.out.println(p));

    }

}

 

@Data

class Person {

    private String name;

    private String password;

    private int age;

 

    public Person(String name, String password, int age) {

        this.name = name;

        this.password = password;

        this.age = age;

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值