Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

403
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

0

Yes, there is retention. retainAll this .

 Set<Type> intersection = new HashSet<Type>(s1); intersection.retainAll(s2);
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda