统计每个单词出现的次数
存储到Map中
key :String
value:自定义类型
分拣思路
1、为所有key创建容器 之后容器中存放对应value
2、第一次创建容器,并存放值value 第二次之后,直接使用容器存放值
package cn.bjsxt.map;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class CountWord {
public static void main(String[] args) {
String str ="hi man whats up i just want to fuck you o my god that is i want it ";
//分割字符串
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());
}
}
}
统计分数
定义一个Student类,属性:name姓名,classNumber班号,score成绩现在将若干Student对象放入List,请统计出每个班级的总分和平均分,分别打印出来
首先定义一个 ClassRoom班级类
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;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class CountStudentScores {
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));
}
}