java - Removing Specific Entries from ArrayList -


i have array list following sample entries (name & disease):

1.  name: aimee cholmondeley. disease: german measles 2.  name: colin anissina.     disease: diphtheria 3.  name: colin anissina.     disease: malaria 4.  name: aimee cholmondeley. disease: typhoid fever 5.  name: isaias cheung.      disease: haemophilus influenza 6.  name: isaias cheung.      disease: scarlet fever 7.  name: sebastian cutting.  disease: gingivitis 8.  name: juan weiss.         disease: acquired immunodeficiency sydrome (aids) 9.  name: kaelyn nauman.      disease: amebiasis 10. name: kaelyn nauman.      disease: human pulmonary syndrome (hps) 11. name: lyndsey stapleton.  disease: chlamydia 12. name: lyndsey stapleton.  disease: chlamydia 
  • same name, different disease -> remove both!
  • one instance -> keep
  • same name, same disease -> keep, 1 copy!

now, reason, .equals isn't working. can't if (arraylist.get(i).equals(arraylist.get(j)) remove. i'm comparing names , diseases individually, using compareto comparing diseases (which working).

here tried:

    (int = 0; < idarray.size(); i++){ //idarray contains elements         int countfound = 0;         idenperson curr1 = idarray.get(i);         (int j = + 1; j < idarray.size(); j++) {                idenperson curr2 = idarray.get(j);                if (curr1.name.tostring().equals(curr2.name.tostring())) { //if name same                    if  ((curr1.dis.tostring().compareto(curr2.dis.tostring())) == 0) { // , disease same                        system.out.println(curr1.name.tostring()); // print name                        break;                    }                }                else {                    // if name not same, , repeated once ... how this?                }         }     } 
public static class idenperson {     string name;     string dis; } 

using above, can find double copy elements cannot separate single instance elements. please help! cannot use libraries external java.

here's above arraylist should when works:

1. name: sebastian cutting.  disease: gingivitis 2. name: juan weiss.         disease: acquired immunodeficiency sydrome (aids) 3. name: lyndsey stapleton.  disease: chlamydia 

this can done using map<string, set<string>>. please have @ approach.

basically, maintaining set<string> hold diseases. if name has same disease, set should have single value, , if name has multiple, set should have multiple entries.

i eliminating value set has multiple entries

public class arraylistdisease {      public static list<string> process(list<string> input) {          map<string, set<string>> map = new hashmap<string, set<string>>();          for(string s : input) {             string [] nameanddisease = s.split("\\.");             if(map.containskey(nameanddisease[0])) {                 set<string> diseases = map.get(nameanddisease[0]);                 diseases.add(nameanddisease[1]);             }             else {                 set<string> set = new hashset<string>();                 set.add(nameanddisease[1]);                 map.put(nameanddisease[0], set);             }         }          list<string> res = new arraylist<string>();         for(entry<string, set<string>> e : map.entryset()) {             string key = e.getkey();             set<string> values = e.getvalue();             if(values.size() == 1) {                 res.add(key+"."+values.iterator().next());             }         }         return res;     }       public static void main(string [] args) {         list<string> input = new arraylist<string>();         input.add("name: aimee cholmondeley. disease: german measles");         input.add("name: colin anissina.     disease: diphtheria");         input.add("name: colin anissina.     disease: malaria");         input.add("name: aimee cholmondeley. disease: typhoid fever");         input.add("name: isaias cheung.      disease: haemophilus influenza");         input.add("name: isaias cheung.      disease: scarlet fever");         input.add("name: sebastian cutting.  disease: gingivitis");         input.add("name: juan weiss.         disease: acquired immunodeficiency sydrome (aids)");         input.add("name: kaelyn nauman.      disease: amebiasis");         input.add("name: kaelyn nauman.      disease: human pulmonary syndrome (hps)");         input.add("name: lyndsey stapleton.  disease: chlamydia");         input.add("name: lyndsey stapleton.  disease: chlamydia");          system.out.println(process(input));     }  } 

and here's output

[name: lyndsey stapleton.  disease: chlamydia, name: juan weiss.         disease: acquired immunodeficiency sydrome (aids), name: sebastian cutting.  disease: gingivitis] 

Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -