How to compare all the values of an array with a variable

0

I would like to know if it is possible for me to have an array with multiple dates and know if another date is longer than all the dates within the array.

For example:

Date[] datas = {"02/02/2000","03/03/2000","04/04/2000"};
Date data1 = "03/01/2000";

Would you like to know if data1 is less than all date dates in the array?

    
asked by anonymous 03.09.2018 / 21:42

1 answer

3

First, you can not convert directly from String to Date . So we'll need a conversion step:

    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    sdf.setLenient(false);

    Date[] datas = {sdf.parse("02/02/2000"), sdf.parse("03/03/2000"), sdf.parse("04/04/2000")};
    Date data1 = sdf.parse("03/01/2000");

So there are at least 3 ways to test whether data1 is less than the ones in the array.

The first is to go through the array dates and compare them to the desired date:

        boolean menor1 = true;
        for (Date d : datas) {
            if (d.compareTo(data1) < 0) {
                menor1 = false;
                break;
            }
        }
        System.out.println(menor1 ? "Era a menor." : "Não era a menor.");

The second is to put all these dates in a Set ordered and see if the one you want is the first (and therefore the smallest):

        SortedSet<Date> datas2 = new TreeSet<>();
        for (Date d : datas) {
            datas2.add(d);
        }
        datas2.add(data1);
        boolean menor2 = data1.equals(datas2.first());
        System.out.println(menor2 ? "Era a menor." : "Não era a menor.");

The third one is similar to the first, but using a Stream :

        boolean menor3 = Stream.of(datas).allMatch(d -> data1.compareTo(d) < 0);
        System.out.println(menor3 ? "Era a menor." : "Não era a menor.");

The complete code to show these examples is this:

import java.util.Date;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.stream.Stream;
import java.text.SimpleDateFormat;
import java.text.ParseException;

class TesteDatas {

    public static void main(String[] args) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        sdf.setLenient(false);

        Date[] datas = {sdf.parse("02/02/2000"), sdf.parse("03/03/2000"), sdf.parse("04/04/2000")};
        Date data1 = sdf.parse("03/01/2000");

        // Exemplo 1.
        boolean menor1 = true;
        for (Date d : datas) {
            if (d.compareTo(data1) < 0) {
                menor1 = false;
                break;
            }
        }
        System.out.println(menor1 ? "Era a menor." : "Não era a menor.");

        // Exemplo 2.
        SortedSet<Date> datas2 = new TreeSet<>();
        for (Date d : datas) {
            datas2.add(d);
        }
        datas2.add(data1);
        boolean menor2 = data1.equals(datas2.first());
        System.out.println(menor2 ? "Era a menor." : "Não era a menor.");

        // Exemplo 3.
        boolean menor3 = Stream.of(datas).allMatch(d -> data1.compareTo(d) < 0);
        System.out.println(menor3 ? "Era a menor." : "Não era a menor.");
    }
}

See all of them working here.

Finally, I recommend migrating to the java.time package types. See more about this on this other question.

To know if it is greater than all others rather than smaller, in the first and third approaches, just change < to > . In the second, just change first by last .

You may also want to consider whether the first and third approaches should use < and <= in case the date is already in the given array and still be considered smaller than all (or >= if you want check if it is larger instead of smaller).

    
03.09.2018 / 22:09