The Collections API's in JAVA were introduced to provide a simple way to handle the collection of objects.
Collections works a bit like arrays where unlike arrays, collections prove very important when you expect their size to change dynamically.
The Iterable interface (java.lang.Iterable) is one of the root interfaces of the Java collection classes.
The Collection interface extends Iterable, so all subtypes of Collection also implement the Iterable interface.
Considering the three most frequently used Collection types ie. Array List, Hash Set and Hash Map, this post will help you iterate over these collections.
Iterating using iterators :
1) Iterate over Array List:
2) Iterate over Hash Set:
HashSet is backed by a HashMap. It makes no guarantees about the sequence of the elements when you iterate them.
LinkedHashSet differs from HashSet by guaranteeing that the order of the elements during iteration is the same as the order they were inserted into the LinkedHashSet.
Reinserting an element that is already in the LinkedHashSet does not change this order.
3) Iterate over Hash Map:
HashMap maps a key and a value. It does not guarantee any order of the elements stored internally in the map.
TreeMap also maps a key and a value and it guarantees the order in which keys or values are iterated - which is the sort order of the keys or values.
Iterating over a Hash Map is tricky as Hash map contains a key-value pair.
A map entry (key-value pair). The Map.entrySet method returns a collection-view of the map, whose elements are of this class.
Iterating using for-each loop and its advantages :
JAVA later introduced 'Enhanced for each loop iteration control', which extends the JAVA5 for-each loop to allow access to the loop index, whether this is the first or last iteration, and to remove the current item.
MAJOR ADVANTAGE :
The for-each loop is almost certainly the most new popular feature from Java 5. It works because it increases the abstraction level - instead of having to express the low-level details of how to loop around a list or array (with an index or iterator), the developer simply states that they want to loop and the language takes care of the rest.
However, all the benefit is lost as soon as the developer needs to access the index or to remove an item. The original Java 5 for each work took a relatively conservative stance on a number of issues aiming to tackle the 80% case.
However, loops are such a common form in coding that the remaining 20% that was not tackled represents a significant body of code.
The process of converting the loop back from the for each to be index or iterator based is painful. This is because the old loop style if significantly lower-level, is more verbose and less clear. It is also painful as most IDEs don't support this kind of 'de-refactoring'.
MAJOR BENEFIT :
A common coding idiom is expressed at a higher abstraction than at present. This aids readability and clarity.
# Iterating over Array list and Hash Set using for-each loop is similar and simple.
# Iterating over Hash Map:
The simplest method to iterate over a Hash Map is by using a for-each loop.
I have used stuff from JAVA Tutorials provided by Oracle and some answers I got from
Stack Overflow. You can find these things online out there, but I have tried using those
things in a simple way and that you can find at one place.
Please let me know if I am wrong or if there are any other easy and efficient methods that can be used to iterate over these collections.
I would love learning something more.
Please leave a comment if this post helped you or if there are any suggestions.
~SunMit
Collections works a bit like arrays where unlike arrays, collections prove very important when you expect their size to change dynamically.
The Iterable interface (java.lang.Iterable) is one of the root interfaces of the Java collection classes.
The Collection interface extends Iterable, so all subtypes of Collection also implement the Iterable interface.
Considering the three most frequently used Collection types ie. Array List, Hash Set and Hash Map, this post will help you iterate over these collections.
Iterating using iterators :
1) Iterate over Array List:
List<Object> list = new ArrayList<Object>(); while(listIterator.hasNext()){ Object object = listIterator.next(); // do something with object }
2) Iterate over Hash Set:
HashSet is backed by a HashMap. It makes no guarantees about the sequence of the elements when you iterate them.
LinkedHashSet differs from HashSet by guaranteeing that the order of the elements during iteration is the same as the order they were inserted into the LinkedHashSet.
Reinserting an element that is already in the LinkedHashSet does not change this order.
Set<Object> set = new HashSet<Object>(); Iterator setIterator = set.iterator(); while(setIterator.hasNext()){ Object object = setIterator.next(); // Do something with object }
3) Iterate over Hash Map:
HashMap maps a key and a value. It does not guarantee any order of the elements stored internally in the map.
TreeMap also maps a key and a value and it guarantees the order in which keys or values are iterated - which is the sort order of the keys or values.
Iterating over a Hash Map is tricky as Hash map contains a key-value pair.
A map entry (key-value pair). The Map.entrySet method returns a collection-view of the map, whose elements are of this class.
Map<Object> map = new HashMap<Object>(); Iterator entries = map.entrySet().iterator(); while (entries.hasNext()) { Entry thisEntry = (Entry) entries.next(); Object key = thisEntry.getKey(); Object value = thisEntry.getValue(); // do something with key and value }
Iterating using for-each loop and its advantages :
JAVA later introduced 'Enhanced for each loop iteration control', which extends the JAVA5 for-each loop to allow access to the loop index, whether this is the first or last iteration, and to remove the current item.
MAJOR ADVANTAGE :
The for-each loop is almost certainly the most new popular feature from Java 5. It works because it increases the abstraction level - instead of having to express the low-level details of how to loop around a list or array (with an index or iterator), the developer simply states that they want to loop and the language takes care of the rest.
However, all the benefit is lost as soon as the developer needs to access the index or to remove an item. The original Java 5 for each work took a relatively conservative stance on a number of issues aiming to tackle the 80% case.
However, loops are such a common form in coding that the remaining 20% that was not tackled represents a significant body of code.
The process of converting the loop back from the for each to be index or iterator based is painful. This is because the old loop style if significantly lower-level, is more verbose and less clear. It is also painful as most IDEs don't support this kind of 'de-refactoring'.
MAJOR BENEFIT :
A common coding idiom is expressed at a higher abstraction than at present. This aids readability and clarity.
# Iterating over Array list and Hash Set using for-each loop is similar and simple.
for(Object object: list){ // Do something with object } for(Object object: set){ // Do something with object }
# Iterating over Hash Map:
The simplest method to iterate over a Hash Map is by using a for-each loop.
for (Map.Entry<Object> entry : map.entrySet()){ Object key = thisEntry.getKey(); Object value = thisEntry.getValue(); // do something with key and value }
I have used stuff from JAVA Tutorials provided by Oracle and some answers I got from
Stack Overflow. You can find these things online out there, but I have tried using those
things in a simple way and that you can find at one place.
Please let me know if I am wrong or if there are any other easy and efficient methods that can be used to iterate over these collections.
I would love learning something more.
Please leave a comment if this post helped you or if there are any suggestions.
~SunMit
No comments:
Post a Comment