concurrentmodification

java.util.ConcurrentModificationException in For loop

删除回忆录丶 提交于 2019-12-02 03:45:53
问题 I am trying to program an IM software, I want to let user leave the conversation and tell his partner that he has left... I prefer to use for loop instead Iterator, seek all the users and get the user who ask to leave and remove him... like that: for(Clientuser Cu: EIQserver.OnlineusersList) if(Cu.ID.equals(thsisUser.ID)) // find the user who ask to leave { Omsg.setBody("@@!&$$$$@@@####$$$$"); //code means : clien! ur parter leaves... sendMessage(Omsg); // sed message to thje partner with

How to correctly animate images in a data structure and not get ConcurrentModificationException

狂风中的少年 提交于 2019-12-02 03:31:40
For those who hate reading long questions, take the complete code below, run it, hit SPACE a few times, and you'll get a ConcurrentModificationException . Simple question: How do you fix it? The problem is trying to remove a Fireball from the list when it exits the screen. The Timer code is where the problem lies. If you want more info, keep reading. In this question where the OP asks how to to shoot fireball images, I answered with this answer indicating that a data structure should be used to hold the fireballs. IMO it was a half @$$ answer. The reason I think this is because the code I gave

Is there an accepted best practice in Java for deleting a list element while iterating over the list?

偶尔善良 提交于 2019-12-02 01:20:07
问题 I'm finding conflicting advice over the best way to avoid a ConcurrentModificationException while doing this: List<Apple> Apples = appleCart.getApples(); for (Apple apple : Apples) { delete(apple); } I'm leaning towards using an Iterator in place of a List and calling its remove method. Does that make the most sense here? 回答1: Yes, use an Iterator. Then you could use its remove method. for (Iterator<Apple> appleIterator = Apples.iterator(); appleIterator.hasNext();) { Apple apple =

Is there an accepted best practice in Java for deleting a list element while iterating over the list?

限于喜欢 提交于 2019-12-01 22:15:54
I'm finding conflicting advice over the best way to avoid a ConcurrentModificationException while doing this: List<Apple> Apples = appleCart.getApples(); for (Apple apple : Apples) { delete(apple); } I'm leaning towards using an Iterator in place of a List and calling its remove method. Does that make the most sense here? Yes, use an Iterator. Then you could use its remove method. for (Iterator<Apple> appleIterator = Apples.iterator(); appleIterator.hasNext();) { Apple apple = appleIterator.next(); if (apple.isTart()) { appleIterator.remove(); } } } If you're getting a

How can I iterate over an object while modifying it in Java? [duplicate]

旧城冷巷雨未停 提交于 2019-12-01 18:13:15
问题 This question already has answers here : Closed 9 years ago . Possible Duplicates: Java: Efficient Equivalent to Removing while Iterating a Collection Removing items from a collection in java while iterating over it I'm trying to loop through HashMap : Map<String, Integer> group0 = new HashMap<String, Integer>(); ... and extract every element in group0 . This is my approach: // iterate through all Members in group 0 that have not been assigned yet for (Map.Entry<String, Integer> entry :

How can I iterate over an object while modifying it in Java? [duplicate]

瘦欲@ 提交于 2019-12-01 18:06:01
Possible Duplicates: Java: Efficient Equivalent to Removing while Iterating a Collection Removing items from a collection in java while iterating over it I'm trying to loop through HashMap : Map<String, Integer> group0 = new HashMap<String, Integer>(); ... and extract every element in group0 . This is my approach: // iterate through all Members in group 0 that have not been assigned yet for (Map.Entry<String, Integer> entry : group0.entrySet()) { // determine where to assign 'entry' iEntryGroup = hasBeenAccusedByGroup(entry.getKey()); if (iEntryGroup == 1) { assign(entry.getKey(), entry

Java - Exception in thread “main” java.util.ConcurrentModificationException

寵の児 提交于 2019-12-01 13:52:27
Is there any way I can modify the HashMap values of a particular key while iterating over it? A sample program is given below: public static void main(String[] args) { HashMap<Integer,ArrayList<String>> hm = new HashMap<Integer, ArrayList<String>>(); ArrayList<String> ar = new ArrayList<String>(); for(int i=0;i<50;i++){ ar.add(Integer.toString(i)); } hm.put(1, ar); for(String s:hm.get(1)){ hm.get(1).add("hello"); } } Error Thrown: Exception in thread "main" java.util.ConcurrentModificationException at java.util.ArrayList$Itr.checkForComodification(Unknown Source) at java.util.ArrayList$Itr

concurrentModificationException

天涯浪子 提交于 2019-12-01 13:19:36
With the snippet below I am, attempting to process a spreadsheet, with the twist of needing to exclude ad hoc columns. I know the crude way I am doing it, put the exceptions in an ArrayList and process the list on each and ever increment over the current row columns is perverse, but you know just get it done. However I am getting the titled error, which I believe should never happen. I am just looping through the ArrayList and comparing, not modifying anything. Where is the error? Is there a better way to handle the exceptions list? ArrayList noProcess = new ArrayList(); Iterator itr00 =

Consuming from Kafka failed Iterator is in failed state

假如想象 提交于 2019-12-01 09:35:45
I am getting exception while consuming the messages from kafka. org.springframework.messaging.MessagingException: Consuming from Kafka failed; nested exception is java.util.concurrent.ExecutionException: java.lang.IllegalStateException: Iterator is in failed state I have one consumer in the application context with one outbound adapter. Consumer configuration in application context <int-kafka:consumer-context id="consumerContext" consumer-timeout="4000" zookeeper-connect="zookeeperConnect"> <int-kafka:consumer-configurations> <int-kafka:consumer-configuration group-id="GR1" value-decoder=

ConcurrentModificationException when using iterator and iterator.remove()

烂漫一生 提交于 2019-12-01 09:19:22
private int checkLevel(String bigWord, Collection<String> dict, MinMax minMax) { /*value initialised to losing*/ int value = 0; if (minMax == MinMax.MIN) value = 1; else value = -1; boolean go = true; Iterator<String> iter = dict.iterator(); while(iter.hasNext()) { String str = iter.next(); Collection<Integer> inds = naiveStringSearch(bigWord, str); if(inds.isEmpty()) { iter.remove(); } for (Integer i : inds) { MinMax passin = minMax.MIN; if (minMax == MinMax.MIN) passin = minMax.MAX; int value2 = checkLevel(removeWord(bigWord, str, i), dict, passin); if (value2 == -1 && minMax == minMax.MIN)