yacine benzmane
yacine benzmane

Reputation: 4208

Avoiding repetition (for loop)

I'm making a program that use two text files (two tables), and perform basic relational algebra (Union, difference, intersection,and join) on them. I'm using a Hashmaps, to save the values (keys/values) each time, but I wonder how can I use one main "for loop" instead of 4 for each operation. This is my code:

for (Map.Entry<Integer, String> htEntries : map.entrySet()) {
    if(map2.containsKey(htEntries.getKey()) && map2.get(htEntries.getKey()).equals(htEntries.getValue())){
        inter.put( htEntries.getKey(), htEntries.getValue());
    }
}
for (Map.Entry<Integer, String> joinEntries : map.entrySet()) {
    if(map2.containsKey(joinEntries.getKey())){
        join.put( joinEntries.getKey(), joinEntries.getValue());
    }
}
for (Map.Entry<Integer, String> diffEntries : map.entrySet()) {
    if(!map2.containsKey(diffEntries.getKey())){
        diff.put( diffEntries.getKey(), diffEntries.getValue());
    } 
}
for (Map.Entry<Integer, String> diffEntries2 : map2.entrySet()) {
    if(!map.containsKey(diffEntries2.getKey())){
        diff2.put( diffEntries2.getKey(), diffEntries2.getValue());
    }
}

Upvotes: 0

Views: 222

Answers (2)

JimW
JimW

Reputation: 186

You can still do set/value pairs with sets:

Set<SetEntry> setA = new HashSet<>();
setA.add(new SetEntry("a", 1));
setA.add(new SetEntry("b", 2));
setA.add(new SetEntry("c", 2));
setA.add(new SetEntry("d", 1));

Set<SetEntry> setB = new HashSet<>();
setB.add(new SetEntry("a", 1));
setB.add(new SetEntry("b", 2));
setB.add(new SetEntry("e", 1));
setB.add(new SetEntry("f", 2));

Set<SetEntry> union = new HashSet<>(setA);
union.addAll(setB);
System.out.println("Union: " + union);

Set<SetEntry> intersection = new HashSet<>(setA);
intersection.retainAll(setB);
System.out.println("Intersection: " + intersection);

Set<SetEntry> difference = new HashSet<>(setA);
difference.removeAll(setB);
System.out.println("Difference: " + difference);

Here is the output:

Union: [a->1, b->2, c->2, d->1, e->1, f->2]
Intersection: [a->1, b->2]
Difference: [c->2, d->1]

Here is a base SetEntry implementation:

private class SetEntry {
private final String key;
private final int value;

public SetEntry(String key, int value) {
    this.key = key;
    this.value = value;
}

public String getKey() {
    return key;
}

public int getValue() {
    return value;
}

// Just use the key for equality
@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    SetEntry setEntry = (SetEntry) o;
    return key.equals(setEntry.key);
}

@Override
public int hashCode() {
    return key.hashCode();
}

@Override
public String toString() {
    return key+"->"+value;
}

Upvotes: 0

Roy Shmuli
Roy Shmuli

Reputation: 5019

I think you must use a least 2 for loops, you can do this:

for (Map.Entry<Integer, String> htEntries : map.entrySet()) {
    if(map2.containsKey(htEntries.getKey()) {
      join.put( htEntries.getKey(), htEntries.getValue());
      if (map2.get(htEntries.getKey()).equals(htEntries.getValue())) {
        inter.put(htEntries.getKey(), htEntries.getValue());
      } 
    } else {
       diff.put( htEntries.getKey(), htEntries.getValue());
    }
}

for (Map.Entry<Integer, String> diffEntries2 : map2.entrySet()) {
    if(!map.containsKey(diffEntries2.getKey())){
        diff2.put(diffEntries2.getKey(), diffEntries2.getValue());
    }
}

Upvotes: 1

Related Questions