How to use Java 8 stream in an Object list []

3

Imagine the following scenario: A list of Object[] . Something similar to this:

    List<Object[]> lista = new ArrayList<>();
    Object[] dados = new Object[3];
    dados[0] = 1;
    dados[1] = 20;
    dados[1] = "cristiano";
    lista.add(dados);

    dados = new Object[3]; 
    dados[0] = 2;
    dados[1] = 40;
    dados[1] = "fulano";
    lista.add(dados);

How would I do to return a list of integers, containing only the values of the first position of the array, using Stream of java 8?

The expected result would be as follows:

1
2
    
asked by anonymous 05.09.2017 / 20:47

1 answer

6

Use this:

List<Integer> lista2 = lista.stream()
        .map(x -> (Integer) x[0])
        .collect(Collectors.toList());

System.out.println(lista2);

Output:

[1, 2]

See here working on ideone.

    
05.09.2017 / 21:16