hi want remove values 1 array list array list both using model class of students , not in sorted order. example:
public class test { public static void main(string[] args) {
model obj1 = new model(); model obj2 = new model(); model obj3 = new model(); model obj6 = new model(); model obj5 = new model(); model obj4 = new model(); obj1.setcity("pune"); obj1.setname("aakshi"); obj1.setsalary("22"); obj1.setstate("mh"); obj2.setcity("pune"); obj2.setname("aakshi"); obj2.setsalary("23"); obj2.setstate("mh"); obj3.setcity("pune"); obj3.setname("aakshi"); obj3.setsalary("24"); obj3.setstate("mh"); obj4.setcity("pune"); obj4.setname("aakshi"); obj4.setsalary("22"); obj4.setstate("mh"); obj5.setcity("pune"); obj5.setname("aakshi"); obj5.setsalary("23"); obj5.setstate("mh"); obj6.setcity("pune"); obj6.setname("aakshi"); obj6.setsalary("24"); obj6.setstate("mh"); arraylist<model> list1 =new arraylist<model>(); arraylist<model> list2 = new arraylist<model>(); list1.add(obj1); list1.add(obj2); list1.add(obj3); list2.add(obj4); list2.add(obj5); list2.add(obj6); (model tablerow: list1) { system.out.println("record deleted "+tablerow); list2.remove(tablerow); system.out.println("records in list"+list2); } }
}
here not removing element list 1 list 2, can provide me solution.
sorry previous answer. did not understand question. think understand now. want take elements in list1
, remove equivalencies in list2.
first , foremost, list1
should list1
, class model
should model
in order follow proper java naming conventions.
now, answer, read documentation on remove() method:
removes first occurrence of specified element list, if present. if list not contain element, unchanged. more formally, removes element lowest index such (o==null ? get(i)==null : o.equals(get(i))) (if such element exists). returns true if list contained specified element (or equivalently, if list changed result of call).
this means that, instance, object4
removed list2
if object4.equals(object1)
(at least in case of application). in order ensure works, you'll have override .equals()
method in model
class (which should named model
- again, proper naming conventions):
public class model{ private string city, name, salary, state; //...setters , getters @override public int hashcode(){ int result = (city != null ? city.hashcode() : 0); result = 31 * result + (name != null ? name.hashcode() : 0); result = 31 * result + (salary != null ? salary.hashcode() : 0); result = 31 * result + (state != null ? state.hashcode() : 0); return result; } @override public boolean equals(object o){ if(!(o instanceof model)) return false; model omodel = (model) o; if(omodel.getcity().equals(city) && omodel.getname().equals(name) && omodel.getsalary().equals(salary) && omodel.getstate().equals(state)) return true; return false; } }
this override method ensure .remove()
, .removeall()
remove object o or collection of objects specified list such said list contains(o)
(or each object in collection removed). had it:
for(model m : list1){ list2.remove(m); }
or, more elegant approach:
list2.removeall(list1);
to reiterate, remove(object o)
, removeall(collection<?> c)
remove object or objects list long said list .contains(o)
(or, when using .removeall
, .contains((c.get(i)) in common iteration.
then, important know .contains(object o)
returns true if , if specified list has object .equals(o)
returns true, shown in documentation contains()
method:
returns true if list contains specified element. more formally, returns true if , if list contains @ least 1 element e such (o==null ? e==null : o.equals(e))
in order case, must define (override) .equals
in model
class.
edit:
as vladimir vagaytsev reminded me, crucial override hashcode
whenever overriding equals
ensure hashcode
derivative of object's relevant values hash collections. included said method in above source code.
Comments
Post a Comment