Kotlin: "if item not in list" proper syntax

Use x !in list syntax.

The following code:

val arr = intArrayOf(1,2,3)
if (2 !in arr)
   println("in list")

is compiled down to the equivalent of:

int[] arr = new int[]{1, 2, 3};
// uses xor since JVM treats booleans as int
if(ArraysKt.contains(arr, 2) ^ true) { 
   System.out.println("in list");
}

The in and !in operators use any accessible method or extension method that is named contains and returns Boolean. For a collection (list, set...) , it uses collection.contains method. For arrays (including primitive arrays) it uses the extension method Array.contains which is implemented as indexOf(element) >= 0


The operator for this in Kotlin is !in. So you can do

if (x !in myList) { ... }

You can find this in the official docs about operator overloading.