https://kotlinlang.org logo
Title
k

kaizo

09/28/2018, 2:47 PM
yes
h

Hamza

09/28/2018, 2:47 PM
what have you tried
k

kaizo

09/28/2018, 2:48 PM
val result = list1.intersect(list2)
some lamnba too, but i'm not confortable with those again
m

marstran

09/28/2018, 2:48 PM
If you want all elements that are not common between the two lists, you could do
list1.union(list2) - list1.intersect(list2)
👍 1
k

kaizo

09/28/2018, 2:50 PM
I need to find the first term of the second list that match the first list
ur 'sugestion give me the element of list 2 that aren't in list1, right?
h

Hamza

09/28/2018, 2:53 PM
why dont u try it out
k

kaizo

09/28/2018, 2:53 PM
I've just try it
it give me number that aren't in the first list
I 'v tought that ther was a kotlin way with lanmbda to do this
i'll keep looking
thx
m

marstran

09/28/2018, 3:01 PM
The code above gives all elements of
list1
which are not in
list2
, and all elements in
list2
which are not in
list1
.
k

kaizo

09/28/2018, 3:09 PM
yes, I saw that but that 's not what I need ^^
a

Alowaniak

09/28/2018, 3:31 PM
It sounds as if you do want a.intersect(b) except maybe only the first element of that result?
k

kaizo

09/28/2018, 3:34 PM
yes I think u've got the idea 🙂
var result: Long = 0
    for (element in r1) {
        for (element2 in r2) {
            if (element2 == element)
                result=element2
            break
        }

    }
    return result
I've done this, it's working but but not pretty ^^
a

Alowaniak

09/28/2018, 3:36 PM
well yea so you want
a.intersect(b)[0]
k

kaizo

09/28/2018, 3:37 PM
I found
a.intersect(b).first()
it's working too
thx!
a

Alowaniak

09/28/2018, 3:39 PM
ah yea [0] probably doesn't even work depending on what types I guess; np
k

kaizo

09/28/2018, 3:40 PM
nope [0] doesn't work but . first() is ^^
so much better than my horrible loops 😂
a

Alowaniak

09/28/2018, 3:44 PM
Note that you'll get a NoSuchElementException if it's empty though, you might want to do
a.intersect(b).firstOrNull() ?: 0
(to match your loops) or whatever you want as default value (Oh and btw for multiline code in slack use triple `)
k

kaizo

09/28/2018, 3:47 PM
thx a lot 👍
i

ilya.gorbunov

09/30/2018, 12:10 AM
If you want just the first item that is in both a and b, it would be more efficient to use
a.first { it in b }
(or
firstOrNull
)
k

kaizo

10/01/2018, 7:20 AM
thx! lambda are so obvious when I read it, but I still need to practice them 😁