采用Map容器 统计每个单词出现的次数
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 :St...
·
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));
}
}
更多推荐
已为社区贡献1条内容
所有评论(0)