2015-04-01 151 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; 
}