根据ID比较两个不同的列表

发布于 2025-02-03 08:45:48 字数 183 浏览 3 评论 0原文

我在这里要做的是,我正在尝试通过CustomerFavoriteRepository的ID找到文章。我有两个不同的列表(Arclelist,Favlist),并且正在比较列表的ID并将其添加到空数组(Favartlist)中。有人帮助我如何实现循环? 我在循环中的“ favartlist.add()”点上遇到错误,我不知道我必须在那里给出哪个值。谢谢

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,FavList ) and I am comparing the ids of both lists and adding them to the empty array (FavartList). Does anyone help me with how can I achieve that using for loop?
I am getting errors at the point of "FavartList.add()" in the loop i don't know which value I have to give there. thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情感失落者 2025-02-10 08:45:48

您问题的简单答案是您需要构造newCustomerFav,然后将其传递到add方法:

//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);
    }
  }
}

在其他注意事项上,您的算法可以在此处进行改进 - 右现在,它将按照n * m操作的顺序来构建customfavartlist,其中n是文章的数量,m是收藏夹的数量。

可以减少它,以便算法在N + M的顺序上执行,而在有很多文章时,该算法的扩展会更好,并且客户有许多收藏夹。

这样做的方法是从FairitesList创建ID的set,类似的是:

// 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);
  }
}

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);
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文