Java 8 flatmap , stream,collect -
i have situation have class { private b b; public b getb() { return b; } } and class b class b { private list<c> c; public list<c> getlistc() { return c; } } now class c contains 2 instance variables class c { private int id; private string name; public int getid() { return id; } public string getname() { return name; } } now want achieve below using java 8 list<c> newlistc = a.getb().getlistc(); map<integer, string> map = new hashmap<>(); for(c c : newlistc) { map.put(c.getid,c.getname()); } i have tried many time every time face different problems. code: optional<a> a=optional.of(new a()); map<integer, string> map= a.map(a::getb) .flatmap(b -> b.getlistc() .stream() ...