//统计一句话中重复字符的个数(Queue)-----------------------------
package day081702;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
/**
* 统计一句话中各个字符的个数
*/
public class MapDemo01 {
public static void main(String[] args) {
String str = "good good study, day day up.";
Queue<Character> queue = toQueue(str);//把一串字符串处理为只含有小写字母的队列Queue
Map<Character,Integer> map = new HashMap<Character,Integer>();
while(queue.size()>0){
Character c = queue.poll();//取出首元素,并做删除操作
if(!map.containsKey(c)){
map.put(c, 1);
}else{
map.put(c, map.get(c)+1);
}
}
System.out.println(map);
}
/**
* 把一串字符串处理为只含有小写字母的Queue
* (其他无关字符过滤掉)
* @param str
* @return Queue<Character>
*/
public static Queue<Character> toQueue(String str){
//存储26个小写字母
List<Character> list26 = new ArrayList<Character>();
for(int i=0;i<26;i++){
Character c = (char)('a'+i);
list26.add(c);
}
//一旦是小写字母,存入queue
Queue<Character> queue = new LinkedList<Character>();
for(int i=0;i<str.length();i++){
Character c = str.charAt(i);//遍历每个字符
if(list26.contains(c)){//如果26个字母包含该字符,把该字符存入queue
queue.offer(c);
}
}
return queue;
}
}
//统计一句话中重复字符的个数(List)--------------------------------------
package day081702;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
public class MapDemo02 {
public static void main(String[] args) {
String str = "good good study,day,day,up.";
List<Character> list = toList(str);//把一串字符串处理为只含有小写字母的List
Map<Character,Integer> map = new HashMap<Character,Integer>();
long start = System.currentTimeMillis();
for(int i=list.size()-1;i>=0;i--){
Character c = list.get(i);
if(!map.containsKey(c)){
map.put(c, 1);
}else{
map.put(c, map.get(c)+1);
}
list.remove(i);
}
System.out.println(map);
long end = System.currentTimeMillis();
System.out.println(end-start);
}
/**
* 把一串字符串处理为只含有小写字母的List
* (其他无关字符过滤掉)
* @param str
* @return List<Character>
*/
public static List<Character> toList(String str){
//存储26个小写字母
List<Character> list26 = new ArrayList<Character>();
for(int i=0;i<26;i++){
Character c = (char)('a'+i);
list26.add(c);
}
//一旦是小写字母,存入list
List<Character> list = new ArrayList<Character>();
for(int i=0;i<str.length();i++){
Character c = str.charAt(i);//遍历每个字符
if(list26.contains(c)){//如果26个字母包含该字符,把该字符存入list
list.add(c);
}
}
return list;
}
}
//统计一句话中重复单词的个数--------------------------------------
package day081702;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Queue;
import java.util.Set;
/**
* 统计一句话中重复单词的个数
*/
public class MapDemo03 {
public static void main(String[] args) {
String str = "good good study, day day up.";
Queue<String> queue = toQueue(str);//toQueue()方法把String转为只包含单词的数组(过滤掉空格和标点符号)
/*
* 循环队列,放入map
*/
Map<String,Integer> map = new LinkedHashMap<String,Integer>();//LinkedHashMap可按顺序循环输出
//Map<String,Integer> map = new HashMap<String,Integer>();//HashMap无顺序输出
while(queue.size()>0){
String key = queue.poll();//取出首元素,并做删除操作
if(!map.containsKey(key)){//如果不包含该key,则装入key,计数1
map.put(key, 1);
}else{
map.put(key, map.get(key)+1);//如果已经包含该key,则value增加1
}
}
/*
* 循环key
*/
Set<String> keySet = map.keySet();
for(String setWord:keySet){//高级for循环
String word = setWord;
System.out.println(word+"的重复次数:"+map.get(word));
}
Iterator<String> it = keySet.iterator();
while(it.hasNext()){//用Iterator来循环
String word = it.next();
System.out.println(word+"的重复次数:"+map.get(word));
}
/*
* 循环Entry
*/
Set<Entry<String,Integer>> entrySet = map.entrySet();
for(Entry<String,Integer> entry: entrySet){//高级for循环
String word = entry.getKey();
Integer num = entry.getValue();
System.out.println(word+"重复次数:"+num);
}
Iterator<Entry<String,Integer>> entryIt = entrySet.iterator();
while(entryIt.hasNext()){//用Iterator来循环
Entry<String,Integer> tmpEntry = entryIt.next();
String word = tmpEntry.getKey();
Integer num = tmpEntry.getValue();
System.out.println(word+"重复次数:::"+num);
}
}
/**
* 把String转为只包含单词的数组(过滤掉空格和标点符号)
* @param str
* @return Queue<String>
*/
public static Queue<String> toQueue(String str){
String regex = "\\,|\\.";
str = str.replaceAll(regex, " ");//把逗号点号转为空格
System.out.println(str);
String regex2 = "\\ +";
str = str.replaceAll(regex2, " ");//把多个空格转为1个空格
System.out.println(str);
String[] words = str.split(" ");//转为单词数组
System.out.println(Arrays.toString(words));
//数组转为队列queue
Queue<String> queue = new LinkedList<String>();
for(String word : words){
queue.offer(word);
}
return queue;
}
}
统计一句话中重复字符、单词的个数,HashMap,Queue List
最新推荐文章于 2022-06-16 15:13:18 发布