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经典储存 分拣存储与面向对象组合的更多相关文章

  1. 集合类学习之HashMap

    一.HashMap概述 HashMap基于哈希表的 Map 接口的实现.此实现提供所有可选的映射操作,并允许使用 null 值和 null 键.(除了不同步和允许使用 null 之外,HashMap  ...

  2. 集合类学习之Hashmap机制研究

    1.遍历的两种实现方法 //新建 Map map=new HashMap(); //存储值 map.put() ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 //遍历方式 ...

  3. java集合类学习心得

    java集合类学习心得 看了java从入门到精通的第十章,做个总结,图片均取自网络. 常用集合的继承关系 Linked 改快读慢 Array 读快改慢 Hash 两都之间 Collection是集合接 ...

  4. Java中分拣存储的demo

      //Letter.java package yzhou.map; /** * * @author 洋 * */ public class Letter { private String name; ...

  5. 学习.Net的经典网站

    学习.Net的经典网站 收藏 还不错推荐给大家 原文-- 名称:快速入门 地址:http://chs.gotdotnet.com/quickstart/ 描述:本站点是微软.NET技术的快速入门网站, ...

  6. ElasticSearch 学习记录之 分布式文档存储往ES中存数据和取数据的原理

    分布式文档存储 ES分布式特性 屏蔽了分布式系统的复杂性 集群内的原理 垂直扩容和水平扩容 真正的扩容能力是来自于水平扩容–为集群添加更多的节点,并且将负载压力和稳定性分散到这些节点中 ES集群特点 ...

  7. 重新学习MySQL数据库3:Mysql存储引擎与数据存储原理

    重新学习Mysql数据库3:Mysql存储引擎与数据存储原理 数据库的定义 很多开发者在最开始时其实都对数据库有一个比较模糊的认识,觉得数据库就是一堆数据的集合,但是实际却比这复杂的多,数据库领域中有 ...

  8. java集合类学习笔记之HashMap

    1.简述 HashMap是java语言中非常典型的数据结构,也是我们平常用的最多的的集合类之一.它的底层是通过一个单向链表(Node<k,v>)数组(也称之为桶bucket,数组的长度也叫 ...

  9. java集合类学习笔记之LinkedHashMap

    1.简述 LinkedHashMap是HashMap的子类,他们最大的不同是,HashMap内部维护的是一个单向的链表数组,而LinkedHashMap内部维护的是一个双向的链表数组.HashMap是 ...

随机推荐

  1. CDOJ 第七届ACM趣味程序设计竞赛第三场(正式赛) 题解

    宝贵资源 题目连接: http://acm.uestc.edu.cn/#/problem/show/1265 题意 平面上给n个点(n<=1000),要求找一个面积最小的正方形,将所有的点都囊括 ...

  2. 浅谈Androidclient项目框架

    写Android也有些时间了,一边工作,一边学习,一边积累.仅仅有遇到问题了,花时间去研究,自己的能力才干提升.刀假设不用.慢慢的就会生锈应该也是这个道理吧!上个月公司项目server框架进行的一些调 ...

  3. 5.ScrollView无法填充满屏幕

    问题: <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.D ...

  4. 仿Android网易新闻客户端,并增加水平图片滑动,改进阅读体验

    仿网易新闻Android端APP 主要功能展示和代码实现 差不多花了一周的时间,目前实现的了新闻下的包括头条.体育.娱乐的一系列的新闻展示,以及点击后进入的新闻详情展示. 目前效果 目前效果请访问该网 ...

  5. iOS开发——UI高级OC篇&自定义控件之调整按钮中子控件(图片和文字)的位置

    自定义控件之调整按钮中子控件(图片和文字)的位置 其实还有一种是在storyBoard中实现的,只需要设置对应空间的左右间距: 这里实现前面两种自定义的方式 一:imageRectForContent ...

  6. ORCLE INNODB 博客与 innodb_lru_scan_depth

    https://blogs.oracle.com/mysqlinnodb/ http://mysqllover.com/?p=485 •MySQL. MySQL 5.6.10 http://www.m ...

  7. javascript 的位操作符转换推断

    var a = "10" | 0; alert(a); alert (typeof a); 结果为10,number. 这就是说这条语句可以将字符串转化为number. 如果: v ...

  8. centos复制到另外一台电脑连不上网

    http://snow-berry.iteye.com/blog/1991754 从一台电脑virtual box克隆出来的centos.vdi复制到另外一台电脑,找不到网卡,提示Device eth ...

  9. 关于 Android项目“error: Apostrophe not preceded by \ (”的解决方法

    用Eclipse环境开发Android项目,如果编译时控制台报出“error: Apostrophe not preceded by \ (”这种错误,那么多半是因为项目中的一个strings.xml ...

  10. LeetCode11 Container With Most Water

    题意: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...