Comparing data numbers, Iterator.remove() throwing NullPointerException

I am trying to make an Android app that can compare integers. However, when I am trying to compare integers and I need to delete some entries because they are redundant or they do not fit in, I cannot use Iterator.remove() without encountering a NullPointerException. Here is the specific output: E/AndroidRuntime: FATAL EXCEPTION: main Process: … Continue reading “Comparing data numbers, Iterator.remove() throwing NullPointerException”

How to use iterator to retrieve objects from two classes in Java

I am new to Java and learning about collections. I have a problem in retrieving data using an Iterator from two class objects, I am only able to get output for any single class but not two classes. This is my code: package ncl; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.ArrayList; import java.util.Iterator; public … Continue reading “How to use iterator to retrieve objects from two classes in Java”

Can I use an Iterator inside another Iterator?

I have a HashTable (I implemented the structure) that implements linked lists to treat collisions (I also implemented the lists). Both of them have Iterators. I want to use a Table as a structure for a class and want to iterate over each item in it. Can I implement an Iterator that iterates first over … Continue reading “Can I use an Iterator inside another Iterator?”

ArrayList iterator throwing ConcurrentModificationException

I have an ArrayList with two accessor methods and a notifier. My list: private final List<WeakReference<LockListener>> listeners = new ArrayList<>(); All subscribe operations use this: public void subscribe(@NonNull LockListener listener) { for (Iterator<WeakReference<LockListener>> it = listeners.iterator(); it.hasNext(); ) { // has this one already subscribed? if (listener.equals( { return; } } listeners.add(new WeakReference<>(listener)); } All … Continue reading “ArrayList iterator throwing ConcurrentModificationException”

JDK java.util.concurrent.ConcurrentSkipListSet.equals(Object o) implementation efficiency

The equalsimplementatin of java.util.concurrent.ConcurrentSkipListSet in JDK is as following public boolean equals(Object o) { // Override AbstractSet version to avoid calling size() if (o == this) return true; if (!(o instanceof Set)) return false; Collection<?> c = (Collection<?>) o; try { return containsAll(c) && c.containsAll(this); } catch (ClassCastException unused) { return false; } catch (NullPointerException … Continue reading “JDK java.util.concurrent.ConcurrentSkipListSet.equals(Object o) implementation efficiency”

Creating new objects using a iterator in a while loop

Here is my code first: public static List<AnagramFamily> assignFamilies(List<Word> theOriginalList) { //Stores the list of anagram Families. List<AnagramFamily> anagramList = new LinkedList<AnagramFamily>(); //TempList to store the word objects. List<Word> tempList = new LinkedList<Word>(); ListIterator<Word> itr = theOriginalList.listIterator(); Word firstWord = theOriginalList.get(0); int cnt = 0; while(itr.hasNext()) { Word secondWord =; if (firstWord.getMyCanonical().equals(secondWord.getMyCanonical())) { tempList.add(secondWord); … Continue reading “Creating new objects using a iterator in a while loop”

Java Iterator infinite loop iterates only the 1st item in hashmap

I am executing a database query, and as result I get a HashMap. I want to iterate through all the results, but I infinitely add the first item from the result to the arraylist. QueryResult result=engine.query(query,params); while(result.iterator().hasNext()) { HashMap res= (HashMap)result.iterator().next(); Node node=(Node)res.get(“n”); results.add(new BusyProfile(node)); } How to iterate through each object and why do … Continue reading “Java Iterator infinite loop iterates only the 1st item in hashmap”

Overriding same method twice from same class

I was understanding how iterator() method works with ArrayList class . In ArrayList class I found iterator() method overridden twice from same class AbstractList . public Iterator<E> iterator() { return new Itr(); // Itr is an inner private class of // ArrayList which // implements Iterator interface . } public Iterator<E> iterator() { return listIterator(); … Continue reading “Overriding same method twice from same class”

iterating over sorted treemap: java.util.ConcurrentModificationException

I have basic knowledge in Java and working currently on a Java based code. EDIT: I didn’t write the code I am iterating over a sorted treemap of Event Objects and getting this exception when I am trying to get the next element: java.util.ConcurrentModificationException at java.util.TreeMap$PrivateEntryIterator.nextEntry(Unknown Source) at java.util.TreeMap$ Source) I think this should be … Continue reading “iterating over sorted treemap: java.util.ConcurrentModificationException”

Hadoop Iterator skip method call at first iteration

I have a Map Reduce program and in the Reducer class my method is not called in the first iteration. What I want to achieve is to generate some new rows between every 2 consecutive values from the iterator. (pairs like: (1,2),(2,3),(3,4)…). What am I missing? And I also tested that I have the pairs … Continue reading “Hadoop Iterator skip method call at first iteration”