採用Map容器 統計每個單詞出現的次數
阿新 • • 發佈:2019-01-25
package cn.bjsxt.map; import java.util.HashMap; import java.util.Map; import java.util.Set; /** * this is a cat and that is a mice and where is the food? * 統計每個單詞出現的次數 * * 儲存到Map中 * key :String * value:自定義型別 * * "分揀" 思路 * 1、為所有key建立容器 * 之後容器中存放對應value * 2、第一次建立容器,並存放值value * 第二次之後,直接使用容器存放值 * @author Administrator * */ public class Demo01 { /** * @param args */ public static void main(String[] args) { String str ="this is a cat and that is a mice and where is the food"; //分割字串 String[] strArray=str.split(" "); //儲存到Map中 Map<String,Letter> letters = new HashMap<String,Letter>(); for(String temp:strArray){ /* //1、為所有key建立容器 if(!letters.containsKey(temp)){ Letter col = new Letter(); col.setCount(1); //第一次值存放容器中 letters.put(temp, col);//組成鍵值對 }else{ //2、 第二次之後,直接使用容器存放值 Letter col =letters.get(temp); //直接使用容器 col.setCount(col.getCount()+1); }*/ Letter col = null; if(null==(col=letters.get(temp))){ col = new Letter(); col.setCount(1); //第一次值存放容器中 letters.put(temp, col); }else{ //2、 第二次之後,直接使用容器存放值 col.setCount(col.getCount()+1); } } //輸出Map的值 Set<String> keys = letters.keySet(); for(String key:keys){ Letter col =letters.get(key); System.out.println("字母:"+key+",次數"+col.getCount()); } } public static void test1(){ String str ="this is a cat and that is a mice and where is the food"; //分割字串 String[] strArray=str.split(" "); //儲存到Map中 Map<String,Letter> letters = new HashMap<String,Letter>(); /* for(String temp:strArray){ //1、為所有key建立容器 之後容器中存放對應value if(!letters.containsKey(temp)){ letters.put(temp, new Letter()); } } for(String temp:strArray){ // 容器中存放對應value Letter col =letters.get(temp); //直接使用容器 col.setCount(col.getCount()+1); } */ for(String temp:strArray){ //1、為所有key建立容器 if(!letters.containsKey(temp)){ letters.put(temp, new Letter()); } //2、 之後容器中存放對應value Letter col =letters.get(temp); //直接使用容器 col.setCount(col.getCount()+1); } //輸出Map的值 Set<String> keys = letters.keySet(); for(String key:keys){ Letter col =letters.get(key); System.out.println("字母:"+key+",次數"+col.getCount()); } } }
package cn.bjsxt.map; public class Letter { private String name; private int count; public Letter() { // TODO Auto-generated constructor stub } public Letter(String name, int count) { super(); this.name = name; this.count = count; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } }
例項2
package cn.bjsxt.map; import java.util.ArrayList; import java.util.List; /** * 班級 * @author Administrator * */ public class ClassRoom { private String no; private List<Student> stus; //學生列表 private double total; //總分 public ClassRoom() { stus = new ArrayList<Student>(); } public ClassRoom(String no) { this(); //構造器沒有相互呼叫 該構造器未初始化 this.no = no; } public String getNo() { return no; } public void setNo(String no) { this.no = no; } public List<Student> getStus() { return stus; } public void setStus(List<Student> stus) { this.stus = stus; } public double getTotal() { return total; } public void setTotal(double total) { this.total = total; }
package cn.bjsxt.map;
public class Student {
private String name;
private String no;
private double score;
public Student() {
}
public Student(String name, String no, double score) {
super();
this.name = name;
this.no = no;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public String toString() {
return "Student [name=" + name + ", no=" + no + ", score=" + score
+ "]";
}
}
package cn.bjsxt.map;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* 定義一個Student類,屬性:name 姓名,classNumber 班號,score 成績
現在將若干Student物件放入List,請統計出每個班級的總分和平均分,分別打印出來
以面向物件的思維解決
* @author Administrator
*
*/
public class MapDemo03 {
public static void main(String[] args) {
List<Student> list = new ArrayList<Student>();
exam(list);
//統計
Map<String,ClassRoom> rooms = new HashMap<String,ClassRoom>();
count(rooms,list);
//列印
printScore(rooms);
}
/**
* 列印 總分與平均分
*/
public static void printScore(Map<String,ClassRoom> rooms){
Set<Map.Entry<String,ClassRoom>> entrySet =rooms.entrySet();
Iterator<Map.Entry<String,ClassRoom>> it =entrySet.iterator();
while(it.hasNext()){
Map.Entry<String,ClassRoom> entry =it.next();
ClassRoom room = entry.getValue();
double avg = room.getTotal()/room.getStus().size();
System.out.println("班號為:"+room.getNo()+",總分"+room.getTotal()+",平均分"+avg);
}
}
/**
* 統計分數
*/
public static void count(Map<String,ClassRoom> rooms,List<Student> list){
for(Student stu:list){
String no = stu.getNo();
double score = stu.getScore();
//根據班級編號 檢視 Map是否存在該班級 分揀思路
ClassRoom room = rooms.get(no);
if(null==room){ //第一次
room = new ClassRoom(no);
rooms.put(no, room);
}
//儲存 總分
room.setTotal(room.getTotal()+score);
room.getStus().add(stu); //加入學生
}
}
/**
* 現在將若干Student物件放入List
* @param list
*/
public static void exam(List<Student> list){
list.add(new Student("a","001",80));
list.add(new Student("b","001",80));
list.add(new Student("a","002",80));
list.add(new Student("c","003",80));
list.add(new Student("d","003",80));
}
}