Как об использовании Set
, если требуется только уникальные соответствия символов:
Пример кода:
import java.util.HashSet;
import java.util.Set;
public class IntersectionChar {
public static void main(String[] args) {
findMatchingChars("shojib", "farhana");
findMatchingChars("one", "onetwothere");
findMatchingChars("temp", "test");
findMatchingChars("dis", "connected");
findMatchingChars("null", "empty");
}
private static void findMatchingChars(String a, String b) {
Set<Character> namea1Chars = new HashSet<Character>();
Set<Character> namea2Chars = new HashSet<Character>();
for (char c : a.toCharArray()) {
namea1Chars.add(c);
}
for (char c : b.toCharArray()) {
namea2Chars.add(c);
}
namea1Chars.retainAll(namea2Chars);
System.out.println("Common Chars in " + a + " & " + b + " are => " + namea1Chars);
}
}
выход будет:
Common Chars in shojib & farhana are => [h]
Common Chars in one & onetwothere are => [e, n, o]
Common Chars in temp & test are => [e, t]
Common Chars in dis & connected are => [d]
Common Chars in null & empty are => []
Если вы хотите захватить все вхождения, тогда вместо этого используйте List
(Хотя будет влияние на производительность:
Пример кода:
import java.util.ArrayList;
import java.util.List;
public class IntersectionChar {
public static void main(String[] args) {
findMatchingChars("shojib", "farhana");
findMatchingChars("one", "onetwothere");
findMatchingChars("tempt", "test");
findMatchingChars("dis", "connected");
findMatchingChars("null", "empty");
}
private static void findMatchingChars(String a, String b) {
List<Character> namea1Chars = new ArrayList<Character>();
List<Character> namea2Chars = new ArrayList<Character>();
for (char c : a.toCharArray()) {
namea1Chars.add(c);
}
for (char c : b.toCharArray()) {
namea2Chars.add(c);
}
namea1Chars.retainAll(namea2Chars);
System.out.println("Common Chars in " + a + " & " + b + " are => " + namea1Chars);
}
}
Выход:
Common Chars in shojib & farhana are => [h]
Common Chars in one & onetwothere are => [o, n, e]
Common Chars in tempt & test are => [t, e, t]
Common Chars in dis & connected are => [d]
Common Chars in null & empty are => []
Вы также можете использовать Map
для лучшей производительности
Пример кода:
import java.util.HashMap;
import java.util.Map;
public class IntersectionChar {
public static void main(String[] args) {
findMatchingChars("shojib", "farhana");
findMatchingChars("one", "onetwothere");
findMatchingChars("tempt", "test");
findMatchingChars("dis", "connected");
findMatchingChars("null", "empty");
}
private static void findMatchingChars(String a, String b) {
System.out.print("Common Chars in " + a + " & " + b + " are => [");
Map<Character, Integer> aCharCount = new HashMap<Character, Integer>();
for (char c : a.toCharArray()) {
if(aCharCount.containsKey(c)) {
aCharCount.put(c, aCharCount.get(c) + 1);
} else {
aCharCount.put(c, 1);
}
}
for (char c : b.toCharArray()) {
if(aCharCount.containsKey(c)) {
System.out.print(c + ", ");
if (aCharCount.get(c) == 1) {
aCharCount.remove(c);
} else {
aCharCount.put(c, aCharCount.get(c) - 1);
}
}
}
System.out.println("]");
}
}
Выход:
Common Chars in shojib & farhana are => [h, ]
Common Chars in one & onetwothere are => [o, n, e, ]
Common Chars in tempt & test are => [t, e, t, ]
Common Chars in dis & connected are => [d, ]
Common Chars in null & empty are => []
@JunedAhsan, 'h' является распространенным явлением. – Ambrish