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. 【开发实例】C#调用SAPI实现语音合成的两种方法

    我们都知道现在的语音合成TTS是可以通过微软的SAPI实现的,好处我就不多说了,方便而已,因为在微软的操作系统里面就自带了这个玩意,主要的方式有两种:  1.使用COM组件技术,不管是C++,C#,D ...

  2. [AngularJS] Best Practise - Controller

    ControllerAs: Use thecontrollerAs syntax always as it aids in nested scoping and controller instance ...

  3. Codeforces Round #250 (Div. 2)——The Child and Set

    题目链接 题意: 给定goal和limit,求1-limit中的若干个数,每一个数最多出现一次,且这些数的lowbit()值之和等于goal,假设存在这种一些数,输出个数和每一个数:否则-1 分析: ...

  4. Linux内存管理学习笔记 转

    https://yq.aliyun.com/articles/11192?spm=0.0.0.0.hq1MsD 随着要维护的服务器增多,遇到的各种稀奇古怪的问题也会增多,要想彻底解决这些“小”问题往往 ...

  5. 动漫网站基于jquery的横向手风琴特效

    今天给大家分享一款动漫网站基于jquery的横向手风琴特效.这款手风琴特效适用浏览器:IE8.360.FireFox.Chrome.Safari.Opera.傲游.搜狗.世界之窗.效果图如下: 在线预 ...

  6. 项目源码--Android聚合视频类播放器

    下载源码 技术要点:  1.高效支持主流的视音频格式 2.本地视频的播放与管理 3.聚合电视在线直播 4.聚合优酷.搜狐.乐视.爱奇艺等多种在线视频 5.优质播放,包含播放.暂停,声音.亮度调整等功能 ...

  7. 嵌入式设备上运行AllJoyn注意事项

    1. 交叉编译AllJoyn库.编译成功后的文件位于:alljoyn-3.3.0-src\build\linux\arm\debug\dist\目录下: 2. 程序要使用AllJoyn,必须要启动al ...

  8. Linux 下configure 参数配置与软件的安装与卸载

    Linux环境下的软件安装,并不是一件容易的事情:如果通过源代码编译后在安装,当然事情就更为复杂一些:现在安装各种软件的教程都非常普遍:但万变不离其中,对基础知识的扎实掌握,安装各种软件的问题就迎刃而 ...

  9. C++-copy constructor、copy-assignment operator、destructor

    本文由@呆代待殆原创,转载请注明出处. 对于一个类来说,我们把copy constructor.copy-assignment operator.move constructor.move-assig ...

  10. foundation框架之反射机制

    概念 例子 一.概念 反射:根据字符串实例话对象或者调用方法 // // main.m // 反射机制 // // Created by apple on 14-3-28. // Copyright ...