【java】HashMap、Map、Set、HashMap.put()、HashMap.keySet()、HashMap.entrySet()、Map.Entry内部类
package com.tn.hashMap; public class Student {
private String id;
private String name;
public Student(String id, String name) {
super();
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}
Student
package com.tn.hashMap; import java.util.HashMap;
import java.util.Map;
import java.util.Set; public class HashMapDemo {
public static void main(String[] args){
Student student1=new Student("0001","哪吒");
Student student2=new Student("0002","杨戬");
Student student3=new Student("0003","雷震子");
HashMap<String,Student> hm=new HashMap<String,Student>();
//HashMap的键值对都是Object类型,如果需要用到基本类型可用Integer之类的包装类
hm.put(student1.getId(), student1);
hm.put(student2.getId(), student2);
hm.put(student3.getId(), student3);
//put方法如果key相同,则后面的value会覆盖前面的
System.out.println(hm.size());
System.out.println(hm); //通过keySet方法用Set遍历
Set<String> set=hm.keySet();
for(String str:set){
System.out.println(hm.get(str));//打印出来的顺序是不可预知的
} //通过entrySet方法用set遍历
/*放入HashMap集合中的key、Value会被包装成Map.Entry内部类的属性
* 一个键值对成为一个Map.Entry实例对象
*
*/
Set<Map.Entry<String, Student>> set1=hm.entrySet();
//Set<Map<K,V>.Entry<K, V>> 注:Map后的泛型可不指明,不用写。
for(Map.Entry<String, Student> mEntry:set1){
System.out.println(mEntry.getKey()+":"+mEntry.getValue());
}
}
}
HashMapDemo
通过HashMap计算字符串中各字母出现次数:
String str="abbcccddddeeeeeffffff";
HashMap<String,Integer> hashMap=new HashMap<String,Integer>();
for(int i=0;i<str.length();i++){
if(!hashMap.containsKey(str.substring(i, i+1))){
hashMap.put(str.substring(i, i+1), 1);
}else{
hashMap.put(str.substring(i,i+1), hashMap.get(str.substring(i, i+1))+1);
}
}
System.out.println(hashMap);
snippet
【java】HashMap、Map、Set、HashMap.put()、HashMap.keySet()、HashMap.entrySet()、Map.Entry内部类的更多相关文章
- map集合中value()、keySet()、entrySet()区别
在Map集合中 values():方法是获取集合中的所有的值----没有键,没有对应关系, KeySet():将Map中所有的键存入到set集合中.因为set具备迭代器.所有可以迭代方式取出所有的键, ...
- 牛客网Java刷题知识点之Map的两种取值方式keySet和entrySet、HashMap 、Hashtable、TreeMap、LinkedHashMap、ConcurrentHashMap 、WeakHashMap
不多说,直接上干货! 这篇我是从整体出发去写的. 牛客网Java刷题知识点之Java 集合框架的构成.集合框架中的迭代器Iterator.集合框架中的集合接口Collection(List和Set). ...
- 【java】TreeMap/HashMap的循环迭代中 keySet和entrySet和forEach方式 + map的几种迭代方式
参考链接:https://www.cnblogs.com/crazyacking/p/5573528.html ================================== java紫色代表迭 ...
- Java基础知识强化之集合框架笔记57:Map集合之HashMap集合(HashMap<Student,String>)的案例
1. HashMap集合(HashMap<Student,String>)的案例 HashMap<Student,String>键:Student 要求:如果两个对象 ...
- Java基础知识强化之集合框架笔记56:Map集合之HashMap集合(HashMap<String,Student>)的案例
1. HashMap集合(HashMap<String,Student>)的案例 HashMap是最常用的Map集合,它的键值对在存储时要根据键的哈希码来确定值放在哪里. HashMap的 ...
- 接口java.util.Map的四个实现类HashMap Hashtable LinkedHashMap TreeMap
java中HashMap,LinkedHashMap,TreeMap,HashTable的区别 :java为数据结构中的映射定义了一个接口java.util.Map;它有四个实现类,分别是HashMa ...
- Java集合源码学习(四)HashMap分析
ArrayList.LinkedList和HashMap的源码是一起看的,横向对比吧,感觉对这三种数据结构的理解加深了很多. >>数组.链表和哈希表结构 数据结构中有数组和链表来实现对数据 ...
- Java集合源代码剖析(二)【HashMap、Hashtable】
HashMap源代码剖析 ; // 最大容量(必须是2的幂且小于2的30次方.传入容量过大将被这个值替换) static final int MAXIMUM_CAPACITY = 1 << ...
- Java集合源码学习(四)HashMap
一.数组.链表和哈希表结构 数据结构中有数组和链表来实现对数据的存储,这两者有不同的应用场景,数组的特点是:寻址容易,插入和删除困难:链表的特点是:寻址困难,插入和删除容易:哈希表的实现结合了这两点, ...
随机推荐
- sso单点登录,单点登录原理图,单点登录图解,单点登录
sso单点登录,单点登录原理图,单点登录图解,单点登录 ============================== ©Copyright 蕃薯耀 2017年11月20日 http://www.cnb ...
- GridControl使用技巧总结,更新中...
1如何禁用GridControl中单击列弹出右键菜单 设置Run Design->OptionsMenu->EnableColumnMenu 设置为:false 2如何定位到第一条数据/记 ...
- ECMAScript 6 第一天 let和const命令
ES6新增声明变量的方法let命令,const命令. (ES5只有两种声明变量的方法:var 命令和 function 命令.) let命令,用来声明变量. 与var声明变量不同于: 1. let声 ...
- 宏WINAPI和几种调用约定
在VC SDK的WinDef.h中,宏WINAPI被定义为__stdcall,这是C语言中一种调用约定,常用的还有__cdecl和__fastcall.这些调用约定会对我们的代码产生什么样的影响?让我 ...
- Lua API 小记1
这些东西是平时遇到的, 觉得有一定的价值, 所以记录下来, 以后遇到类似的问题可以查阅, 同时分享出来也能方便需要的人, 转载请注明来自RingOfTheC[ring.of.the.c@gmail.c ...
- C#学习笔记---数据库连接与异常
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- 软件测试管理QC
一.QC简介 1)是HP公司的产品,是B/S结构的产品 2)在QC服务器中,打开IE浏览器,在地址栏中输入QC服务器的网址或者IP地址. 查看虚拟机的IP地址: 本地连接-属性-TCP/IP协议(重点 ...
- python 正则空格\xa0实录 与xpath取 div 里面的含多个标签的所有文字
业余玩爬虫时,由原先的原生写法 改为 scrapy框架了,使用自带的selector时,xpath配合正则来抓取回复数和阅读数的时候,遇到的小问题,mark下. 首先获取到 我需要的数据块,(我用sc ...
- C++杂分析
class word{ public: word(){cout<<"word constructure \n";} word(int i){cout<<&q ...
- tomcat警告setting property 'debug' to '0' did not find a matching property
在使用tomcat6.0版本结合myeclipse进行java web项目,运行程序显示setting property 'debug' to '0' did not find a matching ...