Java: best way to iterate through a collection (here's an ArrayList)

Today, I was happily coded when I received code that I have already used hundreds of times:

Iterate through a collection (here is an ArrayList)

for some reason, I really looked at the Eclipse autocomplete options, and I wondered:

In what cases is it better to use the following loops than others?

Classic array index loop:

for (int i = 0; i < collection.length; i++) { type array_element = collection.get(index); } 

HasNext iterator () / next ():

 for (Iterator iterator = collection.iterator(); iterator.hasNext();) { type type = (type) iterator.next(); } 

And my love, because it’s so easy to write it:

 for (iterable_type iterable_element : collection) { } 
+75
java collections for-loop
Mar 08 2018-11-11T00:
source share
6 answers

The first is useful when you need an element index. This is basically equivalent to the other two options for ArrayList s, but will be very slow if you use LinkedList .

The second is useful when you do not need the index of an element, but you may need to remove the elements when repeating. But this has the disadvantage of being too verbose IMO.

The third version is my preferred choice. It is short and works in all cases when you do not need indexes or a basic iterator (i.e. you only get access to elements, and do not delete them or change Collection any way - this is the most common case).

+86
Mar 08 2018-11-11T00:
source share

All of them have their own capabilities:

  • If you have iterability and you need to unconditionally proceed to all of them:

    for (iterable_type iterable_element: collection)

  • If you have iterability, but you need to conditionally go through:

    for (Iterator iterator = collection.iterator (); iterator.hasNext ();)

  • If the data structure does not implement iteration:

    for (int i = 0; i <collection.length; i ++)

+31
Mar 08 2018-11-11T00:
source share

In addition, the stream () collection is used with Java 8

 collection.forEach((temp) -> { System.out.println(temp); }); 

Additional information on Java thread and collections for wizards link

+5
Apr 28 '16 at 15:54
source share

None of them are β€œbetter” than the others. The third, for me, is more readable, but for those who do not use foreaches, this may seem strange (they may prefer the first). All 3 are pretty clear to anyone who understands Java, so choose what helps you better in the code.

The first one is the most basic, so it is the most universal template (works for arrays, all repeating ones that I can think of). This is the only difference I can think of. In more complex cases (for example, you need to have access to the current index or you need to filter the list), the first and second cases may make more sense, respectively. For a simple case (an iterable object, no special requirements), the third seems to be the cleanest.

+4
Mar 08 2018-11-11T00:
source share

The first option is better performance (for example, ArrayList implements the RandomAccess interface). According to the java document, the List implementation should implement the RandomAccess interface if for typical class instances this loop:

  for (int i=0, n=list.size(); i < n; i++) list.get(i); 

faster than this loop:

  for (Iterator i=list.iterator(); i.hasNext(); ) i.next(); 

Hope this helps. The first option will be slow for sequential access lists.

+2
Nov 20 '15 at 7:46
source share

Here is an example

 Query query = em.createQuery("from Student"); java.util.List list = query.getResultList(); for (int i = 0; i < list.size(); i++) { student = (Student) list.get(i); System.out.println(student.id + " " + student.age + " " + student.name + " " + student.prenom); } 
+1
Mar 23 '16 at 17:11
source share



All Articles