2011-12-12 20 views
6

Es raro: A es un conjunto y B es un conjunto de conjuntos:¿Cómo detectar si un conjunto de conjuntos contiene otro conjunto?

Set <String> A=new HashSet<String>(); 
Set <Set<String>> B=new HashSet<Set<String>>(); 

he añadido cosas a ellos y la salida de

System.out.println(A) 

es:

[evacuated, leave, prepc_behind] 

y la salida de

System.out.println(B) 

es:

[[leave, to, aux], [auxpass, were, forced], [leave, evacuated, prepc_behind]] 

como se puede observar, el tercer elemento del conjunto B es igual a la serie A. Por lo tanto, hipotéticamente

if(B.contains(A)){...} 

debe devolver cierto, pero al parecer no es así. ¿Cuál es el problema?

Más detalles:

Pattern pattern = Pattern.compile("(.*?)\\((.*?)\\-\\d+,(.*?)\\-\\d+\\).*"); 
    for (int i = 0; i < list.size(); i++) { 
     Set <String> tp = new HashSet<String>(); 
     Matcher m = pattern.matcher(list.get(i).toString()); 
     if (m.find()) { 
      tp.add(m.group(1).toLowerCase()); 
      tp.add(m.group(2).toLowerCase()); 
      tp.add(m.group(3).toLowerCase()); 
     } 
     B.add(tp); 
    } 
    Set <String> A=new HashSet<String>(); 
    A.add("leave"); 
    A.add("evacuated"); 
    A.add("prepc_behind"); 
    System.out.println(A); 
    if(B.contains(A)){ 
    System.out.println("B contains A"); 
} 
+3

¿Cómo está agregando elementos? Porque 'B.contains (A)' devuelve verdadero para mí. –

+0

Funciona según lo previsto (es decir, devuelve cierto) para mí también. –

+0

Tengo un bucle for que sigue agregando conjuntos a B usando: B.add (tp); tp es un conjunto. – Marcus

Respuesta

-1

Set.contains (otra) devuelven cierto si un elemento pertenece al conjunto es igual a otra.

Y Set ha anulado equals() y hash(). Set.equals() devolverá true si ambos conjuntos tienen los mismos elementos.

Entonces, si A2 pertenece a B, y A2 tiene los mismos elementos que A, B.contains (A) devolverá verdadero;

2

La idea básica (setA.contains(setB) == true) parece funcionar bien:

Set<String>  set1 = new HashSet<String>(); 
    Set<Set<String>> set2 = new HashSet<Set<String>>(); 
    Set<String>  tmpSet; 

    set1.add("one"); 
    set1.add("three"); 
    set1.add("two"); 

    tmpSet = new HashSet<String>(); 
    tmpSet.add("1"); 
    tmpSet.add("2"); 
    tmpSet.add("3"); 
    set2.add(tmpSet); 

    tmpSet = new HashSet<String>(); 
    tmpSet.add("one"); 
    tmpSet.add("two"); 
    tmpSet.add("three"); 
    set2.add(tmpSet); 

    System.out.println(set2.contains(set1)); // true 

Me atrevería a decir que va a capturar más en su expresión regular entonces que le gustaría. Intente convertir tanto la coincidencia de la expresión regular como la cadena de prueba en byte[] y verifíquelas entre sí.

Cuestiones relacionadas