根据不同商品组合成一组打促销的商品,计算出一批商品中有多少种组合的计算,刷选出符合组合条件的商品和组合数量的倍数
package com.ghgcn.promotion.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.jflame.toolkit.util.CollectionHelper;
/**
-
@Description: 组合计算过滤工具类
-
@author depeng.wang
-
@date 2018年7月31日 上午11:29:59
*/
public class GroupFilterUtils {/**
-
检测一致的对象和数量
-
@param strList 传入要检测的list,即订单中的skuId组装起来的list
-
比如skuId-A有两个,skuId-B有一个,即结果[skuId-A,skuId-A,skuId-B]
-
@param tjMap 条件对象 组合表达式的组装结果
-
比如skuId-A要n个,skuId-B要m个,即结果{skuId-A=n,skuId-B=m}
-
@return 返回该strList参与满足tjMap的组合倍数结果,或相关数据
*/
public static Map<String,Object> checkMate(List strList, Map<String,Integer> tjMap) {
// 将相同的元素加入到同一个集合当中去
Map<String,List> map = new HashMap<String,List>();
for (String s : strList) {
List sList = map.get(s);
if (sList == null) {
sList = new ArrayList();
sList.add(s);
map.put(s, sList);
} else {
sList.add(s);
map.put(s, sList);
}
}
// 将所有的集合加入到一个集合当中
List<List> tjList = new ArrayList<>(map.values());
// 整合结果的list
List<List> zhjgList = new ArrayList<List>();
boolean istrue = true;
//填充被移除的数据
List list = null;
ok: while (istrue) {
list = new ArrayList();
if (CollectionHelper.isEmpty(tjList)) {
istrue = false;
break ok;
}
Iterator<List> tjIterator = tjList.iterator();
okk: while (tjIterator.hasNext()) {
List tList = tjIterator.next();
if (tList != null && tList.size() > 0) {
String name = tList.get(0);
Integer tj = tjMap.get(name) == null ? 0 : tjMap.get(name);
if (tList.size() >= tj) {
if (name == tjList.get(tjList.size() - 1).get(0) && tj == 0) {
//当走完一轮循环后,进行第二轮循环时,需检验是否还存在符合条件的数据,如果不存在跳出
boolean isAgain = false;
for (List againList : tjList) {
int tjNum = tjMap.get(againList.get(0)) == null ? 0 : tjMap.get(againList.get(0));
if (tjNum > 0) {
isAgain = true;
}
}
if (!isAgain) {
istrue = false;//定义跳出ok循环,下面需要定义跳出okk循环,才能将list填充进zhjgList
}
break okk;
} else {
if (tj > 0) {
Iterator iterator = tList.listIterator();
for (int i = 0; i < tj; i++) {
String s = iterator.next();
iterator.remove();
list.add(s);
}
if (CollectionHelper.isEmpty(tList)) {
tjIterator.remove();
}
} else {
continue;
}
}
} else {
istrue = false;
break ok;
}
} else {
istrue = false;
break ok;
}
}if (CollectionHelper.isNotEmpty(list)) { zhjgList.add(list); }
}
System.out.println(“zhjgList” + zhjgList);
Map<String,Object> resultMap = check(strList, tjMap, zhjgList);
return resultMap;
}
/**
- @param strList 接收的检验list
- @param tjMap 条件对象
- @param zhjgList 整合满足条件tjMap结果的list
*/
public static Map<String,Object> check(List strList, Map<String,Integer> tjMap,
List<List> zhjgList) {
Map<String,Object> resultMap = new HashMap<>();
int fhzhNum = 0;
for (List list : zhjgList) {
boolean issuccess = checktj(list, tjMap);
// 检测符合条件的组合
if (issuccess) {
System.out.println(“符合条件的组合” + list);
for (String str : list) {
strList.remove(str);
}
fhzhNum++;
}
}
// System.out.println("符合的数量: " + fhzhNum);
// System.out.println(“剩余的数量:” + strList);
resultMap.put(“multipeNum”, fhzhNum);
// resultMap.put(“surplusList”, strList);
return resultMap;
}
/**
- 检测条件
- @param list
- @param tjMap 条件对象map
- @return
*/
private static boolean checktj(List list, Map<String,Integer> tjMap) {
boolean issuccess = false;
Map<String,Integer> map = sortListToMap(list);
Set<Entry<String,Integer>> set = tjMap.entrySet();
for (Entry<String,Integer> entry : set) {
Integer sjsl = map.get(entry.getKey());
if (sjsl != null && sjsl >= entry.getValue()) {
issuccess = true;
} else {
issuccess = false;
break;
}
}
return issuccess;
}
/**
- 整理list数据为map格式
- @param list
- @return
*/
private static Map<String,Integer> sortListToMap(List list) {
Map<String,Integer> slMap = new HashMap<String,Integer>();
for (String s : list) {
Integer ss = slMap.get(s);
if (ss == null) {
slMap.put(s, 1);
} else {
slMap.put(s, 1 + ss);
}
}
return slMap;
}
public static void main(String[] args) {
List strList = new ArrayList();
/**
* 条件Map
*/
Map<String,Integer> tjMap = new HashMap<String,Integer>();
strList.add(“988228698920550402”);
strList.add(“928146057023750144”);
strList.add(“928838156639567872”);
strList.add(“928838156639567872”);
strList.add(“928838156639567872”);
strList.add(“928838156639567872”);
strList.add(“928163175547043840”);
strList.add(“928163175547043840”);strList.add("203632276909690884"); strList.add("203632276909690884"); strList.add("927467201283522560"); strList.add("927467201283522560"); tjMap.put("203632276909690884", 1); tjMap.put("927467201283522560", 1); Long start = System.currentTimeMillis(); Map<String,Object> resultMap = checkMate(strList, tjMap); Long end = System.currentTimeMillis(); System.out.println(Integer.parseInt(resultMap.get("multipeNum").toString())); int multipeNum = (int) resultMap.get("multipeNum"); System.out.println("****" + multipeNum); System.out.println(strList);
}
}
执行得出的结果:
-