如何从列表中<object>删除重复项



我在java中有一个名为foo的对象。我可以做foo.getDate();,这将给我一个项目的日期。但现在我有了一个list<foo>,我想为每个项目安排一个日期。因此,如果我循环浏览我的列表,我会看到以下输出:

3-1-2015
3-1-2015
5-1-2015
8-1-2015
8-1-2015

但我想看看:

3-1-2015
5-1-2015
8-1-2015

因此,我希望只有第一个具有唯一日期的项目才会添加到列表中。我如何在Java中做到这一点?

提前感谢!

可能最简单的方法是使用映射(例如HashMap)。。。使用Date作为键,然后将所有Foo对象放入其中。然后,每次键已经存在时,该值都将被覆盖,每个Date只会有一个Foo对象。如果您需要一个列表(例如用于排序),那么您可以执行类似new ArrayList<Foo>( myMap.values() );的操作。

创建将存储唯一日期的集合。如果您的foo实例中的日期尚未添加,请将此实例添加到包含具有唯一日期的foo对象的列表中。

List<Foo> list = new ArrayList<>();
//fill list with your foo instances

Set<Date> uniqueDates = new HashSet<>();
List<Foo> resultList = new ArrayList<>();
for (Foo f : list){
    if (uniqueDates.add(f.getDate())){//if I was able to add date to set
        //it means that instance with this date is seen first time
        //so I can add it to result list
        resultList.add(f);
    }
}

您可能应该使用Set

添加到@Pshemo的答案中,用Java 8做同样的事情很简单:

public class RemoveDuplicates {
    public static void main(String[] args) {
        // Initialize some dates
        long now = System.currentTimeMillis();
        Date d1 = new Date(now);
        Date d2 = new Date(now - 10_000_000_000L);
        Date d3 = new Date(now - 100_000_000_000L);
        // Initialize some Foos with the dates    
        List<Foo> list = new ArrayList<>(Arrays.asList(
            new Foo(d3), new Foo(d3), new Foo(d2), 
            new Foo(d1), new Foo(d1)));
        Set<Date> uniqueDates = new HashSet<>();
        // Filter foos whose date is already in the set    
        List<Foo> distinct = list.stream().filter(
            f -> uniqueDates.add(f.getDate())).
            collect(Collectors.toList());
        System.out.println(distinct); // [Foo [date=17/01/12], 
                                      //  Foo [date=24/11/14], 
                                      //  Foo [date=19/03/15]]
    }
    static class Foo {
        private static DateFormat formatter = DateFormat.getDateInstance(DateFormat.SHORT);
        private final Date date;
        Date getDate() {
            return this.date;
        }
        Foo(Date date) {
            this.date = date;
        }
        @Override
        public String toString() {
            return "Foo [date=" + formatter.format(this.date) + "]";
        }
    }
}

原理完全相同:如果日期已经在集合中,则从流中过滤Foo

最新更新