Java Data Type How to - Check containment in a Set of strings








Question

We would like to know how to check containment in a Set of strings.

Answer

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/*w  ww .  j  a v  a  2s  .  com*/
public class Main {

  public static void main(final String[] args) {
    final Set<String[]> s = new HashSet<String[]>();
    final Set<List<String>> s2 = new HashSet<List<String>>();

    s.add(new String[] { "lucy", "simon" });
    s2.add(Arrays.asList(new String[] { "lucy", "simon" }));

    System.out.println(s.contains(new String[] { "lucy", "simon" })); // false
    System.out.println(s2.contains(Arrays
        .asList(new String[] { "lucy", "simon" }))); // true
  }

}