removing element from a list

Hello i would like to know how to remove an element for exemple a personne from a list, if his name is on the list. The name of the person is furnish dynamically for exemple through a jsp page. I would like to know how to compare the name furnish with the list of person and if the name provided via the jsp is corresponding to someone in the list, i remove this person from the list.

Thanks for help.

Message was edited by:

JusteUneQuestion

[467 byte] By [JusteUneQuestion] at [2007-11-26 12:15:50]
# 1

E.g. ...

List lp = ...;

String nameGiven = ...;

for (Iterator it = lp.iterator();it.hasNext();) {

if (((Person) it.next()).getName().equals(nameGiven)) {

it.remove();

break;

}

}

Though I guess I'd understand the problem better if it was described in French ...

quitte at 2007-7-7 14:51:05 > top of Java-index,Archived Forums,Socket Programming...
# 2
If you need to use the person as a key, use a Map<String, Person> instead of List<Person> and just call map.remove("nameOfPerson");
Peetzore at 2007-7-7 14:51:05 > top of Java-index,Archived Forums,Socket Programming...
# 3
Hello sorry i made a mistake, so i write an example :I have two list one list containing : list1 => Francois and John list2 => John and Pierrei would like to remove from list2 the person that are not in list1.
JusteUneQuestion at 2007-7-7 14:51:05 > top of Java-index,Archived Forums,Socket Programming...
# 4
1) Iterate over list2 and get name.2) For each name, call list1.contains(<name>)3) If it returns true, then call list2.remove(<name>)
sdantam at 2007-7-7 14:51:05 > top of Java-index,Archived Forums,Socket Programming...
# 5
Use retainAll() method
SidGate at 2007-7-7 14:51:05 > top of Java-index,Archived Forums,Socket Programming...
# 6

> 1) Iterate over list2 and get name.

> 2) For each name, call list1.contains(<name>)

> 3) If it returns true, then call list2.remove(<name>)

Yes that is right, i used it as first, but it didnt work cause i didnt redefine equals and hashCode() for my object so contains() never return anything correct. Now i redefine those method and it works fine this way.

JusteUneQuestion at 2007-7-7 14:51:05 > top of Java-index,Archived Forums,Socket Programming...