Skip to content

Desafio 7 #22

Description

@raulward

Existe alguma inconsistência ou má prática em fazer dessa forma?

Além disso, existe alguma maneira mais eficiente de fazer a busca do elemento?

public class Desafio7 {
    public static void main(String[] args) {
        List<Integer> numeros = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 4, 3);
        int maiorNumero = numeros.stream()
                .max(Comparator.naturalOrder())
                .orElseThrow(() -> new NoSuchElementException("Nao foi possivel achar o elemento"));

        int segundoMaiorNumero = numeros.stream()
                .filter(n -> n != maiorNumero)
                .max(Comparator.naturalOrder())
                .orElseThrow(() -> new NoSuchElementException("Nao foi possivel achar o elemento"));

        System.out.println(segundoMaiorNumero);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions