/**
- 3、已知字符串“adhflkalkfdhasdkhflsa”
(1)统计去掉重复后的字符
(2)统计每个字符出现的次数,使用map存储,字符为键,次数为值。
(3)遍历map,打印统计信息
*/
package T3;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
public class T3 {
public static void main(String[] args)
{
int []count=new int[7];
String str="adhflkalkfdhasdkhflsa";
char[]arr=str.toCharArray();
Map<Character,Integer> map=new HashMap<Character,Integer>();
List<Character> list=new ArrayList<Character>();
HashSet<Character> set=new HashSet<Character>();
for(int i=0;i<arr.length;i++)
{
list.add(arr[i]);
set.add(arr[i]);
}
String st=set.toString();
System.out.println("去掉重复元素后"+set+"\n"+"去掉重复后的字符统计:"+set.size());
Collections.sort(list);
// System.out.println(list);
char[] s=new char[]{'a','d','f','h','k','l','s'};
for(int i=0;i<list.size();i++)
{
if(list.get(i)=='a')
count[0]++;
if(list.get(i)=='d')
count[1]++;
if(list.get(i)=='f')
count[2]++;
if(list.get(i)=='h')
count[3]++;
if(list.get(i)=='k')
count[4]++;
if(list.get(i)=='l')
count[5]++;
if(list.get(i)=='s')
count[6]++;
}
for(int i=0;i<s.length;i++)
{
map.put(s[i], count[i]);
}
map.forEach((k,v)->System.out.println(k+":"+v+"个"));
}
}