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

旨意: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. RabittMQ实践(二): RabbitMQ 与spring、springmvc框架集成

    一.RabbitMQ简介 1.1.rabbitMQ的优点(适用范围)1. 基于erlang语言开发具有高可用高并发的优点,适合集群服务器.2. 健壮.稳定.易用.跨平台.支持多种语言.文档齐全.3. ...

  2. 20155333 2016-2017-2 《Java程序设计》第七周学习总结

    20155333 2016-2017-2 <Java程序设计>第七周学习总结 教材学习内容总结 Lambda 教材的引入循序渐近.深入浅出 Lambda去重复,回忆DRY原则 Lambda ...

  3. HIVE: 自定义TextInputFormat (旧版MapReduceAPI ok, 新版MapReduceAPI实现有BUG?)

    我们的输入文件 hello0, 内容如下: xiaowang 28 shanghai@_@zhangsan 38 beijing@_@someone 100 unknown 逻辑上有3条记录, 它们以 ...

  4. numpy模块学习笔记

    # encoding=utf-8 import numpy as np from numpy.linalg import * def main(): # 1.最基本的array lst = [[1, ...

  5. RC1意思

    软件各种版本的表示 alpha 内部测试版 beta 外部测试版 demo 演示版 Enhance 增强版或者加强版 属于正式版 Free 自由版 Full version 完全版 属于正式版 sha ...

  6. Object-C开发之instancetype和id关键字

    一.什么是instancetypeinstancetype是clang 3.5开始,clang提供的一个关键字,表示某个方法返回的未知类型的Objective-C对象.我们都知道未知类型的的对象可以用 ...

  7. 20145104张家明 《Java程序设计》第5周学习总结

    20145104张家明 <Java程序设计>第5周学习总结 教材学习内容总结 第八章(概括为一下内容) 1.如果父类异常对象在子类异常前被捕捉,则catch子类异常对象的区块将永远不会被执 ...

  8. 20145326 《Java程序设计》课程总结

    每周读书笔记链接汇总 20145326第1周学习总结 20145326第2周学习总结 20145326第3周学习总结 20145326第4周学习总结 20145326第5周学习总结 20145326第 ...

  9. kafka生产者和消费者流程

    前言 根据源码分析kafka java客户端的生产者和消费者的流程. 基于zookeeper的旧消费者 kafka消费者从消费数据到关闭经历的流程. 由于3个核心线程 基于zookeeper的连接器监 ...

  10. 51Nod 1070 Bash游戏 V4(斐波那契博弈)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1070 题意: 思路: 这个是斐波那契博弈,http://blog.csd ...