Map的知识点梳理(不包含collections工具类)
一:基础公共方法(map不再是简单的加索引,可以直接命名key,通过key进行遍历)
1.常见的map子类
HashTable:底层是哈希表结构。不允许null键和null值,线程同步
HashMap:底层是哈希表结构,可以允许null键和null值,线程不同步
TreeMap:底层是二叉树结构,线程不同步,可以给map的键排序
2.map程序--公共的方法
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map; public class Test60 { public static void main(String[] args) {
Map<Integer,String> hashMap=new HashMap<>();
//增
hashMap.put(1, "java1");
hashMap.put(2, "java2");
hashMap.put(3, "java3");
//判断
System.out.println(hashMap.containsKey(2));
System.out.println(hashMap.containsValue("java2"));
System.out.println(hashMap.isEmpty());
//删除
System.out.println(hashMap.remove(3));
//获取1
System.out.println(hashMap);
System.out.println(hashMap.get(2));
//获取2
Collection<String> col=hashMap.values();//values方法返回的是Collection,这时候可以使用迭代器迭代出成员。
Iterator it=col.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
} }
3.运行结果

二:map遍历的两个重要方法
1.keyset的使用
把map转换成Set类型,就是将所有的键存入到Set<K>中。
再使用get(K)来获得对应的value值。
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class Test61 {
public static void main(String[] args) {
Map<Integer,String> hashMap=new HashMap<>();
hashMap.put(1, "java1");
hashMap.put(2, "java2");
hashMap.put(3, "java3");
Set<Integer> keySet=hashMap.keySet();
Iterator<Integer> it=keySet.iterator();
while(it.hasNext()){
Integer key=it.next();
String value=hashMap.get(key);
System.out.println(key+":"+value);
}
}
}
2.运行结果

3.entrySet的使用
返回包含键值关系的Set视图,返回值放入Set中,类型是Map.Entry<K,V>。
得到映射关系后,可以选择的方法是getKey与getValue分别取出key与value。
public static void main(String[] args) {
Map<Integer,String> hashMap=new HashMap<>();
hashMap.put(1, "java1");
hashMap.put(2, "java2");
hashMap.put(3, "java3");
Set<Map.Entry<Integer,String>> set=hashMap.entrySet();
Iterator<Map.Entry<Integer, String>> itr=set.iterator();
while(itr.hasNext()){
Map.Entry<Integer, String> mapentry= itr.next();
Integer key=mapentry.getKey();
String value=mapentry.getValue();
System.out.println(key+"::"+value);
}
}
4.运行结果

三:map扩展
1.HashMap
保证学生对象的唯一性。
对学生对象的id进行排序。
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class Test63 {
public static void main(String[] args) {
TreeMap<Student,String> tm=new TreeMap<>();
tm.put(new Student(1,"zhangsan"),"beijing");
tm.put(new Student(2,"lisi"),"shanghai");
tm.put(new Student(4,"zhaoliu"),"hangzhou");
tm.put(new Student(3,"wangwu"),"shenzhen");
tm.put(new Student(2,"lisi"),"shanghai");
Set<Map.Entry<Student,String>> set=tm.entrySet();
Iterator<Map.Entry<Student,String>> itr=set.iterator();
while(itr.hasNext()){
Map.Entry<Student, String> me=itr.next();
Student stu=me.getKey();
String str=me.getValue();
System.out.println(stu+"::"+str);
}
}
}
class Student implements Comparable<Student>{
private int id;
private String name;
Student(int id,String name){
this.id=id;
this.name=name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int compareTo(Student s){
int num=new Integer(this.id).compareTo(new Integer(s.id));
if(num==0){
return name.compareTo(s.name);
}
return num;
}
public int hashCode(){
return name.hashCode()+id*19;
}
public boolean equals(Object obj){
if(!(obj instanceof Student)){
return false;
}
Student s = (Student) obj;
return this.name.equals(s.name)&&this.id==s.id;
}
@Override
public String toString() {
return "id=" + id + ", name=" + name;
} }
2.运行结果
可以进行排序,学生具备了自然排序的功能(comparable接口),并且对于重复的记录只会保存一条

3.TreeMap
对学生对象的name进行排序,同时保持id具备自身的比较性。
student对象仍然是上面例子中的对象,不再改动。
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap; public class Test64 {
public static void main(String[] args) {
TreeMap<Student,String> tm=new TreeMap<>(new StunameComparator());
tm.put(new Student(1,"zhangsan"),"beijing");
tm.put(new Student(2,"lisi"),"shanghai");
tm.put(new Student(4,"zhaoliu"),"hangzhou");
tm.put(new Student(3,"wangwu"),"shenzhen");
tm.put(new Student(2,"lisi"),"shanghai");
Set<Map.Entry<Student,String>> set=tm.entrySet();
Iterator<Map.Entry<Student,String>> itr=set.iterator();
while(itr.hasNext()){
Map.Entry<Student, String> me=itr.next();
Student stu=me.getKey();
String str=me.getValue();
System.out.println(stu+"::"+str);
}
}
}
class StunameComparator implements Comparator<Student>{ @Override
public int compare(Student s1, Student s2) {
int num=s1.getName().compareTo(s2.getName());
if(num==0){
return new Integer(s1.getId()).compareTo(s2.getId());
}
return num;
} }
4.运行结果

5.小练习(统计字符串中出现的字符次数)
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap; public class Test65 {
public static void main(String[] args) {
String s=charCount("aaabdd");
System.out.println(s);
}
private static String charCount(String str) {
char[] arr=str.toCharArray();
TreeMap<Character,Integer> tm=new TreeMap<>();
int count=0;
for(int i=0;i<arr.length;i++){
if(!(arr[i]>='a'&&arr[i]<='z'||arr[i]>='A'&&arr[i]<='Z'))
continue;
Integer value=tm.get(arr[i]);
System.out.println("-----"+value);
if(value!=null)
count=value;
count++;
tm.put(arr[i], count);
count=0;
}
StringBuffer sb=new StringBuffer();
Set<Map.Entry<Character,Integer>> entry=tm.entrySet();
Iterator<Map.Entry<Character, Integer>> itr=entry.iterator();
while(itr.hasNext()){
Map.Entry<Character, Integer> me=itr.next();
Character ch=me.getKey();
Integer value=me.getValue();
sb.append(ch+"("+value+")");
}
return sb.toString();
}
}
6.运行结果

Map的知识点梳理(不包含collections工具类)的更多相关文章
- Map集合、HashMap集合、LinkedHashMap集合、Hashtable集合、Collections工具类和模拟斗地主洗牌和发牌
1.Map集合概述和特点 * A:Map接口概述 * 查看API可以知道: * 将键映射到值的对象 * 一个映射不能包含重复的键 * 每个键最多 ...
- Collections工具类、Map集合、HashMap、Hashtable(十八)
1.Map集合概述和特点 * A:Map接口概述 * 去重复, * 查看API可以知道, * 将键映射到值的对象, * 一个映射不能包含重复的键, * 每个键最多只能映射到一个值.* B:Map接口和 ...
- day07(Set接口,HashSet类,hashcoad(),Collections工具类,Map集合)
Set接口 set接口的实现类特点 1.无序(取出来的顺序和存进去的数据的顺序不一致) 2.唯一(数据不能存相同的) 底层是用Map集合写的 HashSet类 实现了 set接口 唯一 ...
- [19/03/27-星期三] 容器_Iterator(迭代器)之遍历容器元素(List/Set/Map)&Collections工具类
一.概念 迭代器为我们提供了统一的遍历容器的方式 /* *迭代器遍历 * */ package cn.sxt.collection; import java.security.KeyStore.Ent ...
- Java容器类Collection,List,Set,Map.,Iterator,Collections工具类,Arrays工具类,Comparable
Java容器类Collection,List,Set,Map.,Iterator,Collections工具类,Arrays工具类,Comparable接口,泛型 Collection,List,Se ...
- (Set, Map, Collections工具类)JAVA集合框架二
Java集合框架部分细节总结二 Set 实现类:HashSet,TreeSet HashSet 基于HashCode计算元素存放位置,当计算得出哈希码相同时,会调用equals判断是否相同,相同则拒绝 ...
- java 集合Collections 工具类:排序,查找替换。Set、List、Map 的of方法创建不可变集合
Collections 工具类 Java 提供1个操作 Set List Map 等集合的工具类 Collections ,该工具类里提供了大量方法对集合元素进行排序.查询和修改等操作,还提供了将集合 ...
- LinkedHashSet、Map、Map接口HashMap、Hashtable,TreeSet、TreeMap、如何选择使用集合实现类,Collections工具类
一.Set接口实现类LinkedHashSet 实现继承图: 1.LinkedHashSet的全面说明 1) LinkedHashSet是 HashSet的子类 2) LinkedHashSet底层是 ...
- 双列集合Map接口 & Collections工具类
HashMap 常用方法 遍历方式 iterator迭代器 ITIT HashTable 继承字典 Hashtable--Properties 文件读写 总结 Collections工具类
随机推荐
- DBeaver入门
1 安装好连接好数据库,查询操作 注意黄色字体1 2 3 4 执行sql操作
- WEB即时通信
问题 传统的浏览器通信方式:基于HTTP协议的请求/响应模式. 早期:通过刷新浏览器来更新服务器端的数据 后来Ajax(XMLHttpRequest是核心):可以不用刷新浏览器更新服务器端数据.但是这 ...
- jQuery - ajaxUpLoad.js
ajaxFileUpload是一个异步上传文件的jQuery插件 语法:$.ajaxFileUpload([options]) options参数说明: 参数 作用 url 上传处理程序地址 file ...
- JavaScript学习 - 基础(三) - 运算符
js运算符 1.算数运算符 包括 加(+) .减-() .乘(*).除(/).余数(%) 减号 还可以表示为 负号 例如: -1,-3 加号 还可以用于字符串拼接 例如: 'a' + 'b' = 'a ...
- 2018-2019-2 网络对抗技术 20165320 Exp3 免杀原理与实践
### 2018-2019-2 网络对抗技术 20165320 Exp3 免杀原理与实践 一.实验内容 1.1 正确使用msf编码器(0.5分),msfvenom生成如jar之类的其他文件(0.5分) ...
- .Net Core中使用RabbitMQ
(1).引入依赖 RabbitMQ.Client (2).编写发布者代码 var connectionFactory = new ConnectionFactory() { HostName=&quo ...
- C++学习2--坦克大战编写-前置知识
基础班学习的这一个多月里的前三周讲解基础的语法,最后一周需要做坦克大战的项目巩固提高自己掌握的语法知识.这个系列博文主要是为了把学习过程中的知识点总结并记录下来: 开发语言与开发工具:C++,VS20 ...
- Mac环境变量配置错了以后初始化的方法
转自:https://blog.csdn.net/or_7r_ccl/article/details/50886223 配置过安卓开发环境,改过bash_profile这个文件,最后不知怎么的只有cd ...
- nodejs 在线学习课堂
http://ww***/class/5359f6f6ec7452081a7873d8
- windows下解压zip包,包含中文解析
#coding=utf8 import os import zipfile import sys,locale # 本来以为需要,结果不需要 # def p(f): # #print '%s.%s() ...