避免重复(循环)

时间:2015-04-01 20:32:16

标签: java for-loop

我正在制作一个使用两个文本文件(两个表)的程序,并对它们执行基本关系代数(联合,差异,交集和连接)。我正在使用Hashmaps,每次都保存值(键/值),但我想知道如何为每个操作使用一个主“for循环”而不是4。 这是我的代码:

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());
    }
}

2 个答案:

答案 0 :(得分:1)

我认为你必须使用至少2个for循环,你可以这样做:

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());
    }
}

答案 1 :(得分:0)

您仍然可以使用集合进行设置/值对:

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);

这是输出:

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

这是一个基本的SetEntry实现:

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;
}