目的:掌握LinkedList、HashSet、HashMap的使用方法。
要求:给定字符串数组,找出各个字符串中出现的公共字符。
例子:给定字符串数组中有三个字符串"look", “lock”, “cook”,
输出字符’k’, ‘o’,输出字符的顺序不做要求。
package com.cr.test;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
/*
目的:掌握LinkedList、HashSet、HashMap的使用方法。
要求:给定字符串数组,找出各个字符串中出现的公共字符。
例子:给定字符串数组中有三个字符串"look", "lock", "cook",
输出字符’k’, ‘o’,输出字符的顺序不做要求。
*/
public class PublicChar {
public static void main(String[] args) {
String strName[]={"look","lock","cook"};
LinkedList<HashSet<Character>> Filter = new LinkedList<HashSet<Character>>();
for (int i = 0;i < strName.length;i++){
HashSet<Character> temp = new HashSet<Character>();
for (int j = 0;j < strName[i].length();j++){
temp.add(strName[i].charAt(j));
}
Filter.add(temp);
}
HashMap<Character,Integer> sta = new HashMap<Character, Integer>();
for (int i = 0;i < Filter.size();i++){
for (Character c : Filter.get(i)){
if (sta.containsKey(c)){
sta.put(c,sta.get(c)+1);
}else{
sta.put(c,1);
}
}
System.out.println(sta);
}
}
}