How to check if all strings are lexically equal in array of strings with Java

2 Answers

0 votes
public class MyClass {
    static boolean strings_are_equal(String[] strings) {
        String string0 = strings[0];
        
        for (String string : strings) {
            if (!string.equals(string0))
                return false;
        }
        return true;
    }
    public static void main(String args[]) {
        String[] array = {"java", "java", "java", "java"};

        System.out.println(strings_are_equal(array));
    }
}





/*
run:

true

*/

 



answered Aug 12, 2023 by avibootz
0 votes
import java.util.Arrays;

public class MyClass {
    public static void main(String args[]) {
        String[] strings = {"java", "java", "java", "java"};
 
        System.out.println(Arrays.stream(strings).distinct().count() < 2);
    }
}
 
 
 
 
 
/*
run:
 
true
 
*/

 



answered Aug 13, 2023 by avibootz
...