Better way to compare d in the if statment. What if i need to compare it to say 100 values?

Viewed 86

Better way to compare d in the if statement. What if i need to compare it to say 100 values?

int d = 0;
if(d == 3 || d == 8 || d == 1) {
   System.out.println("d is one of the special numbers!");
}
5 Answers

You could create a list to contain the numbers, and then use that list for filtering in the if statement:

List<Integer> nums = new ArrayList<>();
nums.add(1);
nums.add(3);
nums.add(8);

if (nums.contains(d)) {
    System.out.println("d is one of the special numbers!");
}

This might not perform as well as having the numbers directly in the if statement. But, from a code readability/maintenance point of view, it is a better option IMO.

Or, we could use a HashSet, which might benefit from constant lookup time:

Set<Integer> set = new HashSet<>();

Then, after populating with the numbers, we would call set.contains(d) to check for the number.

Because your elements will be unique, storing them in hashset is better.

Have a look at: Hash Set and Array List performances

Set<Integer> mySet = new HashSet<Integer>();
mySet.add(2);
mySet.add(3);
mySet.add(4);
....

if(mySet.contains(d)) {
   System.out.println(d + " is one of the special numbers!");
}

You could create a list of special numbers and then iterate over them comparing against each value in the list:

List<Integer> specialNumbers = Arrays.asList('1', '3', '8');
        int d = '7';
        for (Integer specialNumber : specialNumbers) {
            if (userInput == specialNumber) {
                System.out.println("You've entered one of the special numbers!")
            }
        }

If you can't use an array or list, use a switch:

switch (d) {
    case 1:
    case 3:
    case 8:  System.out.println("d is one of the special numbers");
             break;
    default: break; // Fall through in case d isn't a special number
}

break is an important keyword here, case basically goes through each value and checks if d is equal, and then executes any statements it finds after matching 'd' until it reaches a break statement. For example,if I'd put:

switch (d) {
    case 1: System.out.println("d equals 1");
    case 3: System.out.println("d equals 3");
            break;
    /* More examples*/
}

and had tested it when d = 1 it would print "d equals 1" and "d equals 3."

Don't rely on switches for large sets of data, use a for loop instead.

Integer a = 5;
List<Integer> ints = Arrays.asList(1,2,3,4,5,6);
Stream.of(a).anyMatch(ints::contains); // this will return boolean

If you want to pass not only one value, but list of values, and you don't want to change your code, you can use, code below.

List<Integer> ints = Arrays.asList(1,2,3,4,5,6);
Stream.of(1,2,3,4,5).allMatch(ints::contains); // true;
Stream.of(1,2,7).allMatch(ints::contains); // false;

Also, please be aware of NPE, if you pass to Stream.of() null, there will be NullPointerException.

But isn't problem if you use

Optional.of(ints).orElse(new ArrayList<>()).stream().allMatch(ints::contains)

I would prefer to use apache-common collections utils for my code, there is Collections.emptyIfNull().stream() ... good method.

Related