Bean:
package demo;
import test.Test;
public class SkuVo implements Test{
private Long skuId;
private String productName;
private Long brandStoreSn;
public SkuVo(Long skuId, String productName, Long brandStoreSn) {
super();
this.skuId = skuId;
this.productName = productName;
this.brandStoreSn = brandStoreSn;
}
public Long getSkuId() {
return skuId;
}
public void setSkuId(Long skuId) {
this.skuId = skuId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public Long getBrandStoreSn() {
return brandStoreSn;
}
public void setBrandStoreSn(Long brandStoreSn) {
this.brandStoreSn = brandStoreSn;
}
@Override
public String toString() {
return "SkuVo [skuId=" + skuId + ", productName=" + productName + ", brandStoreSn=" + brandStoreSn + "]";
}
@Override
public Long id() {
// TODO Auto-generated method stub
return this.skuId;
}
}
每个bean要实现Test接口:
package test;
public interface Test {
public Long id();
}
工具类:
package test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Util {
public static <T, t> Map<Long, List<t>> Get(T t,Object skuVoList) {
/* 2、分组算法 **/
Map<Long, List<t>> skuIdMap = new HashMap<>();
for (T skuVo : (List<T>)skuVoList) {
List<t> tempList = skuIdMap.get(((Test) skuVo).id());
/* 如果取不到数据,那么直接new一个空的ArrayList **/
if (tempList == null) {
tempList = new ArrayList<>();
tempList.add((t) skuVo);
skuIdMap.put(((Test) skuVo).id(), tempList);
} else {
/* 某个sku之前已经存放过了,则直接追加数据到原来的List里 **/
tempList.add((t) skuVo);
}
}
return skuIdMap;
}
}
欢迎大神前来指导。