統計一句話中重複字元、單詞的個數,HashMap,Queue List
阿新 • • 發佈:2019-02-13
//統計一句話中重複字元的個數(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;
}
}