How to map each element of List<Integer> to an i * i calculation and print unique square of the numbers in Java

1 Answer

0 votes
package javaapplication1;

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

public class JavaApplication1 {

    public static void main(String[] args) {

        List<Integer> n = Arrays.asList(2, 3, 2, 2, 4, 5, 6);
        List<Integer> n1 = n.stream().map(i -> i * i).distinct().collect(Collectors.toList());

        System.out.println(n1);
    }
}

/*
run:

[4, 9, 16, 25, 36]

*/

 



answered Sep 5, 2016 by avibootz
...