Pairing among the members in same list in java -


in problem statement, have 'n' number of families 'n' number of family members.

eg:

john jane (family 1) tiya (family 2) erika (family 3) 

i have assign members in such way person should not pair family member. , output should be:

john => tiya jane => erika tiya => jane erika => john 

i have created object person(name ,familyid, isallocated). created list , added personname_id in this.

i thinking use map association. john_1 key , tiya_2 value.

i failing associate pairs through map. how can shuffle members list.

also, nice if suggest me better solution.

code:

getting person:

public static list getperson() {     scanner keyboard = new scanner(system.in);     string line = null;     int count = 0;      list <person> people = new arraylist<>();      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; } 

mapping:

public static list mapgifts(list pesonlist)  {     map<string , string> personmap = new hashmap<string , string>();      iterator<person> itr = pesonlist.iterator();     iterator<person> itr2 = pesonlist.iterator();     list<string> sender =  new arraylist<>();      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 , id sender list               //check id match 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())               {                   ( string value: personmap.values()) {                         if ( value != sendername) {                          }                     }                }               if( p2.getfamilyid() != integer.parseint(familyid))               {                // add values in map               }               }                break;             //  person newperson = personlists.get(j);            }      (iterator = 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+"=>"+value);         }     return pesonlist; } 

thanks

from i've read, care match people. how match doesn't matter. said, i'll assume have list of familyid's, , list of names of everyone, , can sort list of people according family ids.

let's call them:

list<familyid> families; , linkedlist<person> people;, respectively. (you can make familyid enumerated class)

we need 2 hashmaps. 1 generate list (essentially adjacency list) of family members given familyid:

hashmap<familyid, list<person>> familymembers; ,

and 1 generate list of sender(key) , receiver(value) pairs:

hashmap<person, person> pairs;

a useful function may that, when given person , family id, can find next available person can receive them.

string generatereceiver(person newsender, familyid familyid);

implementation of method should pretty straightforward. can iterate through list of people , check see if current person not family member. if condition passes, remove them "people" list don't try iterate through them again. if you're using linked list this, removal o(1) since you'll have reference. worst case on traversals list n + n - 1 + ... + 2 times o(n^2) time efficiency (i.e. have 1 large family , many small ones). work around ditch linkedlist, use array-based list, , keep separate array of index values corresponding each "currently available receiver of specified family". you'd initialize these values added people each family people list (i.e. start of family 1 index "0"; if family 1 has 2 people, start of family 2 index "2"). make function o(1) time if incremented current available receiver index everytime added sender-receiver pair. (message me if want more details on this!)

last not least, loop doing people.

 (list<person> family : familymembers)   (person person : family)    {      // next available receiver not family member      // add family member , receiver "pairs" hash    } 

note above loop pseudocode. if you're wondering if generate conflicting receiver/senders method, won't. list of people acting list of receivers. whichever way implement people list, generatereceiver(...)eliminates chance algorithm see faulty-receiver. per efficiency, if array based implementation you're @ o(n) time generating pair values, n total number of people. program o(n) space well.

of course, based on assumption have enough people match sender-receiver pairs. you'd need add bells , whistles check special cases.

hope helps! luck!


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -