Map<String, Set<T> > to List in Java 8 -


so have class :

 class user {     public string name;     public integer age;      public user(string name, integer age) {         this.name = name;         this.age = age;     } } 

and map:

 map<string, set<integer>> map = new hashmap<>();  map.put("user", set.of(18, 19, 20, 21)); 

i want obtain list of 4 users here:

  1. name : "john" , age : 18
  2. name : "john" , age : 19
  3. ....
  4. ....

any ideas how can streams in java 8 ?

list<user> users = map.entryset()    .stream()    .flatmap(e -> e.getvalue().stream().map(x -> new user(e.getkey(), x)))    .collect(collectors.tolist()); 

set.of java-9 btw


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -