import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.alibaba.fastjson.JSON;
import com.cj.supplier.model.SupplierProductImage;
public class test {
public static void main(String[] args){
List<SupplierProductImage> list = new ArrayList<> ();
SupplierProductImage sp = new SupplierProductImage ();
sp.setId (123L);
sp.setImageUrl ("123");
list.add (sp);
SupplierProductImage sp1 = new SupplierProductImage ();
sp1.setId (1234L);
sp1.setImageUrl ("1234");
list.add (sp1);
SupplierProductImage sp2 = new SupplierProductImage ();
sp2.setId (123L);
sp2.setImageUrl ("123dd");
list.add (sp2);
Map<Long, List<SupplierProductImage>> collect = list.stream().collect(Collectors.groupingBy(SupplierProductImage::getId));
System.out.println (JSON.toJSONString (collect));
}
}
本文介绍了一个使用Java Stream API对SupplierProductImage对象列表按ID进行分组的示例。通过创建SupplierProductImage实例并使用stream().collect(Collectors.groupingBy())方法,将列表中的元素按照指定属性进行分组,最后将结果转换为JSON字符串输出。
1906

被折叠的 条评论
为什么被折叠?



