Java compare values, best way

Csanchez

I has a method like this

private boolean validGrade(final StringBuilder grade) {
   boolean isValid = false;
   String semester = "semester"; 

   if ((grade.toString().contains("2o") && grade.toString().contains(semester))
                    || (grade.toString().contains("4o") && grade.toString().contains(semester))
                    || (grade.toString().contains("6o") && grade.toString().contains(semester))
                    || (grade.toString().contains("8o") && grade.toString().contains(semester))) {
    isValid = true;
            }
    }

And I want to replace it witn something like this:

private boolean doValidGradoAntComp(final StringBuilder grade) {
        boolean isValid = false;

        switch (grade.toString()) {
        case "2o semester":
            isValid = true;
            break;
        case "4o semester":
            isValid = true;
            break;
        case "6o semester":
            isValid = true;
            break;
        case "8o semester":
            isValid = true;
            break;
        default:
            break;
        }

        return isValid;
    }

And my doubt is: which one is better? Both works in the same way?

Avi

Why not iterate over the possibilities?

private boolean validGrade(final StringBuilder grade) {
    String gradeString = grade.toString();
    return List.of("2o", "4o", "6o", "8o")
        .stream()
        .map(x -> x + " semester")
        .collect(Collectors.toSet())
        .contains(gradeString);
}

Alternatively, if you're not looking for exact matches, do:

private boolean validGrade(final StringBuilder grade) {
    String gradeString = grade.toString();
    return gradeString.contains("semester") && List.of("2o", "4o", "6o", "8o")
        .stream()
        .anyMatch(gradeString::contains);
}

Finally, if your set of matches is inflexible (will always be "2o", "4o", "6o", "8o"), then you can just use a regular expression:

private boolean validGrade(final StringBuilder grade) {
    return grade.toString().matches("[2468]o semester"); //exact match
//  return grade.toString().matches("[2468]o.*semester|semester.*[2468]o"); //loose match
//  return grade.toString().matches(".*([2468]o.*semester|semester.*[2468]o).*"); //equivalent to contains
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Java compare values, best way

From Dev

Best way to compare 2 images in Java jUnit?

From Dev

best way to compare 2 variables with 4 values each

From Dev

Java - Best way to compare two Sets by hashes (via ==)

From Dev

Java Best way to compare strings from 2 lists

From Dev

Best way to compare two dates in Java (day criteria)?

From Dev

Best way to compare two string

From Dev

Best way to compare ASCII value

From Dev

Best way to switch values

From Dev

Is there a way to compare two objects in a List and combine their values that is most optimal?(Java)

From Dev

Java - Best way to go about collecting PAST values

From Dev

Cleaner way to compare array values?

From Dev

Short way of writing compare values

From Dev

Faster way to compare thousands of values

From Dev

compare values in enum java

From Dev

compare values in enum java

From Dev

Best way to compare IP addresses quickly

From Dev

Best way to compare VARCHAR2 with CHAR

From Dev

What is the best way to compare two CultureInfo instances?

From Dev

Best way to compare - Using Sorting or adding to Set

From Dev

Best way to compare Periods in using NodaTime (or alternative)

From Dev

Best way to compare Objects on the top Level

From Dev

Best way to compare the result of a pdo query

From Dev

What is the best way to compare a string to another string?

From Dev

What is the best way to compare two CultureInfo instances?

From Dev

Best way to compare Periods in using NodaTime (or alternative)

From Dev

What is the best way to compare two different enums?

From Dev

What is the best way to compare variables of same class?

From Dev

Best way to compare the elements of an array of objects

Related Related

HotTag

Archive