首页 技术 正文
技术 2022年11月15日
0 收藏 923 点赞 4,198 浏览 8261 个字

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

旨意: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()); }
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,105
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,582
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,429
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,836
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,919