I have List<MainClass> list1 and List<MainClass> list2.
public class MainClass {
private List<Question> questions;
}
public class Question {
private Long questionId;
}
I need to compare the questionId from the list1 with the questionId from the list2. I should get those questionId (as List<Long> ids) from the List1 which are absent in the List2.
I have written next code where I seperatly where I separately get ids from the list1 and list2. Then I have to compare the idsFromList1 and the idsFromList1 to find out a absent Ids. My question: Is the way to get those questionId (as List ids) from the List1 which are absent in the List2 using stream or in some a short, elegant way?
List<Long> idsFromList1= list1.stream().map(mc->mc.getQuestion)).flatMap(List::stream).map(q->q.getQuestionId()).collect(Collectors.toList());
List<Long> idsFromList2= list2.stream().map(mc->mc.getQuestion)).flatMap(List::stream).map(q->q.getQuestionId()).collect(Collectors.toList());