Java 8 filter by attribute -


i have class of following definition

public class myclass {   int val;   type t; } 

where type enum values a,b,c,d,....

i have list of objects of myclass , want filter out first element of each type occurring in list.

for example :-

given list:

{{1,a},{2,a},{4,b},{5,b},{3,c}} 

output:

{{1,a},{4,b},{3,c}} 

is there way use filter() of stream of list solve problem?

i'm not sure if there's way single stream pipeline, can two.

the first pipeline groups objects val property (producing map<integer,list<myclass>>) , second takes first object of each list produced first pipeline , collects them output list:

list<myclass>   filtered = mycl.stream ()                  .collect (collectors.groupingby (c - > c.val))                  .values ()                  .stream ()                  .map (l -> l.get (0))                  .collect (collectors.tolist ()); 

Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -