How to compare all values of an array with a variable

Asked

Viewed 788 times

0

I wonder if it is possible for me to have an array with multiple dates and to know if another date is larger 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 I have known if the data1 is less than all dates of the date array?

1 answer

3


First, you can’t convert directly from String for 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");

Therefore, there are at least 3 ways to test whether the data1 is smaller than those 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 into one 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 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 here all of them working on ideone.

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

To know if it is bigger than all the others instead of smaller, in the first and third approach, just change the < for >. On Monday, just change first for last.

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

  • Thank you, friend. The second option served me very well.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.