【发布时间】:2020-05-04 20:16:56
【问题描述】:
我有一个 Arraylist,其中包含食谱,每个食谱都包含一个成分列表。食谱显示在 Android 应用的 UI 列表中,用户希望根据他们最喜欢的成分对列表进行排序。
例如这是他们最喜欢的成分列表
1 - Cheese
2 - Potato
3 - Tuna
4 - Chicken
因此,食谱列表应首先显示包含奶酪的所有成分,然后是土豆等。
如何使用 java stream() 存档?
这是我的模型类目前的样子
public class Recipe {
String[] ingredients;
private String description;
private String title;
public String[] getIngredients() {
return ingredients;
}
public void setIngredients(String[] ingredients) {
this.ingredients = ingredients;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
.
public class Ingredient {
String title;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
【问题讨论】:
-
为什么要使用 Java 8 Streams?只需使用
Collections.sort(list);。 -
请显示一些代码。
-
我的意思是你也可以使用
Stream#sorted来获得排序的流。 -
This 可能会有所帮助,但请注意对性能的担忧。
-
@M.S.刚刚添加了一些
标签: java arraylist java-stream