Last active
July 6, 2017 16:18
-
-
Save sumitramteke/5ac2cfd45cb2246b63ae42110aef6997 to your computer and use it in GitHub Desktop.
This will remove Objects from Collection using List of single Attribute (here `String`) by overriding `remove` method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package snippets.model; | |
public class Person { | |
private String id; | |
private String name; | |
private int age; | |
public Person(String id, String name, int age) { | |
this.id = id; | |
this.name = name; | |
this.age = age; | |
} | |
public String getName() { | |
return this.name; | |
} | |
@Override | |
public String toString() { | |
return "Person [id=" + id + ", name=" + name + ", age=" + age + "]\n"; | |
} | |
@Override | |
public boolean equals(Object obj) { | |
if(obj instanceof String) { | |
return this.name.equals((String) obj); | |
} else if(obj instanceof Person) { | |
Person person = (Person) obj; | |
return this.id.equals(person.id) && this.name.equals(person.name); | |
} else { | |
return false; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package snippets.app; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
import snippets.model.Person; | |
public class RemoveListObjectsByListOfString { | |
public static void main(String[] args) { | |
List<String> names = Arrays.asList("jhon","sami"); | |
List<Person> persons = new ArrayList<Person>() { | |
/*@Override | |
public boolean remove(Object o) { | |
if(o instanceof List) { | |
boolean state = true; | |
List<String> names = (List<String>) o; | |
for (int index = 0, size = this.size(); index < size; index++) { | |
try { | |
Person p = this.get(index); | |
if(names.contains(p.getName())) { | |
state = super.remove(p); | |
} | |
} catch (IndexOutOfBoundsException ex) { | |
index = -1; | |
size = this.size(); | |
continue; | |
} | |
} | |
return state; | |
} | |
return super.remove(o); | |
}*/ | |
}; | |
persons.add(new Person("1", "jhon", 23)); | |
persons.add(new Person("2", "sami", 22)); | |
persons.add(new Person("3", "daan", 22)); | |
persons.add(new Person("4", "waly", 22)); | |
persons.add(new Person("5", "bond", 22)); | |
persons.add(new Person("6", "same", 22)); | |
System.out.println("before :::::: \n" + persons); | |
System.out.println("::::::::::::::::::::::::::"); | |
persons.removeAll(names); | |
// persons.remove(names); uncomment remove overriding | |
System.out.println("after :::::: \n" + persons); | |
System.out.println("::::::::::::::::::::::::::"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment