Pairing among the members in same list in java
<p>In problem statement, I have 'n' number of families with 'n' number of family members.</p>

<p>eg:</p>

<pre><code>John jane (family 1)
tiya (family 2)
Erika (family 3)
</code></pre>

<p>I have to assign all members in such a way that person should not pair with the his family member. 
and output should be:</p>

<pre><code>John =&gt; tiya
jane =&gt; Erika
tiya =&gt; jane
Erika =&gt; john
</code></pre>

<p>I have created the object <code>Person(name ,familyID, isAllocated)</code>. 
Created the list and added <code>personName_id</code> in this this.</p>

<p>I am thinking to use the map for association. So that <code>john_1</code> will be key and <code>tiya_2</code> will be value. </p>

<p>I am failing to associate those pairs through map. How can I shuffle the members it the list.</p>

<p>Also, It would be nice if anyone could suggest me the better solution.</p>

<p><strong>Code:</strong></p>

<p>Getting person:</p>

<pre><code>public static List getperson()
{
    Scanner keyboard = new Scanner(System.in);
    String line = null;
    int count = 0;

    List &lt;Person&gt; people = new ArrayList&lt;&gt;();

    while(!(line = keyboard.nextLine()).isEmpty()) {
      String[] values = line.split("\\s+");
      //System.out.print("entered: " + Arrays.toString(values) + "\n");
      int familyid = count++;
      for(String name :values)
      {
        Person person = new Person();
        person.setFamilyId(familyid);
        person.setName(name);
        person.setAllocated(false);

        people.add(person);
      }
    }
    return people;
}
</code></pre>

<p>Mapping:</p>

<pre><code>public static List mapGifts(List pesonList) 
{
    Map&lt;String , String&gt; personMap = new HashMap&lt;String , String&gt;();

    Iterator&lt;Person&gt; itr = pesonList.iterator();
    Iterator&lt;Person&gt; itr2 = pesonList.iterator();
    List&lt;String&gt; sender =  new ArrayList&lt;&gt;();

    while(itr.hasNext())
        {
          Person p = itr.next();

              sender.add(p.getName()+"_"+p.getFamilyId());
              personMap.put(p.getName()+"_"+p.getFamilyId(), "");
             // p.setAllocated(true);

        }

          while(itr2.hasNext())
          {
              /*if(p.isAllocated())
              {*/

              // Separate Sender name and id from sender list
              //check this id match with new p1.getFamilyId()

              for(String sendername :sender)
              {
                 // System.out.println("Sender "+sendername);
                  personMap.put(sendername, "");

              String[] names = sendername.split("_");
              String part1 = names[0]; // 004
              String familyId = names[1]; // 004


              Person p2 = itr2.next();
              System.out.println(p2.getFamilyId() +"   "+familyId +" "+p2.isAllocated());

              if(p2.isAllocated())
              {
                  for ( String value: personMap.values()) {
                        if ( value != sendername) {

                        }
                    }

              }
              if( p2.getFamilyId() != Integer.parseInt(familyId))
              {

              // add values in map
              }
              }

              break;
            //  Person newPerson = personLists.get(j);

          }

    for (Iterator it = personMap.entrySet().iterator(); it.hasNext();) 
    {
          Map.Entry entry = (Map.Entry) it.next();
          Object key = entry.getKey();
          Object value = entry.getValue();

          System.out.println("Gifts "+key+"=&gt;"+value);
        }
    return pesonList;
}
</code></pre>

<p>Thanks</p>