how to find biggest and smallest date in a list?

In Java 8 you can do:

final Date maxDate = dates.stream()
    .max(Date::compareTo)
    .get();

Why not use Collections.sort() and then take the first/last entries? You can either use natural ordering, or specify your own Comparator.

Note this will sort the collection in-place. It won't give you a new sorted collection.


this is a classic minimum & maximum problem. no matter your Object is Date or String, or Number. Important is that they are comparable.

Sorting then take max/min

Most straightforward method is like others' answer, Sort the collection with java build-in Sort method. then take the 1st and last element as your min/max object. However it turns a linear time O(n) problem into O(nlgn) . well if performance matter is not what you are considering about. you can skip reading my rest text. And I will upvote @Quoi's answer.

the easy way with linear time:

keep two variable min and max, then go for each element in your collection. compare to your current min and max and get the right value. till the end.

the Optimized way with linear time

the way above is easy, but it brings more comparisons (2n). we can optimize it a little. Same as above, you have min and max two vars. and in loop, you take a pair of elements instead of single. you compare first the two element in the pair. take the bigger one to compare to your max var, and the smaller one to your min var. now we just have to do 3(n/2) comparisons.

hope it helps

Edit

I think the codes are not so hard to write. As Quoi suggested, if codes could make the answer complete, I would add them.

Note that in the example I used an int array. Basically it is the same as Date object. The codes are written in a unit test method. And it looks long, since I try to explain the idea above clear.

@Test
    public void testx() {
        final int size = 300000;
        final int[] array = new int[size];
        final Random random = new Random();
        // fill a huge array for testing
        for (int i = 0; i < array.length; i++) {
            array[i] = random.nextInt();
        }

        int min1 = array[0], max1 = array[1], cmp1 = 0;
        int min2 = array[0], max2 = array[1], cmp2 = 0;

        for (int i = 2; i < array.length; i++) {
            min1 = array[i] < min1 ? array[i] : min1;
            cmp1++;
            max1 = array[i] > max1 ? array[i] : max1;
            cmp1++;
        }

        LOG.debug("linear time to find Max & Min simultaneously");
        LOG.debug("Size: {}", size);
        LOG.debug("Max : {}", max1);
        LOG.debug("Min : {}", min1);
        LOG.debug("Total comparisons : {}", cmp1);

        // optimized linear
        int bigger, smaller;
        final boolean odd = array.length % 2 == 1;
        final int till = odd ? array.length - 1 : array.length;
        for (int i = 2; i < till; i += 2) {

            if (array[i] >= array[i + 1]) {
                bigger = array[i];
                smaller = array[i + 1];
            } else {
                bigger = array[i + 1];
                smaller = array[i];
            }
            cmp2++;
            min2 = smaller < min2 ? smaller : min2;
            cmp2++;
            max2 = bigger > max2 ? bigger : max2;
            cmp2++;
        }
        if (odd) {
            min2 = array[size - 1] < min2 ? array[size - 1] : min2;
            max2 = array[size - 1] > max2 ? array[size - 1] : max2;
        }
        LOG.debug("====================================================");
        LOG.debug("optimized linear time to find Max & Min simultaneously");
        LOG.debug("Size: {}", size);
        LOG.debug("Max : {}", max2);
        LOG.debug("Min : {}", min2);
        LOG.debug("Total comparisons : {}", cmp2);
    }

output

DEBUG:  linear time to find Max & Min simultaneously
DEBUG:  Size: 300000
DEBUG:  Max : 2147475519
DEBUG:  Min : -2147446732
DEBUG:  Total comparisons : 599996
DEBUG:  ====================================================
DEBUG:  optimized linear time to find Max & Min simultaneously
DEBUG:  Size: 300000
DEBUG:  Max : 2147475519
DEBUG:  Min : -2147446732
DEBUG:  Total comparisons : 449997