...
|
...
|
@@ -8,48 +8,85 @@ import java.util.*; |
|
|
@Service
|
|
|
public class ProductListSortHelper {
|
|
|
|
|
|
public List<Map<String, Object>> sortProductList(List<Map<String, Object>> product_list) {
|
|
|
ProductListSortKey<Map<String, Object>> productSortKey = new ProductListSortKey<Map<String, Object>>() {
|
|
|
@Override
|
|
|
public String getSortKey(Map<String, Object> product) {
|
|
|
int brandId = MapUtils.getInteger(product, "brand_id", 0);
|
|
|
int small_sort_id = MapUtils.getInteger(product, "small_sort_id", 0);
|
|
|
return brandId + "_" + small_sort_id;
|
|
|
}
|
|
|
@Override
|
|
|
public int getMaxCount() {
|
|
|
return 2;
|
|
|
}
|
|
|
};
|
|
|
return this.sortProductList(product_list,productSortKey);
|
|
|
}
|
|
|
|
|
|
|
|
|
public <T> List<T> sortProductList(List<T> product_list, ProductListSortKey<T> productSortKey) {
|
|
|
if (product_list == null || product_list.isEmpty()) {
|
|
|
return product_list;
|
|
|
}
|
|
|
List<T> results = new ArrayList<T>();
|
|
|
while (!product_list.isEmpty()) {
|
|
|
results.addAll(getTempProductMapList(product_list, productSortKey));
|
|
|
}
|
|
|
return results;
|
|
|
}
|
|
|
|
|
|
private <T> List<T> getTempProductMapList(List<T> product_list, ProductListSortKey<T> productSortKey) {
|
|
|
List<T> tempResults = new ArrayList<T>();
|
|
|
Map<String, Integer> tempKeyCount = new HashMap<String, Integer>();
|
|
|
Iterator<T> iterator = product_list.iterator();
|
|
|
while (iterator.hasNext()) {
|
|
|
T product = iterator.next();
|
|
|
String sortKey = productSortKey.getSortKey(product);
|
|
|
int count = tempKeyCount.getOrDefault(sortKey, 1);
|
|
|
if (count <= productSortKey.getMaxCount()) {
|
|
|
tempResults.add(product);
|
|
|
iterator.remove();
|
|
|
}
|
|
|
tempKeyCount.put(sortKey, count + 1);
|
|
|
}
|
|
|
return tempResults;
|
|
|
}
|
|
|
public List<Map<String, Object>> sortProductList(List<Map<String, Object>> product_list) {
|
|
|
ProductListSortKey<Map<String, Object>> productSortKey = new ProductListSortKey<Map<String, Object>>() {
|
|
|
@Override
|
|
|
public String getSortKey(Map<String, Object> product) {
|
|
|
int brandId = MapUtils.getInteger(product, "brand_id", 0);
|
|
|
int small_sort_id = MapUtils.getInteger(product, "small_sort_id", 0);
|
|
|
return brandId + "_" + small_sort_id;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int getMaxCount() {
|
|
|
return 2;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int getMaxLength() {
|
|
|
return 8;
|
|
|
}
|
|
|
};
|
|
|
return this.sortProductList(product_list, productSortKey);
|
|
|
}
|
|
|
|
|
|
public <T> List<T> sortProductList(List<T> product_list, ProductListSortKey<T> productSortKey) {
|
|
|
if (product_list == null || product_list.isEmpty()) {
|
|
|
return product_list;
|
|
|
}
|
|
|
List<T> results = new ArrayList<T>();
|
|
|
while (!product_list.isEmpty()) {
|
|
|
results.addAll(getTempProductMapList(product_list, productSortKey));
|
|
|
}
|
|
|
return results;
|
|
|
}
|
|
|
|
|
|
private <T> List<T> getTempProductMapList(List<T> product_list, ProductListSortKey<T> productSortKey) {
|
|
|
List<T> tempResults = new ArrayList<T>();
|
|
|
Map<String, Integer> tempKeyCount = new HashMap<String, Integer>();
|
|
|
Iterator<T> iterator = product_list.iterator();
|
|
|
while (iterator.hasNext()) {
|
|
|
T product = iterator.next();
|
|
|
String sortKey = productSortKey.getSortKey(product);
|
|
|
int count = tempKeyCount.getOrDefault(sortKey, 1);
|
|
|
if (count <= productSortKey.getMaxCount()) {
|
|
|
tempResults.add(product);
|
|
|
iterator.remove();
|
|
|
}
|
|
|
tempKeyCount.put(sortKey, count + 1);
|
|
|
if (tempResults.size() >= productSortKey.getMaxLength()) {
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
return tempResults;
|
|
|
}
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
List<Integer> list = new ArrayList<>();
|
|
|
for(int i =1 ;i<=10;i++){
|
|
|
list.add(i);
|
|
|
list.add(i);
|
|
|
list.add(i);
|
|
|
list.add(i);
|
|
|
}
|
|
|
List<Integer> newList = new ProductListSortHelper().sortProductList(list, new ProductListSortKey<Integer>() {
|
|
|
@Override
|
|
|
public String getSortKey(Integer product) {
|
|
|
return product.toString();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int getMaxCount() {
|
|
|
return 3;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int getMaxLength() {
|
|
|
return 10;
|
|
|
}
|
|
|
});
|
|
|
|
|
|
System.out.println(newList);
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|