1

I create a map in Java such as private Map<Integer, List<entity>> m, how can I remove an item stay in List?

I run loop in Map to m.get(key).size() and remove when the condition is true:
if (m.get(key).get(i).getID() = xxx); removed clause m.get(key).remove(i).

Alex Kulinkovich
  • 4,408
  • 15
  • 46
  • 50
Phúc
  • 421
  • 1
  • 5
  • 11
  • I run loop in Map to m.get(key).size() and remove when the condition is true: if (m.get(key).get(i).getID() = xxx); removed clause m.get(key).remove(i). What's wrong here? – Phúc Aug 04 '12 at 09:32
  • @Phúc It seems feasible to me. Does it throw exception or what? Also check [this](http://stackoverflow.com/questions/223918/java-efficient-equivalent-to-removing-while-iterating-a-collection) question for safe removing from list. – Mikita Belahlazau Aug 04 '12 at 09:44
  • I have two table in database with 1-n relationship, table have relationship 1 i fill into combobox. And it do filter data of datatable which was loaded from the List, the table have relationship n. I did action but the datatable did not update although i used update="@this" or update "@form", and i do same for process attribute in . – Phúc Aug 04 '12 at 11:22
  • The item in List was removed successfully but it dose not refresh datatable. Pls someone help me! – Phúc Aug 04 '12 at 12:01

1 Answers1

0

I'd go over the map entries and use the removeIf method to remove the relevant elements from each list:

m.values().forEeach(v -> v.removeIf(i -> i.getId() == 123));
// Just an example, of course ------------------------^
Mureinik
  • 297,002
  • 52
  • 306
  • 350