Java Collection, HashSet Exercises: Iterate through all elements in a hash listLast update on December 14 2021 06:27:40 (UTC/GMT +8 hours)Iterate through List in JavaLists in java allow us to maintain an ordered collection of objects. Duplicate elements as well as null elements can also be stored in a List in Java. The List interface is a part of java.util package and it inherits the Collection interface. It preserves the order of insertion. Show There are several ways to iterate over List in Java. They are discussed below: Methods:
Method 1-A: Simple for loop Each element can be accessed by iteration using a simple for loop. The index can be accessed using the index as a loop variable. Syntax: for (i = 0; i < list_name.size(); i++) { // code block to be executed }Example Java
Output
A
B
C
D
Method 1-B: Enhanced for loop Each element can be accessed by iteration using an enhanced for loop. This loop was introduced in J2SE 5.0. It is an alternative approach to traverse the for a loop. It makes the code more readable. Syntax: for(data_type variable : List_name) { // Body of the loop. // Each element can be accessed using variable. }Java
Output
A
B
C
D
Method 1-C: Using a while loop Iterating over a list can also be achieved using a while loop. The block of code inside the loop executes until the condition is true. A loop variable can be used as an index to access each element. Syntax: while(variable<list_name.size()) { // Block of code to be executed }Java
Output
A
B
C
D
Method 2: Using iterator An iterator is an object in Java that allows iterating over elements of a collection. Each element in the list can be accessed using iterator with a while loop. Syntax: Iterator<data_type> variable = list_name.iterator();Example Java
Output
A
B
C
D
Method 3: Using List iterator ListIterator is an iterator is a java which is available since the 1.2 version. It allows us to iterate elements one-by-one from a List implemented object. It is used to iterator over a list using while loop. Syntax ListIterator<data_type> variable = list_name.listIterator();Java
Output
A
B
C
D
Method 4: Using Iterable.forEach() This feature is available since Java 8. It can also be used to iterate over a List. Iteration can be done using a lambda expression. Syntax: list_name.forEach(variable->{//block of code})Java
Output
A
B
C
D
Method 5: Using Stream.forEach() The processing order of stream().forEach() is undefined while in case of forEach(), it is defined. Both can be used to iterate over a List. Syntax: list_name.stream.forEach(variable->{//block of code})Java
Output
A
B
C
D
Article Tags :
Java Java Programs
Java-Collections java-list Practice Tags :
Java Java-Collections How to Iterate Through HashTable in Java?HashTable is an underlying data structure where the insertion order in HashTable is not preserved, and it is based on the hashcode of keys. Duplicates keys are not allowed, but values can be duplicated. Heterogeneous objects are allowed for both keys and values. Value null is not allowed for both key and value otherwise we will get RunTimeException saying NullPointerException. It implements serializable and cloneable interfaces but not RandomAccess. Every method inside it is synchronized and hence HashTable objects are thread-safe. HashTable is the best choice if our frequent operation is search operation. Methods: There are various ways by which we can iterate through the HashTable which are as follows:
Now let us discuss the internal implementation of all methods one by one in detail to get a better understanding of iteration through HashTable Method 1: Using Enumeration Interface java.util.Enumeration interface is one of the predefined interfaces, whose object is used for retrieving the data from collections framework variable. In a forward direction only and not in the backward direction. This interface has been superseded by an iterator. Java
Output
Rank : 5 Name : Piyush
Rank : 4 Name : Hritik
Rank : 3 Name : Bijay
Rank : 2 Name : Shyam
Rank : 1 Name : Ram
Method 2: Using keySet() method of Map and Enhance for loop The java.util.HashMap.keySet() method in Java is used to create a set out of the key elements contained in the hash map. It basically returns a set view of the keys, or we can create a new set and store the key elements in them. Java
Output
Rank : 5 Name : R
Rank : 4 Name : Pearl
Rank : 3 Name : Python
Rank : 2 Name : Scala
Rank : 1 Name : Java
Method 3: Using keySet( ) method of Map and Iterator Interface Again we will be using the same method as been implemented in the above example but here for iteration we will be using the Iterable interface Java
Output
Rank : 5 Name : R
Rank : 4 Name : Pearl
Rank : 3 Name : Python
Rank : 2 Name : Scala
Rank : 1 Name : Java
Method 4: Using entrySet() method of Map and enhanced for loop The java.util.HashMap.entrySet() method in Java is used to create a set out of the same elements contained in the hash map. It basically returns a set view of the hash map, or we can create a new set and store the map elements into them. Java
Output
Rank : 5 Name : R
Rank : 4 Name : Pearl
Rank : 3 Name : Python
Rank : 2 Name : Scala
Rank : 1 Name : Java
Method 5: Using entrySet() method of Map and Iterator interface Again we will be using the same method as been implemented in the above example but here for iteration we will be using the Iterable interface Java
Output
Rank : 5 Name : R
Rank : 4 Name : Pearl
Rank : 3 Name : Python
Rank : 2 Name : Scala
Rank : 1 Name : Java
6. Using Iterable.forEach() method from version Java 8 With the coming of havoc new features in version, 8. It has been Quite a while since Java 8 released. With the release, they have improved some existing APIs and added few new features. One of them is forEach() method in java.lang.Iterable Interface. Java
Output
Rank : 1 Name : Java
Rank : 2 Name : Scala
Rank : 3 Name : Python
Rank : 4 Name : Ruby
Rank : 5 Name : R
Article Tags :
Java Java Programs
Java-Collections Java-HashTable Practice Tags :
Java Java-Collections Java Program to Iterate over a HashMapIn this example, we will learn to iterate over keys, values, and key/value mappings of a Java HashMap. To understand this example, you should have the knowledge of the following Java programming topics:
In Java HashMap, we can iterate through its keys, values, and key/value mappings. Java Program to Iterate over a SetIn this example, we will learn to iterate over the elements of a set in Java. To understand this example, you should have the knowledge of the following Java programming topics:
|