lezzbiic
lezzbiic

Reputation: 11

if too much condition Java

I have this code

if(beforeModify.get(i).equals("a")|beforeModify.get(i).equals("e")|beforeModify.get(i).equals("i")|
                    beforeModify.get(i).equals("o")|beforeModify.get(i).equals("u")|beforeModify.get(i).equals("á")|
                    beforeModify.get(i).equals("é")|beforeModify.get(i).equals("í")|beforeModify.get(i).equals("ě")|
                    beforeModify.get(i).equals("y")|beforeModify.get(i).equals("ý")|beforeModify.get(i).equals("ů")|
                    beforeModify.get(i).equals("ú"))

Can I do it better?

Upvotes: 0

Views: 520

Answers (6)

mattiejas
mattiejas

Reputation: 1

You could try to solve it with an regular expression using the String.matches(String regex) method.

String str = beforeModify.get(i);
if (str.matches("[aeiouáéíěyýůú]") {
    // do whatever you wanna do
}   

Also calling beforeModify.get(i) over and over again is not necessary as it probably yields the same result.

Upvotes: 0

Andy Turner
Andy Turner

Reputation: 140534

You can write it as a List.contains call:

if (Arrays.asList("a", "e", etc).contains(beforeModify.get(i))

But you could also pre-build a Set, and use that:

// Build once, keep the set to reuse.
Set<String> set = new HashSet<>(Arrays.asList("a", "e", etc));

if (set.contains(beforeModify.get(i)) 

The HashSet has the advantage of being O(1) in the number of elements; a List would be O(n).


Also: you are using |, rather than ||. The former will evaluate all operands, the latter will stop as soon as one of them is matched. You don't need to evaluate them all, as String.equals on a literal parameter has no side effect. Use ||.

Upvotes: 12

Marc Van Deuren
Marc Van Deuren

Reputation: 121

You could also do it using java 8 streams.

beforeModify.stream()
.filter(x -> x.matches("[aeiouáéíěyýůú]"))
.forEach(x -> {

    /* Your code here */

});

Upvotes: 0

Youcef LAIDANI
Youcef LAIDANI

Reputation: 60046

I like the solution of Andy Turner here also other solutions using String instead of a List of Set :

Solution 1

String str = "aeiouáéíěyýůú";//put the characters you want to check in a string
String s = "z";
//you can use String.contains
if (s.length() == 1 && str.contains(s)) {
    //...
} 

Solution 2

//you can also use replaceAll non that char to check if it is correct or not
if (s.length() == 1 && !str.replaceAll("[^" + s + "]", "").isEmpty()) {
    //...
} 

Solution 3

// You can either use matches
if (str.matches(".*" + s + ".*")) {
    //...
}

ideon demo

Upvotes: 1

Shriyansh Gautam
Shriyansh Gautam

Reputation: 1082

You can use match() method for regex

String input = beforeModify.get(i);
if(input.matches("a*e*i*o*u*á*é*í*ě*y*ý*ů*ú*") && input.length()==1)

or you could just use

if(input.matches("[aeiouáéíěyýůú]"))

Upvotes: 0

Ramiz
Ramiz

Reputation: 464

I'd say it is classical case for a switch:

switch(beforeModify.get(i)) {
    case "a":
    case "e":
// etc... 
}

it looks also more readable and performs as fast as your if

Upvotes: 1

Related Questions