-3

what is the most elegant way to join two Hashtables?

I have this HashTable and I cannot use an Array or List or Set instead, because it messes up dealing with the custom-class. At least that's what I think.

        **Hashtable<ItemSet, Integer> frequentItemSetL1 = new Hashtable<>();**
        for (Map.Entry<ItemSet, Integer> entry : candidateItemSetC1.entrySet()) {
        if (entry.getValue() >= supportThreshold) {
            frequentItemSetL1.put(entry.getKey(), entry.getValue());
        }

which contains: Key-value-pairs.

  • Key = Number of an Itemset
  • Value = Number of occurrences of that itemset in the data

Looks like this:

{1}, 3
{2}, 23
{3}, 7
{4}, 18
..... 

I need the KEYS of frequentItemSetL1 to join with THEMSELVES;

Result should be:

{1, 2}
{1, 3}
{1, 4}
{2, 3}
{2, 4}
{3, 4}

I experimented with two for-loops and KeySets but did not get far. Could need some fresh ideas.

Tried this:

for (int i = 0; i < frequentItemSetL1.size(); i++) {
        Integer item1 = frequentItemSetL1.get(i);
        for (int j = i + 1; j < frequentItemSetL1.size(); j++) {
            Integer item2 = frequentItemSetL1.get(j);

            // Create a new candidate by combining itemset1 and itemset2
            candidateItemSetC2.put(item1, item2);
        }
Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
ld4795
  • 7
  • 2
  • Possible duplicate of [Find all subsets of length k in an array](http://stackoverflow.com/questions/12548312/find-all-subsets-of-length-k-in-an-array) – Marvin Apr 14 '17 at 15:44
  • When you say "Result should be: {1, 2} ... what class is this that you are expecting? A string? Some sort of array of tuples? – ControlAltDel Apr 14 '17 at 15:48
  • There's a huge amount of ambiguity and some evidence of incorrect assumptions in your question. You say `Key = Number of an Itemset` but in the code the key is the actual `Itemset`, not its "number" whatever that means. The code indicates that the output is itself a `Map`, but your expected output contains duplicate keys, which is not possible. The question seems to be based on an incorrect understanding of some basic topics, and is not answerable in its present form. – Jim Garrison Apr 14 '17 at 15:59
  • Key refers to a so called Itemset, which is e.g. {1} And I took this code from an implemented version of Apriori, so it cannot be that wrong, it just does not work well with Maps because it was based on arrays before and I try to rewrite it :) – ld4795 Apr 14 '17 at 16:15

1 Answers1

0

Since nobody was able to help, just to clarify and help others, what I was looking for is this:

List<ItemSet> candidateItemSetC2 = new ArrayList<>();
    Hashtable<ItemSet, Integer> frequentItemSetL2 = new Hashtable<>();
    // For each itemset I1 and I2 of level k-1
    for (ItemSet is : frequentItemSetL1.keySet()) {
        for (ItemSet is2 : frequentItemSetL1.keySet()) {
            if (is.equals(is2)) {
                continue;
            }
            if (is.set.length != is2.set.length) {
                continue;
            }
            boolean shouldJoin = true;
            for (int i = 0; i < is.set.length - 1; i++) {
                if (is.set[i] != is2.set[i]) {
                    shouldJoin = false;
                }
            }

            if (shouldJoin == false) {
                continue;
            }

            int[] join = new int[is.set.length + 1];
            for (int i = 0; i < is.set.length; i++) {
                join[i] = is.set[i];
            }
            join[join.length - 1] = is2.set[is2.set.length - 1];
            Arrays.sort(join);
            ItemSet joinedSet = new ItemSet(join);

            if (!candidateItemSetC2.contains(joinedSet)) {
                candidateItemSetC2.add(joinedSet);
            }


            for (int p = 0; p < candidateItemSetC2.size(); p++) {
                //System.out.print(candidateItemSetC2.get(p));
            }


        }
ld4795
  • 7
  • 2
  • To understand what you are trying to achieve, could you make it a [mcve] ? This should include initializing `frequentItemSetL1` with some test data. – c0der Nov 26 '17 at 15:16