Core Java - Interview Questions and Answers for 'Iterators' | Search Interview Question - javasearch.buggybread.com
Javasearch.buggybread.com

Search Interview Questions


 More than 3000 questions in repository.
 There are more than 900 unanswered questions.
Click here and help us by providing the answer.
 Have a video suggestion.
Click Correct / Improve and please let us know.
Label / Company      Label / Company / Text

   



Core Java - Interview Questions and Answers for 'Iterators' - 11 question(s) found - Order By Newest

Frequently asked.
  Q1. What is an Iterator?Core Java
Ans. Iterator is an interface that provides methods to iterate over any Collection.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   iterator     Asked in 11 Companies      basic        frequent


 Q2. What are few iterator classes provided by Java ?
Ans. http://www.buggybread.com/2015/01/java-iterator-classes-and-interfaces.html

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   iterator


 Q3. What are fail-fast Iterators ?Core Java
Ans. Fail-fast iterators detect illegal concurrent modification during iteration and fail quickly and cleanly rather than risking arbitrary, non deterministic behavior at an undetermined time in future. Example could be of an Iterator failing if it smells ConcurrentModificationException.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     fail-fast Iterators  Iterators     Asked in 5 Companies      intermediate


 Q4. Is Iterator a class ?
Ans. No, iterator is an interface that is used to parse through the elements of a Collection

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     collections  iterator


 Q5. How Spliterator in Java 8 different than iterator ?Core Java
Ans. Though there are many differences the way internally they both iterates the collections and streams respectively, but the main difference in performance that is achieved by spliterator as it can iterate Streams in Parallel whereas iterator only iterates collections sequentially.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java8  java 8  spliterator  java 8 streams  streams


 Q6. Which is better in terms of performance - Iterator or Spliterator ?Core Java
Ans. Spliterator has better performance potential than iterators but only if the potential is used. Spliterator can iterate streams in parallel as well as in sequence whereas iterator can only iterate in sequence.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     iterator  collections  streams  parallel streams  Spliterator


 Q7. What are the cursors available in Java ?Core Java
Ans. Enumeration
Iterator
List iterator

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     cursors  iterator


 Q8. What is the difference between Enumeration and Iterator ?Core Java
Ans. Enumeration can iterate only legacy collections like Vector , HashTable and Stack whereas Iterator can iterate both legacy and non legacy collections.

Enumeration is less safer than Iterator

Enumeration is fail safe whereas Iterator is fail fast

Iterator allows for removal of element while traversal whereas Enumeration doesn't have remove method.

Enumerations were introduced in Java 1 whereas Iterators were introduced with Java 2

Enumerations have methods like hasMoreElements and nextElement whereas Iterators have methods like hasNext, next and remove

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     enumeration vs iterator  collections      Basic        frequent


 Q9. How can i split a linked list in two parts in java 8?Core Java
Ans. This can be done using a Spliterator.

LinkedList list = Arrays.asList("names","numbs","birds","animals");
Spliterator split1 = list.Spliterator();
Spliterator split2 = split1.Spliterator();

Now, the LinkedList is split into split1 and split2.
use split2 first then split1 to check the output.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java 8  parallel stream  spliterator     Asked in 1 Companies


 Q10. Can we modify a list while iterating it ?Core Java
Ans. No,we cannot.I t will give concurrentModificationExceptin error. It can be resolved by using ConcurrentClasses like ConcurrentHashMap,CopyOnWriteArrayList,BlockingQueue etc which are fail-safe and wont give exception.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     collections  list  iterator     Asked in 1 Companies      basic


 Q11. How to write an iterator for an iterator of iterators?Core Java
Ans. CustomArrayList myarrayList = new CustomArrayList();
      myarrayList.add("Value 1");
      myarrayList.add("Value 2");
      myarrayList.add("Value 3");
      
      for (String string : myarrayList) {
         System.out.println(string);
      }



package sample.utils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class CustomArrayList implements Iterable {

   private ArrayList mylist = new ArrayList();

   public void add(T t) {
      this.mylist.add(t);
   }

   @Override
   public Iterator iterator() {
      return new CustomIterator(mylist);
   }

   class CustomIterator implements Iterator {

      private int indexPosition = 0;
      private List internalList;

      public CustomIterator(List internalList) {
         this.internalList = internalList;
      }

      @Override
      public boolean hasNext() {
         if (internalList.size() >= indexPosition 1) {
            return true;
         }
         return false;
      }

      @Override
      public E next() {
         E val = internalList.get(indexPosition);
         indexPosition ;
         return val;
      }

   }

}

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     iterator     Asked in 1 Companies



Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked: