To compare 2 integer arrays using Java 8 Features -
this question has answer here:
- how intersection between 2 arrays new array? 22 answers
is possible without external foreach iterate b. need identify common values in 2 arays using java 8
integer a[]={1,2,3,4}; integer b[]={9,8,2,3}; for(integer b1:b) { stream.of(a).filter(a1 -> (a1.compareto(b1) ==0)).foreach(system.out::println); } output: 2 3
i suggest using sets if want common values (i.e. not taking duplicates account)
integer a[]={1,2,3,4}; integer b[]={9,8,2,3}; set<integer> aset = new hashset<>(arrays.aslist(a)); set<integer> bset = new hashset<>(arrays.aslist(b)); aset.retainall(bset);
Comments
Post a Comment