Home > OS >  Comparing two different Lists based on Ids
Comparing two different Lists based on Ids

Time:06-02

what I am trying to do here is I am trying to find articles by given id from the CustomerFavoriteRepository. I have two different lists (articleList,customerFavList ) and I am comparing the ids of both lists and adding them to the empty array (customFavartList). Does anyone help me with how can I achieve that using for loop? I am getting errors at the point of "customFavartList.add()" in the loop i don't know which value I have to give there. thanks

void main() {
  final articleList = ArticleRepository().getArticleList();
  final customerFavList = CustomerFavoriteRepository().getCustomerFavorite();
  final List<NewCustomerFav> customFavartList = [];

  //this is what i tried 
  for (final articleId in articleList) {
    for (final customArticleId in customerFavList) {
      if (articleId.id == customArticleId.articleId) {
        customFavartList.add();
      }
    }
  }
}

class NewCustomerFav {
  final int id;
  final Article article;
  final int like;

  NewCustomerFav(this.id, this.article, this.like);
}

class ArticleRepository {
  final _articleList = [
    Article(1, 'burger'),
    Article(2, 'pizza'),
    Article(3, 'chicken'),
    Article(4, 'pasta'),
  ];

  List<Article> getArticleList() {
    return _articleList;
  }
}

class CustomerFavoriteRepository {
  final _customerFavoriteList = [
    CustomerFavorite(1, 1, 2),
    CustomerFavorite(2, 2, 2),
    CustomerFavorite(3, 3, 4),
  ];

  List<CustomerFavorite> getCustomerFavorite() {
    return _customerFavoriteList;
  }
}

class CustomerFavorite {
  final int id;
  final int articleId;
  final int like;

  CustomerFavorite(this.id, this.articleId, this.like);
}

class Article {
  final int id;
  final String name;

  Article(this.id, this.name);
}

CodePudding user response:

The simple answer to your question is that you need to construct the NewCustomerFav, and pass it to the add method:

//this is what i tried 
for (final article in articleList) {
  for (final customArticle in customerFavList) {
    if (article.id == customArticle.articleId) {
      // The values are just for example - I'm not certain how
      // you mean to derive `id` or `like` for a NewCustomerFav.
      final fav = NewCustomerFav(0, article, 1);
      customFavartList.add(fav);
    }
  }
}

On a different note, your algorithm can be improved here - Right now it will take on the order of n * m operations to build customFavartList, where n is the number of articles, and m is the number of favorites.

It can be reduced so that the algorithm performs on the order of n m, instead, which will scale much better when there are many articles, and the customer has many favorites.

The way to do it is to create a Set of ids from the favoritesList, something like this:

// Assuming customerFavList is a list of int ids
final customerFavIdSet = Set.from(customerFavList.map((e) => e.articleId));

for (final article in articleList) {
  // Now you can check whether the article is a favorite with this one
  // query, rather than searching the whole list.
  if (customerFavIdSet.contains(article.id) {
    final fav = NewCustomerFav(0, article, 1);
    customFavartList.add(fav);
  }
}
  • Related