Question: I have a object in java called foo.
I can do `foo.getDate();` and that will give me the date of one item.
But now I have a `list<foo>` and I want to get per item one date.
So if I loop through my list I will see this output:

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

```

But I want to see:

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

```

So I want that only the first item with a unique date will be added to the list.
How can I do this in Java? 

Thanks in advance!

Answer: 
Probably the most easy way would be use a map (`HashMap` for example)... Use the Date as a key, then put all your Foo objects into it. Then every time a key already exists the value will be overwritten and you'll end up with only one Foo object per Date. If you need a list (for sorting, for example) you can do something like `new ArrayList<Foo>( myMap.values() );` then.
Just to add to @Pshemo's answer, doing the same with Java 8 is straightforward:

```
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) + "]";
        }
    }
}

```

The principle is exactly the same: if the date is already in the set, then the `Foo` is filtered from the stream.