One machine can do the work of fifty ordinary men. No machine can do the work of one extraordinary man

Saturday, December 29, 2018

Iterate Map in Java 8


Java 1.8 is a huge bump in the programming world with the introduction of great features sometimes object oriented programmers didn't even imagine. They introduced some of the functional programming features into Java world such as functional interfaces, lamda functions and that helps programmers to use object oriented concepts in a better way.

Sometimes iterating through a collection such as a map or list is a headache for a programmer since there can be concurrent issues.


The most famous way of doing it is using an Iterator (This is the best way before 1.8).

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class Main {

public static void main(String[] args) {

// Creating students.
Student s01 = new Student("001", "Steven");
Student s02 = new Student("002", "Mathews");

// Creating a map.
Map<String, Student> students = new HashMap<>();

// Populating the map.
students.put(s01.getId(), s01);
students.put(s02.getId(), s02);

// Get the Iterator.
Iterator<Map.Entry<String, Student>> iterator = students.entrySet().iterator();

// Iterator through the map using the iterator.
while(iterator.hasNext()) {

Map.Entry<String, Student> studentEntry = iterator.next();

// Can retrieve the id if required.
String id = studentEntry.getKey();

// Retrieve the student from the map entry.
Student student = studentEntry.getValue();

System.out.println(student);

}
}


static class Student {

private String id;
private String name;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Student(String id, String name) {
this.id = id;
this.name = name;
}

@Override
public String toString() {
return "Student{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
'}';
}
}
}


The simplest way is a for loop.


public static void main(String[] args) {
// Creating students.
Student s01 = new Student("001", "Steven");
Student s02 = new Student("002", "Mathews");

// Creating a map.
Map<String, Student> students = new HashMap<>();

// Populating the map.
students.put(s01.getId(), s01);
students.put(s02.getId(), s02);

// Iterator through the entry set.
for(Map.Entry<String, Student> entry: students.entrySet()) {

// Accessing the entry.
Student student = entry.getValue();

System.out.println(student);
}
}

Now with the introduction of lamda functions, we have a better way of doing this.

public static void main(String[] args) {
// Creating students.
Student s01 = new Student("001", "Steven");
Student s02 = new Student("002", "Mathews");

// Creating a map.
Map<String, Student> students = new HashMap<>();

// Populating the map.
students.put(s01.getId(), s01);
students.put(s02.getId(), s02);

// Use forEach method for a collection in Java 8.
students.entrySet().forEach((studentEntry) -> {

// Access the entry of the map.
Student student = studentEntry.getValue();

System.out.println(student);

});
}


Hope this helps you, please let me know your comments about the post.

No comments:

Post a Comment