Background
Create a series of SQL JOIN statements using two operands: primary and secondary. The generic form of the JOIN statement is:
JOIN primary primary ON (secondary.id == primary.id)
Problem
The code currently iterates over a list of primary and secondary operands, as follows:
for( Bundle primaryOperand : bundleComparators ) {
for( Bundle secondaryOperand : sortedBundles ) {
The problem is that the nested loop generates the following:
JOIN primary primary ON (secondary.id == primary.id)
JOIN secondary secondary ON (primary.id == secondary.id)
The second join is superfluous and, in this case, causes an error. The duplication can be eliminated with the following hypothetical data structure:
if( !interchangeableMap.contains( primaryOperand, secondaryOperand ) ) {
interchangeableMap.put( primaryOperand, secondaryOperand );
outputJoin( primaryOperand, secondaryOperand );
}
Where interchangeableMap.contains(...)
will return true if primaryOperand
is mapped to secondaryOperand
or secondaryOperand
is mapped to primaryOperand
.
Questions
- Does such a data structure exist in the Java libraries?
- If not, what data structures would you use for its implementation?
Ideas
My first thought is to create a class that contains two HashMap
s. Checking for containment queries the two HashMap
s to see if one map contains the primary and secondary operands or the other map contains the secondary and primary operands. Insertions put the two operand combinations into their respective HashMap
s.
Thank you!