In a social media, suppose each user is represented like below:
public class User {
private int userId;
private List<User> friendsList;
User(int id) {
this.userID = id;
this.friendsList = new LinkedList<>();
}
void addFriend(User a) {
this.friendsList.add(a);
}
int getUserId() {
return this.userID;
}
List<User> getFriendsList() {
return this.friendsList;
}
}
if A and B are friends(A->B), then A will be added to B's friend list and B will be added to A's friend list.
A.addFriend(B);
B.addFriend(A);
How i can efficiently find two users X and Y are connected by using Java 8 steam methods ?
ie if X->p->q->r->s->Y, then
bool isConnected(User X, User Y) should return true
Here X and Y are connected through common friends p, q, r and s.