Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

400
Visualizações
How to calculate the intersection of two sets?

Possible Duplicate:
Efficiently finding the intersection of a variable number of sets of strings

Say, have two Hashset, how to calculate the intersection of them?

Set<String> s1 = new HashSet<String>();

Set<String> s2 = new HashSet<String>();

S1 INT S2 ?
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Use the retainAll() method of Set :

 Set<String> s1; Set<String> s2; s1.retainAll(s2); // s1 now contains only elements in both sets

If you want to keep the sets, create a new set to contain the intersection:

 Set<String> intersection = new HashSet<String>(s1); // use the copy constructor intersection.retainAll(s2);

The retainAll() javadoc says that is exactly what you want:

Keeps only the elements of this set that are contained in the specified collection (optional operation). In other words, it removes from this set all its elements that are not contained in the specified collection. If the specified collection is also a set, this operation effectively modifies this set so that its value is the intersection of the two sets.

over 4 years ago · Santiago Trujillo Relatório

0

Yes, there is retention. retainAll this .

 Set<Type> intersection = new HashSet<Type>(s1); intersection.retainAll(s2);
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda