I have a situation whereby I looping over a map twice, not particularly happy with my algorithm and was wondering if there was another way to achieve this. This is a dummed down version of something I am trying to achieve.
import java.util.*;
public class DummyTest{
public static void main(String []args){
Map<String, String> someMap = new HashMap<>();
someMap.put("Key1", "Value1");
someMap.put("Key2", "Value2");
someMap.put("Key3", "Value3");
for (final Map.Entry<String, String> entry : someMap.entrySet()) {
// I think this is a code smell - Is there a way to avoid this inner
// loop and still get the same output?
for (final Map.Entry<String, String> innerLoop : someMap.entrySet())
{
if (entry.getValue() != innerLoop.getValue()) {
System.out.println(entry.getValue() + " " +
innerLoop.getValue());
}
}
}
}
}
Desired Output
Value2 Value1
Value2 Value3
Value1 Value2
Value1 Value3
Value3 Value2
Value3 Value1