Java 8: iterate over Map of List of Objects

In this example I am going to show you how to iterate over map of list of objects. Let’s say you have keys as strings and values as list of objects in a HashMap. Now you want to iterate list of objects and want make one attribute of the object as keys and other attribute(s) as values and want to put as key/value pairs in another map.

Map Of List Of Objects

Here I am creating few list of objects first. Then I will create map of list of objects.

List<ProgInfo> javaList = Arrays.asList(new ProgInfo("1000", "Java"), new ProgInfo("1001", "JEE"));

List<ProgInfo> frameworkList = Arrays.asList(new ProgInfo("1002", "Spring"), new ProgInfo("1003", "JSF"),
		new ProgInfo("1004", "Hibernate"), new ProgInfo("1005", "JPA"));

List<ProgInfo> databaseList = Arrays.asList(new ProgInfo("1006", "Oracle"), new ProgInfo("1007", "PostgreSQL"),
		new ProgInfo("1008", "MySQL"), new ProgInfo("1009", "Sybase"), new ProgInfo("1010", "DB2"));

List<ProgInfo> uiList = Arrays.asList(new ProgInfo("1011", "HTML"), new ProgInfo("1012", "CSS"));

List<ProgInfo> jsList = Arrays.asList(new ProgInfo("1013", "JavaScript"), new ProgInfo("1014", "jQuery"),
		new ProgInfo("1015", "ExtJS"), new ProgInfo("1016", "AngularJS"));

Let’s say, you have put these list of objects into a map as shown below:

Map<String, List<ProgInfo>> map = new HashMap<>();
map.put("java", javaList);
map.put("framework", frameworkList);
map.put("db", databaseList);
map.put("ui", uiList);
map.put("js", jsList);

Iterate Map Of List Of Objects

Now you want to iterate the above map and want to extract the list of values and want to keep into another map, where ids such as 1000, 1001, 1002 etc. will be keys and values are Java, JEE, Hibernate etc.

Now create another map – idMap:

Map<String, String> idMap = new HashMap<>();

Iterate over map and put into idMap:

map.forEach((id, progList) -> progList.forEach(prog -> idMap.put(prog.getId(), prog.getName())));

The complete source code is given below:

package com.roytuts.java.iterate.map.of.list;

public class ProgInfo {

	private String id;
	private String name;

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

	public String getId() {
		return id;
	}

	public String getName() {
		return name;
	}

}
package com.roytuts.java.iterate.map.of.list;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MapIteratorApp {

	public static void main(String[] args) {
		List<ProgInfo> javaList = Arrays.asList(new ProgInfo("1000", "Java"), new ProgInfo("1001", "JEE"));

		List<ProgInfo> frameworkList = Arrays.asList(new ProgInfo("1002", "Spring"), new ProgInfo("1003", "JSF"),
				new ProgInfo("1004", "Hibernate"), new ProgInfo("1005", "JPA"));

		List<ProgInfo> databaseList = Arrays.asList(new ProgInfo("1006", "Oracle"), new ProgInfo("1007", "PostgreSQL"),
				new ProgInfo("1008", "MySQL"), new ProgInfo("1009", "Sybase"), new ProgInfo("1010", "DB2"));

		List<ProgInfo> uiList = Arrays.asList(new ProgInfo("1011", "HTML"), new ProgInfo("1012", "CSS"));

		List<ProgInfo> jsList = Arrays.asList(new ProgInfo("1013", "JavaScript"), new ProgInfo("1014", "jQuery"),
				new ProgInfo("1015", "ExtJS"), new ProgInfo("1016", "AngularJS"));

		Map<String, List<ProgInfo>> map = new HashMap<>();
		map.put("java", javaList);
		map.put("framework", frameworkList);
		map.put("db", databaseList);
		map.put("ui", uiList);
		map.put("js", jsList);

		Map<String, String> idMap = new HashMap<>();

		map.forEach((id, progList) -> progList.forEach(prog -> idMap.put(prog.getId(), prog.getName())));

		idMap.forEach((k, v) -> System.out.println("key: " + k + ", value: " + v));
	}

}

Output

key: 1000, value: Java
key: 1011, value: HTML
key: 1010, value: DB2
key: 1008, value: MySQL
key: 1007, value: PostgreSQL
key: 1006, value: Oracle
key: 1005, value: JPA
key: 1016, value: AngularJS
key: 1004, value: Hibernate
key: 1015, value: ExtJS
key: 1003, value: JSF
key: 1014, value: jQuery
key: 1002, value: Spring
key: 1013, value: JavaScript
key: 1001, value: JEE
key: 1012, value: CSS
key: 1009, value: Sybase

Source Code

Download

Thanks for reading.

Related posts

Leave a Comment