Giter VIP home page Giter VIP logo

Comments (2)

cami-la avatar cami-la commented on August 12, 2024 1

Oi, Rafa. Tudo bem?

Obrigada pela pergunta, bem interessante. Vamos lá: quando você inicia uma List com o Arrays.asList() você cria uma lista que não pode ser modificada. Daí, você esta tentando usar um método que para ele ser executado, por default, ele precisa modificar o objeto.

Screenshot from 2023-08-17 14-46-19

Iniciei o objeto números com uma lista que pode ser modificada, e deu certo. Sua implementação do método removeIf() está correta!

Aí para utilizar o método removeIf() você poderá fazer da seguinte forma:

    List<Integer> numeros = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 4, 3);
    /*numeros.removeIf(n -> n % 2 != 0);
    System.out.println(numeros);*/

    //Fazendo uma cópia da lista que não pode ser modificada:
    List<Integer> numeros2 = new ArrayList<>(numeros);

    // Aqui retornará true porque as duas listas contêm os mesmos elementos na mesma ordem.
    //Porém, elas são objetos distintos em memória.
    System.out.println(numeros2.equals(numeros));

    //Agora sim você consegue remover os elementos:
    numeros2.removeIf(n -> n % 2 != 0);
    System.out.println(numeros2);
  }
}

Screenshot from 2023-08-17 15-16-25

from ganhando_produtividade_com_stream_api_java.

RafaSamm avatar RafaSamm commented on August 12, 2024 1

olá, tudo bem Cami....agora eu entendi...muito obrigado...simples e direto....perfeito. No máximo eu estava somente isolando os ímpares dos pares, obrigado!!!!

from ganhando_produtividade_com_stream_api_java.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.