Question: I have some strings defined in my Java application, like so:

```
 m3 = "T, V, E";
 m2 = "T, W, E";

```

as an example.

Now I need to check, which parts of the strings match. So in this case, I would want a string `m4`, containing `T, E`, as a result.

In that case for example:

```
m1 = "A, S";
m3 = "T, V, E";

```

i would want an empty (but declared) string.

Or is there a better way, to do it with another method then with strings? I'm actually declaring those strings by hand. Would an array be better? If yes, how could I do it with arrays?

Answer: 
The appropriate data structure is Set.

```
    Set<String> m3 = new TreeSet<>();
    Collections.addAll(m3, "T", "V", "E");
    Collections.addAll(m3, "T, V, E".split(",\\s*")); // Alternatively.

    Set<String> m2 = new HashSet<>();
    Collections.addAll(m2, "T", "W", "E");

    Set<String> m5 = new TreeSet<>(m2);
    m5.retainAll(m3);

```

Java 9:

```
    Set<String> m3 = Set.of("T", "V", "E");
    Set<String> m2 = Set.of("T", "W", "E");

```
String s1 = "T,V,E";
 String s2 = "T,W,E";

```
List<String> l1 = Arrays.asList(s1.split(","));
List<String> l2 = Arrays.asList(s2.split(","));

List<String> result = l1.stream().filter(l2::contains).collect(Collectors.toList());

System.out.println(String.join(",", result));

```

result = T&E
This is good answer I will tell you too this