当前位置: 首页 > 图灵资讯 > 技术篇> JAVA去除List中某个重复的属性

JAVA去除List中某个重复的属性

来源:图灵教育
时间:2023-06-25 14:18:35

 public static void main(String[] args) {List<Driver> drivers = new ArrayList<Driver>();Driver driver1 = new Driver();driver1.setName(张三);drivers.add(driver1);Driver driver2 = new Driver();driver2.setName(张三);drivers.add(driver1);List<Driver> bDrivers = removeRepeatDriver(drivers);for (Driver driver : bDrivers) {System.out.println(driver.getName());}}      public static List<Driver> removeRepeatDriver(List<Driver> aDrivers){        Set<Driver> set = new TreeSet<Driver>(new Comparator<Driver>() {            [@Override](https://my.oschina.net/u/1162528)            public int compare(Driver o1, Driver o2) {                //字符串,按照asicc码排序                return o1.getName().compareTo(o2.getName());            }        });        set.addAll(aDrivers);        return new ArrayList<Driver>(set);}