I've tried to iterate over the items in a HashMap MyMap in order to get all the possible combination pairs of 2 keys (nodes):
I used this code:
Iterator Iterator1= MyMap.entrySet().iterator();
while (Iterator1.hasNext() ) {
Map.Entry X = (Map.Entry) Iterator1.next();
NodeX=(String) X.getKey();
Iterator Iterator2= MyMap.entrySet().iterator();
while (Iterator2.hasNext() ) {
Map.Entry Y = (Map.Entry) Iterator2.next();
NodeY= (String) Y.getKey();
System.out.println("{ "+NodeX+" , "+NodeY+" }");
}
}
Each time, the compiler executes first "while loop" successfully, it restarts over again with the first key of the hashmap. During the second "while loop" I want to start NodeY from the following element of currently-chosen NodeX.
Here is my desired output:
- loop 1: (a,b),(a,c),(a,d),(a,e),....
- loop 2: (b,c),(b,d),(b,e),....
- loop3: (c,d),(c,e),.....
...
entry1.next();where doesentry1come from?StringandMap.Entryeverywhere. Secondly, you seem to be interested in only the keys, so usekeySet(), notentrySet(). Lastly, your code doesn't make much sense. Of course the whole map will be iterated in the secondwhile. Are you really sure you want to use a map?Map, seems it would be simpler with aListhere. And the iteration on index would give you a simple solution too.