Interview Question? How to find square of each element in this Array {2,4,5,6,8} using map ?

 Q. How to find square of each element in this Array {2,4,5,6,8}

Output : [4, 16, 25, 36, 64]

Solution :-


package p3;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class MapExample {

public static void main(String[] args) {
Integer[] abc={2,4,5,6,8};
List<Integer> list=Arrays.asList(abc);
List<Integer> squareEle=list.stream().map(i->i*i).collect(Collectors.toList());
System.out.println(squareEle);
for(Integer i:squareEle) {
System.out.println(i);

}
 
}

}

********************************************************************
O/P:-  Output : [4, 16, 25, 36, 64]


Comments