DEV Community

FUNDAMENTOS JAVA
FUNDAMENTOS JAVA

Posted on

1 1

6.3 Compondo comparators

1 Ordenando por Diferentes Critérios

  • Podemos ordenar usuários pelos pontos, evitando autoboxing com comparingInt:

usuarios.sort(Comparator.comparingInt(Usuario::getPontos));

2 Compondo Comparadores com thenComparing

  • Para ordenar por pontos e, em caso de empate, por nome:

Comparator<Usuario> c = Comparator.comparingInt(Usuario::getPontos)
.thenComparing(Usuario::getNome);
usuarios.sort(c);

  • Há métodos equivalentes como thenComparingInt para evitar boxing.

3 Lidando com Valores null

  • Para mover valores null para o final da lista:

usuarios.sort(Comparator.nullsLast(Comparator.comparing(Usuario::getNome)));

  • O método nullsFirst faz o contrário, colocando null no início.

4 Ordenando em Ordem Decrescente

  • Usamos reversed() no Comparator:

usuarios.sort(Comparator.comparing(Usuario::getPontos).reversed());

5 Explorando Novos Métodos

  • A API de Comparator traz muitas melhorias, e é importante pesquisar e experimentar para evitar escrever código desnecessário.

Quadratic AI

Quadratic AI – The Spreadsheet with AI, Code, and Connections

  • AI-Powered Insights: Ask questions in plain English and get instant visualizations
  • Multi-Language Support: Seamlessly switch between Python, SQL, and JavaScript in one workspace
  • Zero Setup Required: Connect to databases or drag-and-drop files straight from your browser
  • Live Collaboration: Work together in real-time, no matter where your team is located
  • Beyond Formulas: Tackle complex analysis that traditional spreadsheets can't handle

Get started for free.

Watch The Demo 📊✨

Top comments (0)

Image of DataStax

AI Agents Made Easy with Langflow

Connect models, vector stores, memory and other AI building blocks with the click of a button to build and deploy AI-powered agents.

Get started for free

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay