题目:给定一个字符串,求出字符串中每一个单词在字符串中出现的次数

旨意:map的分拣思想。

每一个key的包装类,存放出现的次数

 /**
* 作为包装类,用来存放英文单词,和该英文单词出现的次数
* @ClassName: Str
* @Description: TODO(这里用一句话描述这个类的作用)
* @author 尚晓飞
* @date 2014-7-30 下午6:57:29
*
*/
public class Str {
private String st;
private int count;
public Str() {
super();
}
public String getSt() {
return st;
}
public void setSt(String st) {
this.st = st;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
} }

第一种分拣思想:(1)先为key创建对应的容器(2)使用容器,存放key对应的值

 /**
* 字符串:this is a cat and that is a nice and where is the food
* 将该字符串的每个单词出现的次数统计出来
* 【分拣的思想】
* 第一种:为所有key创建容器
* 之后存放对应的value
* 第二种:第一次创建容器,并存放value
* 第二次之后,直接使用容器存放value
* @ClassName: TestMap
* @Description: TODO(这里用一句话描述这个类的作用)
* @author 尚晓飞
* @date 2014-7-30 下午6:58:16
*
*/
public class TestMap { public static void main(String[] args) {
test4(); } //第一种分拣思路 (1)先为所有的key创建对应的容器(2)为对应key的容器中存放值
public static void test1(){
String sts="this is a cat and that is a nice and where is the food";
//将字符串分割成一个个单词,并存放入数组中
String[] strings=sts.split(" ");
//创建一个map对象,用来存放单词和单词出现的次数
Map<String, Str> countMap=new HashMap<String, Str>();
//第一种分拣思想
//第一步:为所有的key创建容器,
for(int i=0;i<strings.length;i++){
String temp=strings[i];
//判断map是否含有此key,如果有返回true,否则返回false
//第一次为所有的key创建容器
if(!countMap.containsKey(temp)){
Str str=new Str();
countMap.put(temp, str);
}
} //第二步:使用容器,存放值
for(String temp:strings){
Str clsStr=countMap.get(temp);
clsStr.setCount(clsStr.getCount()+1);
clsStr.setSt(temp);
} //测试countMap是否算是成功达到目的
Set<String> keys=countMap.keySet();
for (String key:keys) {
Str sd=countMap.get(key);
Integer cInteger=sd.getCount();
System.out.println("字母:"+key+"--次数:"+cInteger);
} }
//第一种分拣思路 (1)先为所有的key创建对应的容器(2)为对应key的容器中存放值
public static void test2(){
String sts="this is a cat and that is a nice and where is the food";
//将字符串分割成一个个单词,并存放入数组中
String[] strings=sts.split(" ");
//创建一个map对象,用来存放单词和单词出现的次数
Map<String, Str> countMap=new HashMap<String, Str>();
//第一种分拣思想
//第一步:为key创建容器的同时,并存放值
for(int i=0;i<strings.length;i++){
String temp=strings[i];
//判断map是否含有此key,如果有返回true,否则返回false
//先创建容器,之后为容器存放值
if(!countMap.containsKey(temp)){
Str str=new Str();
countMap.put(temp, str);
}
//使用容器存放值
Str str=countMap.get(temp);
str.setCount(str.getCount()+1); } //测试countMap是否算是成功达到目的
Set<String> keys=countMap.keySet();
for (String key:keys) {
Str sd=countMap.get(key);
Integer cInteger=sd.getCount();
System.out.println("字母:"+key+"--次数:"+cInteger);
}
} }

第二种分拣思想:(1)第一次为key创建容器,并存key对应的值(2)第二次使用创建好的容器,存放key对应的值

  * 【分拣的思想】
* 第一种:为所有key创建容器
* 之后存放对应的value
* 第二种:第一次创建容器,并存放value
* 第二次之后,直接使用容器存放value
* @ClassName: TestMap
* @Description: TODO(这里用一句话描述这个类的作用)
* @author 尚晓飞
* @date 2014-7-30 下午6:58:16
*
*/
public class TestMap { public static void main(String[] args) {
test4(); } //分拣第二种思想 (1)第一次为key创建容器,并存放值(2)第二次使用容器存放值
public static void test3(){
String sts="this is a cat and that is a nice and where is the food";
//将字符串分割成一个个单词,并存放入数组中
String[] strings=sts.split(" ");
//创建一个map对象,用来存放单词和单词出现的次数
Map<String, Str> countMap=new HashMap<String, Str>();
//第一种分拣思想
//第一步:为key创建容器的同时,并存放值
for(int i=0;i<strings.length;i++){
String temp=strings[i];
//判断map是否含有此key,如果有返回true,否则返回false
//第一次创建容器,并为容器中存放值
if(!countMap.containsKey(temp)){
Str str=new Str();
str.setCount(1);
countMap.put(temp, str);
}else{
//第二次使用容器存放值
Str str=countMap.get(temp);
str.setCount(str.getCount()+1);
}
} //测试countMap是否算是成功达到目的
Set<String> keys=countMap.keySet();
for (String key:keys) {
Str sd=countMap.get(key);
Integer cInteger=sd.getCount();
System.out.println("字母:"+key+"--次数:"+cInteger);
}
} //第二种分拣思路:(1)第一次为key创建容器,并存放值(2)第二次使用容器存放值
public static void test4(){
String sts="this is a cat and that is a nice and where is the food";
//将字符串分割成一个个单词,并存放入数组中
String[] strings=sts.split(" ");
//创建一个map对象,用来存放单词和单词出现的次数
Map<String, Str> countMap=new HashMap<String, Str>();
//第一种分拣思想
//第一步:为key创建容器的同时,并存放值
for(int i=0;i<strings.length;i++){
String temp=strings[i];
//判断map是否含有此key,如果有返回true,否则返回false
//第一次创建容器,并为容器中存放值
Str str=null;
if(null==(str=countMap.get(temp))){
str=new Str();
str.setCount(1);
countMap.put(temp, str);
}else{
//第二次使用容器存放值
str=countMap.get(temp);
str.setCount(str.getCount()+1);
}
} //测试countMap是否算是成功达到目的
Set<String> keys=countMap.keySet();
for (String key:keys) {
Str sd=countMap.get(key);
Integer cInteger=sd.getCount();
System.out.println("字母:"+key+"--次数:"+cInteger);
}
}
}

分拣思想的应用:

 需求:查询出学生List集合,对学生集合进行加工,将学生按照班级分类,并求出班级的总分和平均分

 思路:map分拣思想。需要创建一个班级po,班级po里存放学生信息,该班集的总分,班级号码。

Student的po

 /**
* 学生对象
* @ClassName: Student
* @Description: TODO(这里用一句话描述这个类的作用)
* @author 尚晓飞
* @date 2014-7-31 下午6:16:39
*
*/
public class Student {
private String name;//姓名
private String no;//班级
private Integer score;//分数 public Student() {
super();
} public Student(String name, String no, Integer 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 Integer getScore() {
return score;
}
public void setScore(Integer score) {
this.score = score;
} }

需要进行存放学生信息的ClassRoom的po

 package com.bjsxt.xiaofei;

 import java.util.HashSet;
import java.util.Set; public class ClassRoom {
private String no;//班级号码
private Set<Student> students;//班级里的学生
private Integer countScore;//总分 public ClassRoom(){
students=new HashSet<Student>();
} public ClassRoom(String no, Integer countScore) {
this();
this.no = no;
this.countScore = countScore;
} public String getNo() {
return no;
} public void setNo(String no) {
this.no = no;
} public Set<Student> getStudents() {
return students;
} public void setStudents(Set<Student> students) {
this.students = students;
} public Integer getCountScore() {
return countScore;
} public void setCountScore(Integer countScore) {
this.countScore = countScore;
} }

加工学生list集合,并进行测试

package com.bjsxt.xiaofei;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set; /**
* 从学生表里查询出一个学生集合,现在求出每个班级的成绩总分,和平均分。
* 利用Map的分拣思想,做到。
* @ClassName: Test2
* @Description: TODO(这里用一句话描述这个类的作用)
* @author 尚晓飞
* @date 2014-8-1 上午8:47:24
*
*/
public class Test2 {
public static void main(String[] args) {
//获取学生集合
List<Student> studentList=queryAll();
//加工学生集合。返回一个map.map里装的是key:【班级号】 value:【classRoom】
Map<String,ClassRoom> classMap=processStList(studentList);
//测试Map
testMap(classMap);
} //获得学生集合
public static List<Student> queryAll(){
List<Student> list=new ArrayList<Student>();
list.add(new Student("a", "一班", 80));
list.add(new Student("b", "二班", 100));
list.add(new Student("c", "三班", 60));
list.add(new Student("d", "一班", 80));
list.add(new Student("e", "二班", 100));
list.add(new Student("f", "三班", 60));
return list;
} //加工学生集合,返回Map
public static Map<String, ClassRoom> processStList(List<Student> studentList){
//生成一个map
Map<String, ClassRoom> classMap=new HashMap<String, ClassRoom>(); //遍历学生集合
for(Student st:studentList){
//获取当前学生的班级号码,和成绩
String classNum=st.getNo();
Integer score=st.getScore();
//如果map中不含该学生的班级号,则为该学生创建新班级对象,并将该学生信息存入其中
if(!classMap.containsKey(classNum)){
//创建班级
ClassRoom cls=new ClassRoom();
//将班级号和班级作为映射关系,存放入classMap
classMap.put(classNum, cls);
//将当前此学生的信息存入班级中
cls.setCountScore(score);
Set<Student> set=cls.getStudents();
set.add(st); }else{
//通过存在的班级号,往里存放当前学生
ClassRoom cls=classMap.get(classNum);
cls.setCountScore(cls.getCountScore()+score);
Set<Student> set=cls.getStudents();
set.add(st);
} } return classMap;
} public static void testMap(Map<String, ClassRoom> classMap){
//遍历map
Set<String> set=classMap.keySet();
//遍历set中的map键
for (String key : set) {
//班级
ClassRoom cls=classMap.get(key);
//打印要求的信息
System.out.println("班级号码:"+key+" 班级总分:"+cls.getCountScore()+" 班级平均分"+cls.getCountScore()/cls.getStudents().size());
} }
}

 第二种测试里,展现了另一种map的遍历方法和set集合的三种遍历方法

 public static void testMap(Map<String, ClassRoom> classMap){
//第二种遍历Map。将Map装换成set集合,set集合里的每一个对象是map的映射关系新组成的一个Map.Entry的对象。通过getkey() getvalue()方法获取KEY-VALUE的映射
Set<Map.Entry<String,ClassRoom>> setEntry=classMap.entrySet(); //第一种遍历set集合 将set集合转换成数组
Object[] objects=setEntry.toArray();
for(int i=0;i<objects.length;i++){
Map.Entry<String, ClassRoom> entyr=(Entry<String, ClassRoom>) objects[i];
String classNum=entyr.getKey();
ClassRoom cls=entyr.getValue();
System.out.println("普通for班级号码:"+classNum+"----班级总分:"+cls.getCountScore()+"----班级平均分"+cls.getCountScore()/cls.getStudents().size()); } //第二种遍历set集合 增强for循环
for(Map.Entry<String, ClassRoom> entry:setEntry){ String classNum=entry.getKey();
ClassRoom cls=entry.getValue();
System.out.println("班级号码:"+classNum+"----班级总分:"+cls.getCountScore()+"----班级平均分"+cls.getCountScore()/cls.getStudents().size());
} //第三种遍历set集合:利用迭代器遍历Set集合
Iterator<Map.Entry<String, ClassRoom>> iterator=setEntry.iterator();
while (iterator.hasNext()) {
Map.Entry<String, ClassRoom> entry=iterator.next();
String classNum=entry.getKey();
ClassRoom cls=entry.getValue();
System.out.println("班级号码:"+classNum+"----班级总分:"+cls.getCountScore()+"----班级平均分"+cls.getCountScore()/cls.getStudents().size()); }
}

Java编程之Map中分拣思想。的更多相关文章

  1. Swift函数编程之Map、Filter、Reduce

    在Swift语言中使用Map.Filter.Reduce对Array.Dictionary等集合类型(collection type)进行操作可能对一部分人来说还不是那么的习惯.对于没有接触过函数式编 ...

  2. Python函数式编程之map()

    Python函数式编程之map() Python中map().filter().reduce()这三个都是应用于序列的内置函数. 格式: map(func, seq1[, seq2,…]) 第一个参数 ...

  3. 怎么在java 8的map中使用stream

    怎么在java 8的map中使用stream 简介 Map是java中非常常用的一个集合类型,我们通常也需要去遍历Map去获取某些值,java 8引入了Stream的概念,那么我们怎么在Map中使用S ...

  4. MapReduce编程之Map Join多种应用场景与使用

    Map Join 实现方式一:分布式缓存 ● 使用场景:一张表十分小.一张表很大. ● 用法: 在提交作业的时候先将小表文件放到该作业的DistributedCache中,然后从DistributeC ...

  5. java编程之:Unsafe类

    Unsafe类在jdk 源码的多个类中用到,这个类的提供了一些绕开JVM的更底层功能,基于它的实现可以提高效率.但是,它是一把双刃剑:正如它的名字所预示的那样,它是 Unsafe的,它所分配的内存需要 ...

  6. java 线程之concurrent中的常用工具 CyclicBarrier

    一.CyclicBarrier CyclicBarrier是一个同步辅助类,它允许一组线程互相等待,直到到达某个公共屏障点 (common barrier point).在涉及一组固定大小的线程的程序 ...

  7. Java 将容器 Map中的内容保存到数组

    import java.util.Map; import java.util.HashMap; import java.util.Map.Entry; public class mapToArr { ...

  8. java编程之POI读取excel表格的内容

    07版本的excel需要另外加一个jar包.xbean.jar的jar包 读取代码模板.利用模板介绍读取excel的一些poi的api这是重点 /** * 读取excel文件 * @Title: re ...

  9. java编程之JDBC

    JDBC的常用类和接口 1.       DriverManager类 管理数据库中的所有驱动程序,其所有的方法都是静态方法,调用时无需实例化,通过类名就可以直接调用. 2.       Connec ...

随机推荐

  1. Polya

    using namespace std; typedef long long LL; const int MAXN = 1e3 +10; const LL MOD = (LL)1 << 6 ...

  2. TED #02#

    Amanda Palmer: The art of asking 1. I think people have been obsessed with the wrong question, which ...

  3. linux及安全第五周总结——20135227黄晓妍

    (注意:本文总结备份中有较多我手写笔记的图片,其中重要的部分打出来了.本文对分析system_call对应的汇编代码的工作过程,系统调用处理过程”的理解,以及流程图都写在实验部分.) 实验部分 使用g ...

  4. 20145311王亦徐 实验三 "敏捷开发与XP实践"

    20145311王亦徐 实验三 "敏捷开发与XP实践"程序设计过程 实验内容 使用 git 上传代码 使用 git 相互更改代码 实现代码的重载 git 上传代码 查看代码是否有修 ...

  5. SQL优化:清理生产环境中已失效字段基本步骤

    1.统计相应字段的数据情况(如:几年没更新,无数据等情况) 2.确认产品逻辑已无效(产品经理邮件确认) 3.数据备份 4.将数据清空(置为0或空) 5.测试环境中删除引用页面 6.修改定时程序,存储过 ...

  6. BZOJ 1853: [Scoi2010]幸运数字(容斥原理)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1853 题意: 在中国,很多人都把6和8视为是幸运数字!lxhgww也这样认为,于是他定义自己的“幸运 ...

  7. javascript 时间与时间戳的转换

    一:时间转时间戳:javascript获得时间戳的方法有五种,都是通过实例化时间对象 new Date() 来进一步获取当前的时间戳 1.var timestamp1 = Date.parse(new ...

  8. UVA-11167 Monkeys in the Emei Mountain(区间模型最大流+输出方案)

    题目大意:有n只猴子,每只猴子都有一组参数(v,a,b),表示这只猴子在时间段[a,b]之间必须要喝v个单位水,并且每个时间单位只能和一个单位水,每次至少喝一个单位.但是只有一个水池,并且这个水池最多 ...

  9. vim中将tab 设置成4个空格

    在.vimrc中添加以下代码后,重启vim即可实现按TAB产生4个空格:set ts=4  (注:ts是tabstop的缩写,设TAB宽4个空格)set expandtab 对于已保存的文件,可以使用 ...

  10. 快速切题 poj 2996 Help Me with the Game 棋盘 模拟 暴力 难度:0

    Help Me with the Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3510   Accepted:  ...