What an idiomatic way to check if a string array contains a value in kotlin? As well as ruby #include?.
#include?
At least about:
array.filter { it == "value" }.any()
Is there a better way?
The equivalent you are looking for is the operator contains .
array.contains("value")
Kotlin offers an alternative infix notation for this statement:
"value" in array
This is the same function that is called backstage, but since infix notation is not found in Java, we can say that this inis the most idiomatic way.
in
, any()
any()
listOfObjects.any{ object -> object.fieldxyz == value_to_compare_here }
You can use the operator in, which in this case calls contains:
contains
Using an operator in is an idiomatic way to do this.
val contains = "a" in arrayOf("a", "b", "c")
Here is the code where you can find a specific field in an ArrayList with objects. Amar Jain's answer helped me:
listOfObjects.any{ it.field == "value"}
Source: https://habr.com/ru/post/1669738/More articles:are there any advantages to using a value class (without methods) and a type alias? - scalaSSIS Poll Execution Status - c #Count () where the counted element is a JOIN back to another table - sqlGet emoji flag by country code - javaОбратный вызов сервера Bokeh из инструментов - pythonВозможно ли вернуть несколько строк в хранимую процедуру Firebird? - sqlDefine a zoom event for a Google Line chart? - javascriptHow to execute a procedure that returns a result set in Firebird - procedureRxJS: combining historical data with an update stream - javascriptSymfony - caching database result sets - cachingAll Articles