I have the below method which checks if given two strings have atleast one character in common
String checkCommonCharacters(String s1, String s2) {
Set<String> s1Chars = new HashSet(Arrays.asList(s1.split("")));
Set<String> s2Chars = new HashSet(Arrays.asList(s2.split("")));
String res = Collections.disjoint(s1Chars, s2Chars) ? "NO" : "YES";
System.out.println(res);
return res;
}
Above code works fine but if I change it to a Character
set it doesnt work. Having the hashcode()
and equals()
in Character
class why HashSet
is not behaving the same way as of String
type?
Set<Character> s1Chars = new HashSet(Arrays.asList(s1.toCharArray()));
Set<Character> s2Chars = new HashSet(Arrays.asList(s2.toCharArray()));
Example:
Input s1="abc"
, s2="dea"
output is YES
for String
type
Input s1="abc"
, s2="dea"
output is NO
for Character
type