Java编程之Map中分拣思想。
题目:给定一个字符串,求出字符串中每一个单词在字符串中出现的次数
旨意: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中分拣思想。的更多相关文章
- Swift函数编程之Map、Filter、Reduce
在Swift语言中使用Map.Filter.Reduce对Array.Dictionary等集合类型(collection type)进行操作可能对一部分人来说还不是那么的习惯.对于没有接触过函数式编 ...
- Python函数式编程之map()
Python函数式编程之map() Python中map().filter().reduce()这三个都是应用于序列的内置函数. 格式: map(func, seq1[, seq2,…]) 第一个参数 ...
- 怎么在java 8的map中使用stream
怎么在java 8的map中使用stream 简介 Map是java中非常常用的一个集合类型,我们通常也需要去遍历Map去获取某些值,java 8引入了Stream的概念,那么我们怎么在Map中使用S ...
- MapReduce编程之Map Join多种应用场景与使用
Map Join 实现方式一:分布式缓存 ● 使用场景:一张表十分小.一张表很大. ● 用法: 在提交作业的时候先将小表文件放到该作业的DistributedCache中,然后从DistributeC ...
- java编程之:Unsafe类
Unsafe类在jdk 源码的多个类中用到,这个类的提供了一些绕开JVM的更底层功能,基于它的实现可以提高效率.但是,它是一把双刃剑:正如它的名字所预示的那样,它是 Unsafe的,它所分配的内存需要 ...
- java 线程之concurrent中的常用工具 CyclicBarrier
一.CyclicBarrier CyclicBarrier是一个同步辅助类,它允许一组线程互相等待,直到到达某个公共屏障点 (common barrier point).在涉及一组固定大小的线程的程序 ...
- Java 将容器 Map中的内容保存到数组
import java.util.Map; import java.util.HashMap; import java.util.Map.Entry; public class mapToArr { ...
- java编程之POI读取excel表格的内容
07版本的excel需要另外加一个jar包.xbean.jar的jar包 读取代码模板.利用模板介绍读取excel的一些poi的api这是重点 /** * 读取excel文件 * @Title: re ...
- java编程之JDBC
JDBC的常用类和接口 1. DriverManager类 管理数据库中的所有驱动程序,其所有的方法都是静态方法,调用时无需实例化,通过类名就可以直接调用. 2. Connec ...
随机推荐
- android自定义Activity窗口大小(theme运用)
http://gundumw100.iteye.com/blog/906195 正常情况下,我们开发的应用程序都会上占满整个屏幕,那么怎么样才能开发出自定义窗口大小的的程序呢?如下图所示: 实现起来非 ...
- Zynq-Linux移植学习笔记之十-u-boot网络配置
在zynq开发板zc706上,网络通路由下面三个设备组成: 其中zynq负责对phy进行配置,当zynq上的网络控制器以及phy完成正确配置时,能够看到RJ45上面的黄灯亮,此时表明链路已经通了.如果 ...
- 如何使用AngularJS对表单提交内容进行验证
AngularJS是一款优秀的前端JS框架,已经被用于Google的多款产品当中.它有着诸多特性,最为核心的是:MVC.模块化.自动化双向数据绑定.语义化标签.依赖注入等……使用它可以大大减少书写代码 ...
- Python学习札记(二十六) 函数式编程7 修饰器
修饰器 NOTE 1.函数对象有一个__name__属性,可以拿到函数的名字: #!/usr/bin/env python3 def now(): print('2017/2/19') def mai ...
- Python学习札记(二十三) 函数式编程4 sorted
参考:sorted NOTE 1.sorted,快速排序,时间复杂度O(nlogn)渐进最优. #!/usr/bin/env python3 L = [] for i in range(10): L. ...
- GET 和 POST 方法的区别
GET 和 POST 是 HTTP 请求的两种基本方法,最直观的区别就是 GET 把参数包含在 URL 中,POST 通过 request body 传递参数. 一些标准的区别: 1. GET 在浏览 ...
- 服务器jupyter配置与ssh远程登录
jupyter 配置 首先安装jupyter,在anaconda套装中已包含,如果安装的是精简版的miniconda则通过conda install jupyter安装. 生成配置文件 jupyter ...
- Mac OS 下安装 Vagrant
Vagrant是一款用来构建虚拟开发环境的工具,它其实算是一个跨平台的虚拟机管理工具 1 安装 1.1 安装Vagrant 下载好pkg文件后,下一步安装即可 1.2 安装Virtualbox Vag ...
- bzoj-1009-dp+kmp处理转移矩阵幂
1009: [HNOI2008]GT考试 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 4723 Solved: 2940[Submit][Statu ...
- 【Matplotlib】线设置,坐标显示范围
改变线的颜色和线宽 参考文章: controlling line properties Line API 线有很多属性你可以设置:线宽,线型,抗锯齿等等:具体请参考matplotlib.lines.L ...