import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Map; import java.util.Set; public class Test { public static void main(String[] args) { String s1 = "123456abcde"; String s2 = "111222bbbcc"; System.out.println(new Test().findSame(s1, s2)); } public String findSame(String s1, String s2) { Map map = new HashMap(); Set result = new HashSet(); char[] char1 = s1.toCharArray(); char[] char2 = s2.toCharArray(); for (int i = 0; i < char1.length; i++) { map.put("" + char1[i], char1[i]); } for (int j = 0; j < char2.length; j++) { if (map.get("" + char2[j]) != null) { result.add(char2[j]); } } /*for( Iterator it = result.iterator(); it.hasNext(); ) { System.out.println("value="+it.next().toString()); }*/ return result.toString(); } }
时间: 2024-10-09 15:48:47