0

I have to two lists, one named keys for example {a,b,c,d} and the other one named values for example {e,f,g,h}, for simplicity I've written just elements [a-d] and [e-h] but in practice the lists can be of any length altough always the two lists will have the same amount of items.(for example keys {a...z} values {0...25})

List<Character> keys = new ArrayList<Character>();
keys.add('a');
keys.add('b');
keys.add('c');
keys.add('d');

List<Character> values = new ArrayList<Character>();
values.add('e');
values.add('f');
values.add('g');
values.add('h');

I need to create hashMaps objects one by one containig exactly one combination of all the keys and all the values, so for example: one hashMap would be

HashMap<Character,Character> keys_values = new HashMap<Character,Character>();
keys_values.put(keys.get('a'), values.get('e'));
keys_values.put(keys.get('b'), values.get('f'));
keys_values.put(keys.get('c'), values.get('g'));
keys_values.put(keys.get('d'), values.get('h'));

Then in a different hashMap I need to store a different combination for example:

keys_values.put(keys.get('a'), values.get('h'));
keys_values.put(keys.get('b'), values.get('e'));
keys_values.put(keys.get('c'), values.get('f'));
keys_values.put(keys.get('d'), values.get('g'));

And so on.. so for this example of 4 items there should be 4! different hashMaps (I think!) , and each of them with one combination of the elements.

I've thought about doing two nested for loops, but this doesnt give me the results that I'm looking for, because I get several objects with the same key inside the same hashmap.

J. Bend
  • 299
  • 2
  • 3
  • 14
  • Why are you doing this? Can't help feeling this is a bad solution to an underlying problem. – Duncan Jones Apr 08 '14 at 15:47
  • Is this an academic exercise ? – chronodekar Apr 08 '14 at 15:51
  • lets assume there are two items.. `{1,2}` and `{3,4}`.. so will it be `2!`? I can come up these possibilities `{13,14,23,24}`? what is the logic of `4!`? – sakura Apr 08 '14 at 15:51
  • It's part of a programming assignment at school – J. Bend Apr 08 '14 at 15:52
  • @sakura that's why I wrote: I think. I dont remember what is the rule of product, maybe it is 4x4? – J. Bend Apr 08 '14 at 15:54
  • @ogren Have you seen similar solutions, such as [Get all possible combinations of characters in an array](http://stackoverflow.com/questions/9521729/get-all-possible-combinations-of-characters-in-an-array)? Why don't you post your current for-loop effort and we can help. – Duncan Jones Apr 08 '14 at 15:55
  • @Duncan It's no good because I need to separate each combination in different objects – J. Bend Apr 08 '14 at 15:56
  • @ogren That's why I said "similar". Look at the logic employed in those answers and see if you can apply it to your case. I assume (perhaps mistakenly) that you're not looking for a copy/paste answer here. – Duncan Jones Apr 08 '14 at 15:57
  • @ogren and 1 more thing.. the combinations are always in factorials so consider the situation when you will need 26 or more hashmaps. – Aditya Peshave Apr 08 '14 at 16:01
  • nope, actually It wasn't solved!!! It;s way to few keys.. – J. Bend Apr 08 '14 at 16:27

0 Answers0