2015-04-01 223 views
0

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

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

是否有一些理由不只是使用Set? – JimW 2015-04-01 20:50:03

+0

@JimW我需要得到一个“键 - >值”(值的关键),例如{a→1,b→2,c→2,d→1} – yacinebenzmane 2015-04-02 02:15:30

回答

1

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

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