How to remove just one element from arraylist while iterating over it

Lcifzhidvs

New Member
I wanted to remove single element which matches orderID = 542 .But the thing is this program is removing two elements from that list.In the real program i m iterating through an arraylist and i call a function to check whether that element is to be removed from the list and that function is suppose to remove the element from the list\[code\]package testMap;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Map.Entry;import java.util.Set;public class TestHashMap { static ArrayList<LimitOrder> a = new ArrayList<>(); public static void main(String args[]) { create(); } public static void create() { LimitOrder l1 = new LimitOrder(1, 100, "145"); LimitOrder l2 = new LimitOrder(1, 100, "542"); LimitOrder l3 = new LimitOrder(1, 100, "355"); a.add(0, l1); a.add(1, l2); a.add(2, l3); Iterator<LimitOrder> i = a.iterator(); while (i.hasNext()) { boolean toremove = false; LimitOrder l = i.next(); toremove=remove(); if (toremove == true) { System.out.println("Removed "+l.orderID); i.remove(); } } } public static boolean remove() { boolean flag = false; Iterator<LimitOrder> i = a.iterator(); while (i.hasNext()) { LimitOrder l = i.next(); if (l.orderID.equals("542")) { flag = true; } } return flag; }}\[/code\]Please help me
 
Back
Top