2011-12-12 11 views
6

E 'strano: A è un set e B è un insieme di insiemi:Come rilevare se un Set di Set contiene un altro Set?

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

ho aggiunto le cose a loro e l'uscita del

System.out.println(A) 

è:

[evacuated, leave, prepc_behind] 

e l'output di

System.out.println(B) 
.210

è:

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

come si può vedere, terzo elemento della serie B è uguale per impostare A. Quindi ipoteticamente

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

deve restituire vero, ma a quanto pare non è così. Qual è il problema?

piu 'dettagli:

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

Come stai aggiungendo gli elementi? Perché "B.taintains (A)" restituisce il vero per me. –

+0

Funziona come previsto (vale a dire, restituisce true) anche per me. –

+0

Ho un ciclo for che continua ad aggiungere i set a B usando: B.add (tp); tp è un set. – Marcus

risposta

-1

Set.contains (altro) Restituisce vero se un elemento appartiene alla serie è uguale ad altri.

E Set ha l'override di equals() e hash(). Set.equals() restituisce true se entrambi i set hanno gli stessi elementi.

Quindi, se A2 appartiene a B, e A2 ha gli stessi elementi di A, B.contains (A) restituirà true;

2

L'idea di base (setA.contains(setB) == true) sembra funzionare bene:

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 

mi azzarderei a dire che si sta acquisendo maggiore nella vostra espressione regolare, allora si vorrebbe. Provare a convertire sia la corrispondenza dall'espressione regolare che la stringa di test in byte[] e verificarli l'uno rispetto all'altro.

Problemi correlati