• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Iterating the hash map to form combinations

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to iterate a hashmap and form all possible combinations for example if there are five keys say 1, 2, 3, 4 , 5

I need to form all possible distinct combinations for example (1,2) , (1,3) , (1,2,3) , (2,3,4) etc
Can someone help me with an algorithm for this?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What have you tried so far? Can you describe the process in English before trying to code it?

Hint: A HashMap's keySet() method returns a Set of keys.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please dont' double post.
 
Shruthi Babu
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to use the combination rule of "n!/r!(n-r)!" but here is the pbm according to this if I wanted all the combinations for a particular number of pairs eg all two pair values ... eg 1, 2, 3, 4 the outcome would be (1,2) , (2,3) , (3,4) , (1,3) (1,4) , (2,4)

but I need all possible pairs say all 2 pairs, 3 pairs , 4 pairs etc... is it a good way to wrap it with a loop and pass the "r" value for each loop or is there a way to figure this out. Any help is highly appreciated.
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if the order doesn't matter (i.e. (1,2) is the same as (2,1)), it's pretty simple. count from 0 to 2^(number of elements) - 1. Then, use a bit mask to figure out which to include.

in other words, assuming 3 elements:

[ May 22, 2007: Message edited by: Fred Rosenberger ]
 
The longest recorded flight time of a chicken is 13 seconds. But that was done without this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic