Collections in JAVA 3

Starting towards next part of collections in JAVA. Previous part links are given here: Collections in JAVA 1 and Collections in JAVA 2 look at these, Now come towards this post.


  • You can scan (iterate over) a collection using an iterator. The basic Iterator interface enables you to scan forward through any collection.
  • In the case of an iterator over a set, the order is non deterministic.
  • The order of an iterator over a list moves forward through the list elements. A List object also supports a ListIterator, which permits you to scan the list backwards and insert or modify list elements.
  • So, from above we understood that to display each element we use an Iterator interface.

Note: – The order of the set is deterministic if the set is an instance of some class that guarantees the order. For example, if the set is an instance of TreeSet, which implements SortedSet, the order of the set is deterministic.

  • The code given below demonstrates the use of an iterator,
  1. List list<Student> = new ArrayList<Student>();
  2. // add some elements
  3. Iterator<Student> elements = list.iterator();
  4. while (elements.hasNext())
  5. {
  6. System.out.println(;
  7. }

Following shows the generic Iterator interfaces for the Collections API,


  • hasNext() : boolean
  • next() : E
  • remove()


  • hasPrevious() : boolean
  • previous() : E
  • add(element : E)
  • set(element : E)
  • The remove method enables code to remove the current item in the iteration (the item returned by the most recent next or previous method).
  • If removal is not supported by the underlying collection, then an UnsupportedOperationException is thrown.
  • While using a ListIterator, it is common to move through the list in only one direction: either forward using next or backward using previous.
  • If you use previous immediately after next, then you receive the same element; likewise calling next after previous.
  • The set method changes the element of the collection currently referenced by the iterator’s cursor.
  • The add method inserts the new element into the collection immediately before the iterator’s cursor.
  • Therefore, if you call previous after an add, then it returns the newly added element. However, a call to next is not affected.
  • If setting or adding is not supported by the underlying collection, then an UnsupportedOperationException is thrown.


Collections in JAVA


The Enhanced for Loop:-

  • The code given below illustrates the use of an iterator in combination with a traditional for loop to iterate over a collection.


  1. public void deleteAll(Collection<NameList> c)
  2. {
  3. for(Iterator<NameList>) i = c.iterator(); i.hasNext())
  4. {
  5. NameList nl =;
  6. nl.deleteItem();
  7. }
  8. }


  • In above code, the method deleteAll uses variable I three times in the for loop. The enhanced for loop makes traversing through a collection simple, understandable and safe.
  • The enhanced for loop eliminates the usage of separate iterator methods and minimizes the number of occurrences of the iterator variable.
  • The code given below illustrates the method deleteAll with the enhanced for loop.


  1. public void deleteAll(Collection<NameList> c)
  2. {
  3. for(NameList nl : c)
  4. {
  5. nl.deleteItem();
  6. }
  7. }


  • The functionality of the enhanced for loop, makes nested for loops traversal simpler and easier to understand in comparison with the traditional for loop.
  • One reason for this is, using enhanced for loops reduces the ambiguity of the variables used in the nested loops.
  • The code given below contains an example of nested enhanced for loops.


  1. List<Subject> subjects = …;
  2. List<Teacher> teachers = …;
  3. List<Course> courseList = new ArrayList<Course>();
  4. for(Subject subj : subjects)
  5. {
  6. for(Teacher tchr : teachers)
  7. {
  8. courseList.add(new Course(subj, tchr));
  9. }
  10. }

For more reading about technology news in singapore or from all over the world in all aspect from seo to online marketing do view more about other pages.

Sourabh Bhunje

Sourabh Bhunje, B.E. IT from Pune University. Currently Working at Techliebe. Professional Skills: Programming - Software & Mobile, Web & Graphic Design, Localization, Content Writing, Sub-Titling etc.

Leave a Reply