### Question:
Apriori { public static <T> Ruleset<T> buildRules(Collection<Set<T>> data, double minSupport, double minConfidence) { List<Rule<T>> rules = new ArrayList<>(); Map<Set<T>, Double> itemsets = extractItemsets(data, minSupport); for (Entry<Set<T>, Double> itemset : itemsets.entrySet()) { Set<T> l_k = itemset.getKey(); if (l_k.size() < 2) { continue; } double support = itemset.getValue(); Set<Set<T>> H_m = getSingleItemSets(l_k); for (Set<T> Y : H_m) { Set<T> X = new HashSet<>(l_k); X.removeAll(Y); double confidence = confidence(X, Y, data); if (confidence >= minConfidence) { double lift = confidence / support(Y, data); rules.add(new Rule<>(X, Y, support, confidence, lift)); } } for (int m = 1; m < l_k.size() - 1; m++) { Set<Set<T>> H_m_plus_1 = aprioriGen(H_m); Iterator<Set<T>> iterator = H_m_plus_1.iterator(); while (iterator.hasNext()) { Set<T> Y = iterator.next(); Set<T> X = new HashSet<>(l_k); X.removeAll(Y); double confidence = confidence(X, Y, data); if (confidence >= minConfidence) { double lift = confidence / support(Y, data); rules.add(new Rule<>(X, Y, support, confidence, lift)); } else { iterator.remove(); } } H_m = H_m_plus_1; } } Collections.sort(rules); return new Ruleset<>(rules); } private  Apriori(); static Map<Set<T>, Double> extractItemsets(Collection<Set<T>> data, double minSupport); static Ruleset<T> buildRules(Collection<Set<T>> data, double minSupport, double minConfidence);  }### Answer:
@Test public void testGenerateAssociationRules() { Set<Integer> s1 = newHashSet(1, 2, 5); Set<Integer> s2 = newHashSet(2, 4); Set<Integer> s3 = newHashSet(2, 3, 6); Set<Integer> s4 = newHashSet(1, 2, 4); Set<Integer> s5 = newHashSet(1, 3); Set<Integer> s6 = newHashSet(2, 3); Set<Integer> s7 = newHashSet(1, 3); Set<Integer> s8 = newHashSet(1, 2, 3, 5); Set<Integer> s9 = newHashSet(1, 2, 3); List<Set<Integer>> data = Arrays.asList(s1, s2, s3, s4, s5, s6, s7, s8, s9); Ruleset<Integer> rules = Apriori.buildRules(data, 0.1, 0.1); assertEquals(.5, rules.getRule(newHashSet(1, 2), newHashSet(5)).getConfidence(), DELTA); assertEquals(1., rules.getRule(newHashSet(1, 5), newHashSet(2)).getConfidence(), DELTA); assertEquals(1., rules.getRule(newHashSet(2, 5), newHashSet(1)).getConfidence(), DELTA); assertEquals(.33, rules.getRule(newHashSet(1), newHashSet(2, 5)).getConfidence(), DELTA); assertEquals(.29, rules.getRule(newHashSet(2), newHashSet(1, 5)).getConfidence(), DELTA); assertEquals(1., rules.getRule(newHashSet(5), newHashSet(1, 2)).getConfidence(), DELTA); }