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

旨意: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. 2016-2017 ACM-ICPC Pacific Northwest Regional Contest (Div. 1) Solution

    A:Alphabet Solved. 签. #include<bits/stdc++.h> using namespace std; ]; ]; int main(){ scanf(); ...

  2. SQLServer中char、varchar、nchar、nvarchar比较

    转自:http://www.cnblogs.com/bluesky_blog/archive/2009/07/31/1535722.html 对于程序中的string型字段,SQLServer中有ch ...

  3. map.containsKey

    该方法判断Map集合对象中是否包含指定的键名.如果Map集合中包含指定的键名,则返回true,否则返回false. 语法  containsKey(Object key) . e.g public s ...

  4. c++第二十三天

    p124~p126: 算数运算符 1.形如+ -(一元) * / % + -(二元)为算数运算符. 2.所有算数运算符都满足左结合律. 3.算数运算符的运算对象和求值结果都是右值. 4.在表达式求值之 ...

  5. 20145221 《Java程序设计》实验报告一:Java开发环境的熟悉(Windows+IDEA)

    20145221 <Java程序设计>实验报告一:Java开发环境的熟悉(Windows+IDEA) 实验要求 使用JDK编译.运行简单的Java程序: 使用IDEA 编辑.编译.运行.调 ...

  6. ExtJS清除表格缓存

    背景 在使用ExtJS时遇到不少坑,如果不影响使用也无所谓,但是有些不能忍的,比如表格数据缓存问题.如果第一次打开页面查询出一些数据展示在表格中:第二次打开,即使不查询也会有数据,这是缓存的数据. 我 ...

  7. tar: Cowardly refusing to create an empty archive 问题

    在解压 .tar.gz文件的时候遇到了这个解压的问题. 原命令:tar -zcvf ···.tar.gz 提示:tar: Cowardly refusing to create an empty ar ...

  8. R语言包相关命令

    R的包(package)通常有两种:1 binary package:这种包属于即得即用型(ready-to-use),但是依赖与平台,即Win和Linux平台下不同.2 Source package ...

  9. UOJ #131 【NOI2015】 品酒大会

    题目链接:品酒大会 学了后缀自动机之后再来写这道题就轻松多了…… 首先,题面中的两杯酒\(r\)相似就是这两个后缀的最长公共前缀大于等于\(r\).把串翻转过来之后就变成了两个前缀的最长公共后缀……然 ...

  10. 51nod 1225 余数的和 数学

    1225 余数之和 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题  收藏  关注 F(n) = (n % 1) + (n % 2) + (n % 3) + ... ...