122

What's an idiomatic way to check if an array of strings contains a value in Kotlin? Just like ruby's #include?.

I thought about:

array.filter { it == "value" }.any()

Is there a better way?

Mahozad
  • 18,032
  • 13
  • 118
  • 133
jturolla
  • 6,596
  • 7
  • 26
  • 41

7 Answers7

163

The equivalent you are looking for is the contains operator.

array.contains("value") 

Kotlin offer an alternative infix notation for this operator:

"value" in array

It's the same function called behind the scene, but since infix notation isn't found in Java we could say that in is the most idiomatic way.

Geoffrey Marizy
  • 5,282
  • 2
  • 26
  • 29
  • 2
    When I try this in Kotlin I get a "Type inference failed. The value of the type parameter T should be mentioned in the input Types." private var domainsArray = arrayOf("@ebay.com", "@google.com", "@gmail.com", "@umd.edu") var domainFound = Arrays.asList(domainsArray).contains(email) – AdamHurwitz Aug 03 '17 at 01:30
  • Just write var domainFound = domainsArray.contains(email) or more idiomatic var domainFound = email in domainsArray – Geoffrey Marizy Aug 03 '17 at 13:49
  • Any idea how to compare with ignorecase string? array.contains("value") do not check ignorecase for string. – Viks Oct 20 '21 at 01:12
  • You could map array with the toLowerCase String extension function before calling contains. – Geoffrey Marizy Oct 24 '21 at 17:01
135

You could also check if the array contains an object with some specific field to compare with using any()

listOfObjects.any{ object -> object.fieldxyz == value_to_compare_here }
Amar Jain
  • 1,698
  • 1
  • 14
  • 11
44

Here is code where you can find specific field in ArrayList with objects. The answer of Amar Jain helped me:

listOfObjects.any{ it.field == "value"}
Dimitar
  • 659
  • 7
  • 6
39

Using in operator is an idiomatic way to do that.

val contains = "a" in arrayOf("a", "b", "c")
Gerson
  • 508
  • 5
  • 5
24

You can use the in operator which, in this case, calls contains:

"value" in array

mfulton26
  • 29,956
  • 6
  • 64
  • 88
10

You can use it..contains(" ")

 data class Animal (val name:String)


 val animals = listOf(Animal("Lion"), Animal("Elephant"), Animal("Tiger"))

 println(animals.filter { it.name.contains("n") }.toString())

output will be

[Animal(name=Lion), Animal(name=Elephant)]
Jack
  • 1,825
  • 3
  • 26
  • 43
7

You can use find method, that returns the first element matching the given [predicate], or null if no such element was found. Try this code to find value in array of objects

 val findedElement = array?.find {
            it.id == value.id
 }
 if (findedElement != null) {
 //your code here           
 } 
L.Petrosyan
  • 430
  • 4
  • 12
  • How can you get the position of findedElement value from array? – Aan Oct 26 '21 at 13:23
  • @AnzyShQ you can find a position with a methods `array.indexIf(Object o)` -> will return you index of first founded element, `array.lastIndexOf(Object o)` -> will return you index of last founded element. You can also use predicate too, etc `array.indexOfLast { it.name == "test " }` or `array.indexOfFirst { it.name == "test "}` – L.Petrosyan Oct 27 '21 at 13:10