Skip to content
Advertisement

Tag: iterator

java.util.ConcurrentModificationException while mutating an object

I am iterating over a List of CustomObject and while doing this iteration, I am mutating this object by adding a tag to tags list of this custom object. I am not adding or removing any CustomObject to or from customObjects (List). I am still getting java.util.ConcurrentModifcationException. Here is the stack trace: Does this mean we can get ConcurrentModifcationException even

Backward Traverse using ListIteartor in java

I am new to java and learning from a very basic level. I am trying to run this below code which is not showing any result in the console. It’s only working when I add forward traverse code before it. Can anyone please help me with that? Thanks Answer When you create a list iterator with a call to myList.listIterator(),

Latency in Iterator or foreach

i have one ArrayList that has a 1000 Insert SQL statement. but rather in execution time the latency of Iterator or (enhanced for loop) for this ArrayList take 1 minute . and my JFrame is not responding in this period. what can i do? Thanks Answer foreach internally uses iterator only, in order to handle bulk updates and inserts from

Iterator vs for

I was asked in an interview what is the advantage of using iterator over for loop or what is the advantage of using for loop over iterator? Can any body please answer this? Answer First of all, there are 2 kinds of for loops, which behave very differently. One uses indices: This kind of loop isn’t always possible. For example,

What are fail-safe & fail-fast Iterators in Java

There are two types of iterators in Java: fail-safe and fail-fast. What does this mean, and is the difference between them? Answer What is the difference between them … “Fail-safe” (in engineering) means that something fails in a way that causes no or minimal damage. Strictly speaking, there is no such thing in Java as a fail-safe iterator. If an

hasPrevious() Method not working

I don’t understand why my hasPrevious iterator is not functioning, it looks like this: I have another function using hasNext() and it works, but this one will not. I am not sure if this method is no longer in existence or not, but when I look into it, Java websites speak of it. I don’t understand why it is not

Why is this ArrayList throwing a ConcurrentModificationException when I try to remove an element?

I’m trying to remove a particular element from Arraylist, it throws an ConcurrentModificationException any comments, what am I doing wrong? Answer Only remove an element from the array while iterating by using Iterator.remove(). The line for(String st: ar) { is a bit misleading. You’re actually creating an iterator behind the scenes which is being used for this iteration. If you

Does using an Iterator over a TreeMap in Java interfere with the order of keys?

I want to know that if using an Iterator over a TreeMap in Java interfere with the order of keys? Answer From http://docs.oracle.com/javase/6/docs/api/java/util/TreeMap.html#keySet() : Returns a Set view of the keys contained in this map. The set’s iterator returns the keys in ascending order. If for some reason you need the keys in descending order, you can use descendingKeySet() If

Find element position in a Java TreeMap

I am working with a TreeMap of Strings TreeMap<String, String>, and using it to implement a Dictionay of words. I then have a collection of files, and would like to create a representation of each file in the vector space (space of words) defined by the dictionary. Each file should have a vector representing it with following properties: vector should

Advertisement