public class Test {
/**
* 打印一个数组所有的非空子集
*/
public List<String> printAllSubsets(Integer[] array) {
if (null == array || 0 == array.length) {
throw new IllegalArgumentException("数组不能为Null,至少有一个元素");
}
Arrays.sort(array); //进行排序
int len = array.length;
List<String> stringList = new LinkedList<String>();
int allMasks = 1 << len;
// 遍历所有的二进制表示方式
for (int i = 1; i < allMasks; i++) {
//if (i == allMasks - 1) break;
StringBuilder s = new StringBuilder();
for (int j = 0; j < len; j++)
if ((i & (1 << j)) > 0) {
s.append(array[j]+ "|");
}
stringList.add(s.toString());
}
return stringList;
}
public static void main(String[] args) {
Test exam = new Test();
//三个订单
List<String> stringList = exam.printAllSubsets(new Integer[]{1,3,2,4});
List<String> stringList2 = exam.printAllSubsets(new Integer[]{1,3,2});
List<String> stringList3 = exam.printAllSubsets(new Integer[]{1,4,3});
//候选项集,出现次数,每天增量更新没有加入支持度限制(实际中排列组合会很多数据量巨大,根据每天的订单增量计算)
Map<String,Integer> map=new HashMap<String, Integer>();//模拟数据库
for (String s : stringList) {
System.out.println(s);
if(!map.containsKey(s)) {
map.put(s, 1);
}else
{
map.put(s,map.get(s)+1);
}
}
for (String s : stringList2) {
System.out.println(s);
if(!map.containsKey(s)) {
map.put(s, 1);
}else
{
map.put(s,map.get(s)+1);
}
}
for (String s : stringList3) {
System.out.println(s);
if(!map.containsKey(s)) {
map.put(s, 1);
}else
{
map.put(s,map.get(s)+1);
}
}
float support = map.get("1|2|");
System.out.println("支持度"+support);
float confidence = map.get("1|3|")*100/map.get("3|");
System.out.println(confidence+"%的用户购买了3|还购买了1|");
}
}