Suppose we have keys as strings and values as list of objects in a HashMap. Now we 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.

So this example is going to show you in step by step.

Example

Create below lists with static values

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"));

Then suppose you have put these list of values in 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);

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 Java, JEE, Hibernate etc. will be values.

So 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

package com.roytuts.lambda.map;

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.lambda.map;

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

public class MapLambdaTest {

    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

Thanks for reading.

Tags:

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Roy Tutorials | TwitterFacebook Google PlusLinkedin | Reddit

Leave a Reply

Your email address will not be published. Required fields are marked *