java - Removing elements from list based on condition -


i have code selectively adds data map following,

public static void main(string[] args) {          list<person> people = arrays.aslist(                 new person("johnny", "depp", 18),                 new person("jennifer", null, 30),                 new person("angelina", null, 23),                 new person("angelina", "garlic", 38),                 new person("angelina", "jolie", 40),         );          map<string, person> map = new hashmap<>();          (person person:              people) {             person temp = map.get(person.getfirstname());             if(temp == null || temp.getlastname() == null)                 map.put(person.getfirstname(), person);         }          (map.entry mapper:              map.entryset()) {             person temp = (person) mapper.getvalue();             system.out.println(temp.getfirstname());             system.out.println(temp.getlastname());             system.out.println();         }      } 

person.java contains following:

public class person {      string firstname;     string lastname;     int age;      public person(string firstname, string lastname, int age) {         this.firstname = firstname;         this.lastname = lastname;         this.age = age;     }      public string getfirstname() {         return firstname;     }      public void setfirstname(string firstname) {         this.firstname = firstname;     }      public string getlastname() {         return lastname;     }      public void setlastname(string lastname) {         this.lastname = lastname;     }      public int getage() {         return age;     }      public void setage(int age) {         this.age = age;     } } 

now, need check following conditions, 1) map has multiple records same firstname; , if record has null last name, should add record map. 2) if map has 1 records person's firstname; , if record has null last name, qualifies added map.

so output current solution is,

johnny depp

jennifer null

angelina garlic

where required output should be,

johnny depp

jennifer null

angelina garlic

angelina jolie

note : making use of java 7 (no, java 8 not option)

it's because, in map, key has unique when check second time angelina , matches because last name null , put map override original angelina. maps have have unique keys,

public interface map object maps keys values. map cannot contain duplicate keys; each key can map @ 1 value.

i found 2 different ways of solving it

1. use supplementary list maintain objects override current keys

list<person> supplementarylist = new arraylist<person>();  map<string, person> map = new hashmap<string, person>();  (person person : people) {    person temp = map.get(person.getfirstname());    if (temp != null) {     supplementarylist.add(person);     if (temp.getlastname() == null)       map.remove(temp.getfirstname());    } else {     map.put(person.getfirstname(), person);   } }  (map.entry mapper : map.entryset()) {   person temp = (person) mapper.getvalue();   system.out.println(temp.getfirstname());   system.out.println(temp.getlastname());   system.out.println(); }  (person p : supplementarylist) {    system.out.println(p.getfirstname() + p.getlastname());  } 

2. map<string,list<person>>

map<string, list<person>> namesmap = new hashmap<string, list<person>>();  (person p : people) {    list<person> res = namesmap.get(p.getfirstname());    // if there not valid list of names key add new person   if (res == null) {     list<person> newlistofpeople = new arraylist<person>();     newlistofpeople.add(p);     namesmap.put(p.getfirstname(), newlistofpeople);   } else {      if (p.getlastname() != null) {       res.add(p);       namesmap.put(p.getfirstname(), res);       iterator<person> iter = res.iterator();       // remove null list if exists       while (iter.hasnext()) {         person person = iter.next();         if (person.getlastname() == null)           iter.remove();       }     }   } }  (map.entry<string, list<person>> kv : namesmap.entryset()) {   if (kv.getvalue() != null) {     (person p : kv.getvalue())       system.out.println(p.getfirstname() + " " + p.getlastname());    } } 

output

johnny depp jennifer null angelina garlic angelina jolie 

Comments