集合类学习之HashMap经典储存 分拣存储与面向对象组合
HashMap:键值对(key-value)
通过对象来对对象进行索引,用来索引的对象叫做key,其对应的对象叫做value.
默认是1:1关系(一对一)
存在则覆盖,当key已经存在,则利用新的value覆盖原有的value
例1:给定一个字符串,求出字符串中每一个单词在字符串中出现的次数
旨意:map的分拣思想。
第一种分拣思想:(1)先为key创建对应的容器(2)使用容器,存放key对应的值
/**
* 作为包装类,用来存放英文单词,和该英文单词出现的次数
* @author qjc
* @date 2016-3-9
*/
public class Str {
private String st;
private int count;
//....
} /**
* 字符串:this is a cat and that is a nice and where is the food
* 将该字符串的每个单词出现的次数统计出来
* 【分拣的思想】
* 第一种:为所有key创建容器
* 之后存放对应的value
* 第二种:第一次创建容器,并存放value
* 第二次之后,直接使用容器存放value
* @author qjc
* @date 2016-3-9
*
*/
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
* @author qjc
* @date 2016-3-9
*
*/
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);
}
}
}
例2:
分拣思想的应用
需求:查询出学生List集合,对学生集合进行加工,将学生按照班级分类,并求出班级的总分和平均分
思路:map分拣思想。需要创建一个班级po,班级po里存放学生信息,该班集的总分,班级号码。
/**
*思考:
* 定义一个Student类,属性:name 姓名,no班号,score 成绩
* 现在将若干Student对象放入List,请统计出每个班级的总分和平均分
*方案: 面向对象+分拣存储(1:N)
* @author qjc
* 2016-3-9
*/
public class MapDemo02 {
/**
* 模拟考试 测试数据到List
*/
public static List<Student> exam(){
List<Student> list = new ArrayList<>();
list.add(new Student("张三", "java", 84));
list.add(new Student("李四", "java", 88));
list.add(new Student("王五", "c++", 90));
list.add(new Student("赵六", "c++", 98));
return list;
}
/**
* 统计分析
* 1、面向对象
* 2、分拣存储
*/
public static Map<String, ClassRoom> count(List<Student> list){
Map<String, ClassRoom> map = new HashMap<String, ClassRoom>();
//1、遍历List
for(Student stu : list){
String no = stu.getNo(); //班级编号
double score = stu.getScore(); //成绩
//2、分拣 查看是否存在该编号的班级
//如果不存在,创建班级
ClassRoom room = map.get(no);
if(null==room){
room = new ClassRoom(no);
map.put(no, room);
}
//存在,放入学生
room.getStudents().add(stu);
//计算总分
room.setTotal(room.getTotal()+score);
}
return map;
}
/**
* 查看每个班级的总分和平均分 --->遍历map
*/
public static void view(Map<String, ClassRoom> map){
Set<String> keySet = map.keySet();
Iterator<String> iter = keySet.iterator();
while(iter.hasNext()){
String no = iter.next();
ClassRoom room = map.get(no);
//查看总分 计算平均分
double total = room.getTotal();
double avg = total/room.getStudents().size();
System.out.println(no+"---->"+total+"---->"+avg);
}
}
public static void main(String[] args) {
//1、考试
List<Student> list = exam();
//2、分析成绩
Map<String, ClassRoom> map = count(list);
//3、查看成绩(总分、平均分)
view(map);
}
} /**
* 一个班级多个学生(学生列表)
* @author qjc
* 2016-3-9
*/
public class ClassRoom {
private String no;//班级
private List<Student> students; //班级列表
private double total; //总分
public ClassRoom() {
students = new ArrayList<>();
}
public ClassRoom(String no) {
this();
this.no = no;
}
//…..
} public class Student {
private String name;
private String no; //班级
private double score; //成绩
//…..
}
集合类学习之HashMap经典储存 分拣存储与面向对象组合的更多相关文章
- 集合类学习之HashMap
一.HashMap概述 HashMap基于哈希表的 Map 接口的实现.此实现提供所有可选的映射操作,并允许使用 null 值和 null 键.(除了不同步和允许使用 null 之外,HashMap ...
- 集合类学习之Hashmap机制研究
1.遍历的两种实现方法 //新建 Map map=new HashMap(); //存储值 map.put() ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 //遍历方式 ...
- java集合类学习心得
java集合类学习心得 看了java从入门到精通的第十章,做个总结,图片均取自网络. 常用集合的继承关系 Linked 改快读慢 Array 读快改慢 Hash 两都之间 Collection是集合接 ...
- Java中分拣存储的demo
//Letter.java package yzhou.map; /** * * @author 洋 * */ public class Letter { private String name; ...
- 学习.Net的经典网站
学习.Net的经典网站 收藏 还不错推荐给大家 原文-- 名称:快速入门 地址:http://chs.gotdotnet.com/quickstart/ 描述:本站点是微软.NET技术的快速入门网站, ...
- ElasticSearch 学习记录之 分布式文档存储往ES中存数据和取数据的原理
分布式文档存储 ES分布式特性 屏蔽了分布式系统的复杂性 集群内的原理 垂直扩容和水平扩容 真正的扩容能力是来自于水平扩容–为集群添加更多的节点,并且将负载压力和稳定性分散到这些节点中 ES集群特点 ...
- 重新学习MySQL数据库3:Mysql存储引擎与数据存储原理
重新学习Mysql数据库3:Mysql存储引擎与数据存储原理 数据库的定义 很多开发者在最开始时其实都对数据库有一个比较模糊的认识,觉得数据库就是一堆数据的集合,但是实际却比这复杂的多,数据库领域中有 ...
- java集合类学习笔记之HashMap
1.简述 HashMap是java语言中非常典型的数据结构,也是我们平常用的最多的的集合类之一.它的底层是通过一个单向链表(Node<k,v>)数组(也称之为桶bucket,数组的长度也叫 ...
- java集合类学习笔记之LinkedHashMap
1.简述 LinkedHashMap是HashMap的子类,他们最大的不同是,HashMap内部维护的是一个单向的链表数组,而LinkedHashMap内部维护的是一个双向的链表数组.HashMap是 ...
随机推荐
- jquery Mobile点击显示加载等待效果
点击某个按钮或链接时,触发等待加载效果: <script> <!-- $(document).bind("mobileinit", function(){ }); ...
- jquery 之validate 笔记
默认分类 2010-04-04 20:35:01 阅读123 评论0 字号:大中小 jquery.validate是jquery旗下的一个验证框架,借助jquery的优势,我们可以迅速验证一些常见的输 ...
- 浅析C#基于TCP协议的SCOKET通信
TCP协议是一个基本的网络协议,基本上所有的网络服务都是基于TCP协议的,如HTTP,FTP等等,所以要了解网络编程就必须了解基于TCP协议的编程.然而TCP协议是一个庞杂的体系,要彻底的弄清楚它的实 ...
- maven项目编译漏掉src/main/java下的xml配置文件
在整合Spring + Mybatis框架的时候,自动扫描配置都已经配置好了. 配置如下: <?xml version="1.0" encoding="UTF-8& ...
- 即时通信(RPC)的Rtmp实现--配置篇
http://flexman.blog.sohu.com/129838570.html http://flexman.blog.sohu.com/130007574.html step 1: 首先要确 ...
- android安卓最新快捷环境搭建(转)
现在很多视频和文章上的安卓环境搭建还是比较老的,挺麻烦.现在写快速方便的搭建: 一.下载JDK: 网址:http://www.oracle.com/technetwork/java/javase/do ...
- percona-xtrabackup 文档
https://www.percona.com/doc/percona-xtrabackup/2.4/index.html
- MHA参数 转
http://blog.csdn.net/wulantian/article/details/12503473 http://blog.csdn.net/wulantian/article/categ ...
- BootStrap2学习日记15----选项卡
导航格式1: <ul class="nav nav-tabs"> <li class="active"><a href=" ...
- Android 高级UI设计笔记13:Gallery(画廊控件)之 循环显示图像
1. 循环显示图像的原理 循环显示有些类似于循环链表,最后一个结点的下一个结点又是第1个结点.循环显示图像也可以模拟这一点. 也许细心的读者从上一节实现的ImageAdapter类中会发现些什么.对 ...