根据 Id 比较两个不同的列表
Comparing two different Lists based on Ids
我在这里要做的是尝试通过给定的 ID 从 CustomerFavoriteRepository 中查找文章。我有两个不同的列表 (articleList,customerFavList),我正在比较两个列表的 ID 并将它们添加到空数组 (customFavartList)。有没有人帮助我如何使用 for 循环实现?
我在循环中的“customFavartList.add()”处出现错误,我不知道我必须在那里给出哪个值。谢谢
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);
}
您的问题的简单答案是您需要构建 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 的数量级上执行,相反,当有很多文章并且客户有很多收藏夹时,它会更好地扩展。
方法是从 favoritesList 创建一个 Set
id,像这样:
// 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);
}
}
我在这里要做的是尝试通过给定的 ID 从 CustomerFavoriteRepository 中查找文章。我有两个不同的列表 (articleList,customerFavList),我正在比较两个列表的 ID 并将它们添加到空数组 (customFavartList)。有没有人帮助我如何使用 for 循环实现? 我在循环中的“customFavartList.add()”处出现错误,我不知道我必须在那里给出哪个值。谢谢
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);
}
您的问题的简单答案是您需要构建 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 的数量级上执行,相反,当有很多文章并且客户有很多收藏夹时,它会更好地扩展。
方法是从 favoritesList 创建一个 Set
id,像这样:
// 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);
}
}