Iterating two list with different objects and comparing one parameter

时间:2019-03-17 22:47:04

标签: android kotlin

I have two list declared as :

private var answersList: MutableSet<AnswerRequest> = mutableSetOf()
private var answerListCreated: MutableSet<AnswerResponse> = mutableSetOf()

AnswerRequest only contains a String inside, and AnswerResponse contains more attributes, like Int, String, Boolean, so what I'm trying to achieve is to iterate on answerListCreated and find inside the same String as answerList for instance.

answerList contains : "Hello","Bye","Cucu"

answerListCreated contains ; {"Hello",3,false},{...}

So I'm trying to find the "Hello" word in answerListCreated so I can get the id.

What I'm doing from now is :

answerListCreated.forEach { a1->
            correctAnswersList.forEach { a2->
                if(a1.answerToQuestion == a2.answerToQuestion){
                    //Get the a1.id
                }
            }
        }

But I don't know why, is going to the if statement twice, it it's a Set this list doesn't have repeated ones, right? Is there any other way to do that?

EDIT

List1 Just strings List2 Objects result from API, they have the same String of List1, but then I can check the Id.

data class ListExample1 (val text : String)
data class ListExample2 (val text : String, val id: Int, val exist: Boolean)

0 个答案:

没有答案