k
yes
h
what have you tried
k
val result = list1.intersect(list2)
some lamnba too, but i'm not confortable with those again
m
If you want all elements that are not common between the two lists, you could do
list1.union(list2) - list1.intersect(list2)
👍 1
k
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
why dont u try it out
k
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
The code above gives all elements of
list1
which are not in
list2
, and all elements in
list2
which are not in
list1
.
k
yes, I saw that but that 's not what I need ^^
a
It sounds as if you do want a.intersect(b) except maybe only the first element of that result?
k
yes I think u've got the idea 🙂
Copy code
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
well yea so you want
a.intersect(b)[0]
k
I found
Copy code
a.intersect(b).first()
it's working too
thx!
a
ah yea [0] probably doesn't even work depending on what types I guess; np
k
nope [0] doesn't work but . first() is ^^
so much better than my horrible loops 😂
a
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
thx a lot 👍
i
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
thx! lambda are so obvious when I read it, but I still need to practice them 😁