[英]Split ArrayList into multipleList based on the value in the list
我有一個MyObjects數組列表,我想根據對象的標題值拆分列表。
List<MyProduct> productList = instance.getMyProductList();
這是我的清單,其中包含許多產品。
product = productList.get(i);
String tittle = product.getTitle();
我想將我的arraylist分成幾個具有相似產品標題的列表。
請告訴我。 謝謝。
用番石榴:
ListMultimap<String, MyProduct> result = Multimaps.index(productList, new Function<String, Product>() {
@Override
public String apply(Product input) {
return input.getTitle();
}
});
使用簡單的舊Java集合:
Map<String, List<MyProduct>> result = new HashMap<>();
for (MyProduct p : productList) {
List<MyProduct> list = result.get(p.getTitle());
if (list == null) {
list = new ArrayList<>();
result.put(p.getTitle(), list);
}
list.add(p);
}
兩者都假定“相似”標題實際上是“相等”標題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.