技术标签: jvm java servlet Java基础知识总结
Map是用于保存具有映射关系的数据集合,它具有双列存储的特点,即一次必须添加两个元素,即一组键值对==><Key,Value>,其中Key的值不可重复(当Key的值重复的时候,后面插入的对象会将之前插入的具有相同的Key值的对象覆盖掉),Value的值可重复。Map作为接口,它最常见的实现类是HashMap和TreeMap,作为接口它抽取了所有实现类的共有方法。同时Map不具有带索引的方法,因此无法使用普通for循环来遍历Map集合
其中<Key,Value>键值对,在Java语言中又被称之为Entry/entry,Map.Entry就相当于Student.name,若name的数据类型为String,则Student.name的数据类型为String,同理若<key,value>中key的数据类型为Integer,value的数据类型为String,则Map.Entry的数据类型为<Integer,String>。
接下来我们将结合代码来具体学习Map相关知识。
Map应知应会
Map作为接口,它最常见的实现类是HashMap和TreeMap,作为接口它抽取了所有实现类的共有方法,所有实现Map接口的实现类均可使用这些方法。例如HashMap和TreeMap均可使用如下共有方法。
方法名 | 说明 |
---|---|
V put(K key,V value) | 增添数据 |
V remove(Object Key) | 根据键删除键值对元素 |
void clear() | 移除所有的键值对元素 |
boolean containsKey(Object key) | 判断集合是否包含指定的键 |
boolean containsValue(Object value) | 判断集合是否包含指定的值 |
boolean isEmpty() | 判断集合是否为空 |
V get(Object key)() | 根据键获取值 |
Set keySet() | 获取所有键集合 |
具体实现类为HashMap的Map
public static void main(String[] args) {
//Map为双列存储(一次必须同时存储两个元素),<key,value>key是键,value是值,键不可以重复,值可以重复
//创建一个Key数据类型为Integer类型和Value数据类型为String类型的Map对象,由于Map是一个接口,因此在创建对象时必须指定具体实现类(TreeMap和HashMap均可)
Map<Integer,String> map=new HashMap<>();
//调用put()方法增添数据
map.put(18,"aaa");
map.put(20,"bbb");
map.put(19,"ccc");
//输出数据
System.out.println(map);
}
运行结果:
具体实现类为TreeMap的Map
public static void main(String[] args) {
//Map为双列存储(一次必须同时存储两个元素),<key,value>key是键,value是值,键不可以重复,值可以重复
//创建一个Key数据类型为Integer类型和Value数据类型为String类型的Map对象,由于Map是一个接口,因此在创建对象时必须指定具体实现类(TreeMap和HashMap均可)
Map<Integer,String> map=new TreeMap<>();
//调用put()方法增添数据
map.put(18,"aaa");
map.put(20,"bbb");
map.put(19,"ccc");
//输出数据
System.out.println(map);
}
运行结果:
接下来我们以具体实现类为HashMap的Map来举例
public static void main(String[] args) {
//Map为双列存储(一次必须同时存储两个元素),<key,value>key是键,value是值,键不可以重复,值可以重复
//创建一个Key数据类型为Integer类型和Value数据类型为String类型的Map对象,由于Map是一个接口,因此在创建对象时必须指定具体实现类(TreeMap和HashMap均可)
Map<Integer,String> map=new HashMap<>();
//调用put()方法增添数据
map.put(18,"aaa");
map.put(20,"bbb");
map.put(19,"ccc");
//调用remove()方法删除数据
map.remove(18);
//输出数据
System.out.println(map);
}
运行结果:
public static void main(String[] args) {
//Map为双列存储(一次必须同时存储两个元素),<key,value>key是键,value是值,键不可以重复,值可以重复
//创建一个Key数据类型为Integer类型和Value数据类型为String类型的Map对象,由于Map是一个接口,因此在创建对象时必须指定具体实现类(TreeMap和HashMap均可)
Map<Integer,String> map=new HashMap<>();
//调用put()方法增添数据
map.put(18,"aaa");
map.put(20,"bbb");
map.put(19,"ccc");
map.put(20,"ddd");
map.put(21,"eee");
//调用clear()方法清空数据
map.clear();
//输出数据
System.out.println(map);
}
运行结果:
public static void main(String[] args) {
//Map为双列存储(一次必须同时存储两个元素),<key,value>key是键,value是值,键不可以重复,值可以重复
//创建一个Key数据类型为Integer类型和Value数据类型为String类型的Map对象,由于Map是一个接口,因此在创建对象时必须指定具体实现类(TreeMap和HashMap均可)
Map<Integer,String> map=new HashMap<>();
//调用put()方法增添数据
map.put(18,"aaa");
map.put(19,"bbb");
map.put(20,"ccc");
//调用containsKey()方法查看是否存在指定的键
boolean b = map.containsKey(20);
System.out.println(b);
boolean b1 = map.containsKey(21);
System.out.println(b1);
}
运行结果:
public static void main(String[] args) {
//Map为双列存储(一次必须同时存储两个元素),<key,value>key是键,value是值,键不可以重复,值可以重复
//创建一个Key数据类型为Integer类型和Value数据类型为String类型的Map对象,由于Map是一个接口,因此在创建对象时必须指定具体实现类(TreeMap和HashMap均可)
Map<Integer,String> map=new HashMap<>();
//调用put()方法增添数据
map.put(18,"aaa");
map.put(19,"bbb");
map.put(20,"ccc");
//调用containsValue()方法查看是否存在指定的值
boolean bbb = map.containsValue("bbb");
System.out.println(bbb);
boolean ddd = map.containsValue("ddd");
System.out.println(ddd);
}
运行结果:
public static void main(String[] args) {
//Map为双列存储(一次必须同时存储两个元素),<key,value>key是键,value是值,键不可以重复,值可以重复
//创建一个Key数据类型为Integer类型和Value数据类型为String类型的Map对象,由于Map是一个接口,因此在创建对象时必须指定具体实现类(TreeMap和HashMap均可)
Map<Integer,String> map=new HashMap<>();
//调用put()方法增添数据
map.put(18,"aaa");
map.put(19,"bbb");
map.put(20,"ccc");
//调用isEmpty()方法查看集合是否为空
boolean empty = map.isEmpty();
System.out.println(empty);
//调用clear()方法清空数据
map.clear();
调用isEmpty()方法查看集合是否为空
boolean empty1=map.isEmpty();
System.out.println(empty1);
}
运行结果:
public static void main(String[] args) {
//创建一个Key的数据类型为String,Value数据类型为Integer的Map对象,并指明其实现类为HashMap
Map<String ,Integer> map=new HashMap<>();
//调用put()方法增添数据
map.put("zhangsan",18);
map.put("lisi",19);
map.put("wangwu",20);
map.put("zhaoliu",21);
//调用get()方法来获取Key值对应的Value值
//在Map集合中Key值是非常重要的,我们可以通过Key来获取与Key对应的Value,具体方法为集合调用get()方法
Integer zhangsan = map.get("zhangsan");
System.out.println("Key值为zhangsan的Value值:"+zhangsan);
Integer lisi = map.get("lisi");
System.out.println("Key值为lisi的Value值:"+lisi);
Integer wangwu = map.get("wangwu");
System.out.println("Key值为wangwu的Value值:"+wangwu);
Integer zhaoliu = map.get("zhaoliu");
System.out.println("Key值为zhaoliu的Value值:"+zhaoliu);
}
运行结果:
public static void main(String[] args) {
//创建一个Key的数据类型为String,Value数据类型为Integer的Map对象,并指明其实现类为HashMap
Map<String ,Integer> map=new HashMap<>();
//调用put()方法增添数据
map.put("zhangsan",18);
map.put("lisi",19);
map.put("wangwu",20);
map.put("zhaoliu",21);
//调用keySet()方法获取所有键的集合,并存入Set集合中
//由于Key键的数据类型为String,因此Set的数据类型为String
Set<String> strings = map.keySet();
for (String string : strings) {
System.out.println(string);
}
}
运行结果:
Map入门
Key数据类型为String类型,Value数据类型为Integer类型的Map
public static void main(String[] args) {
//若要使用Map,我们需要导入import java.util.HashMap;与import java.util.Map;
//我们使用泛型来约束Map的数据类型,Key为String数据类型,Value为Integer数据类型
//其具体实现类为HashMap
Map<String,Integer> map=new HashMap<>();
//使用put()方法来添加数据
map.put("zhangsan",18);
map.put("lisi",19);
map.put("wangwu",20);
map.put("chenliu",21);
//普通打印
System.out.println(map);
}
运行结果:
接下来我们探讨Key的值不可重复(当Key的值重复的时候,后面插入的对象会将之前插入的具有相同的Key值的对象覆盖掉),Value的值可重复此问题
Key数据类型为Integer类型,Value数据类型为double类型的Map
其中第四对象和第五对象的Value值相同,第三对象与第六对象的Key值相同。
public static void main(String[] args) {
//若要使用Map,我们需要导入import java.util.HashMap;与import java.util.Map;
//我们使用泛型来约束Map的数据类型,Key为Integer数据类型,Value为Double数据类型
//其具体实现类为HashMap
Map<Integer, Double> map=new HashMap<>();
//使用put()方法来添加数据
map.put(18,18.99);//第一对象
map.put(19,20.00);//第二对象
map.put(20,59.99);//第三对象
map.put(21,20.11);//第四对象
map.put(22,20.11);//第五对象
map.put(20,99.99);//第六对象
//普通打印
System.out.println(map);
}
运行结果:(我们发现后面插入的第六对象将之前插入的第三对象给覆盖掉了,而第四对象与第五对象的Value值重复则不会带来任何影响)
Map基础
Map的遍历分为调用keySet()方法遍历和entrySet()方法遍历
Map调用keySet()方法遍历
public static void main(String[] args) {
//若要使用Map,我们需要导入import java.util.HashMap;与import java.util.Map;
//我们使用泛型来约束Map的数据类型,Key为Integer数据类型,Value为Double数据类型
//其具体实现类为HashMap
Map<Integer, Double> map=new HashMap<>();
//使用put()方法来添加数据
map.put(18,18.99);//第一对象
map.put(19,20.00);//第二对象
map.put(20,59.99);//第三对象
map.put(21,20.11);//第四对象
map.put(22,20.11);//第五对象
map.put(20,99.99);//第六对象
//调用keySet方法遍历
//在Map遍历中Key占据着主导地位,可以通过Key值找到对应的Value值
//调用keySet()方法,Set<>泛型约束应与Key的数据类型一致
//例如在本代码中,Map<Integer, Double>,Key的数据类型为Integer,因此Set<>泛型约束也应当为Integer
//Set<Integer> set11=map.keySet();代码的意思为将Map中所有Key值存入Set集合(18,19,20,21,22)
//那么set11即为Key值集合
Set<Integer> set11=map.keySet();
//使用forEach()语句遍历,Integer为set11的数据类型,i为set11的复用名(相当于set11)
//那么i就成为了Key值
for(Integer i:set11){
//在Map遍历中Key占据着主导地位,可以通过Key值找到对应的Value值
//接下来我们要根据Key值来查找各个Key值对应的Value值
//Value数据类型为Double,设置一个Double变量来存储Value
//map.get(i);代码意思为根据i(Key值)找到相对应的Value值
Double dou=map.get(i);
//打印输出
System.out.println("Key的值为:"+i+" "+"Value的值为:"+dou);
}
}
运行结果:(我们发现输出顺序与插入顺序不一致,这是因为我们使用了Set集合来遍历Map,Set集合具有存取数据不一致的问题)
Map调用entrySet()方法遍历
public static void main(String[] args) {
//若要使用Map,我们需要导入import java.util.HashMap;与import java.util.Map;
//我们使用泛型来约束Map的数据类型,Key为Integer数据类型,Value为Double数据类型
//其具体实现类为HashMap
Map<Integer, Double> map=new HashMap<>();
//使用put()方法来添加数据
map.put(18,18.99);//第一对象
map.put(19,20.00);//第二对象
map.put(20,59.99);//第三对象
map.put(21,20.11);//第四对象
map.put(22,20.11);//第五对象
map.put(20,99.99);//第六对象
//调用entrySet方法遍历
//调用entrySet()方法,Set<>泛型约束应与Map.Entry的数据类型一致,即<Integer, Double>
//<Key,Value>键值对,在Java语言中又被称之为Entry/entry,Map.Entry就相当于Student.name,若name的数据类型为String,则Student.name的数据类型为String,同理若<key,value>中key的数据类型为Integer,value的数据类型为String,则Map.Entry的数据类型为<Integer,String>,在这里就是<Integer, Double>。
//Set<Map.Entry<Integer,Double>> set11=map.entrySet();代码的意思为将Map中所有(Key,Value)值存入Set集合[(18,18.99),(19,20.00),(20,99.99),(21,20.11),(22,20.11)]
//那么set11即为(Key,Value)值集合
Set<Map.Entry<Integer,Double>> set11=map.entrySet();
//使用forEach()语句遍历,Integer为set11的数据类型,i为set11的复用名(相当于set11)
//那么i就成为了(Key,Value)值
for(Map.Entry<Integer,Double> i:set11){
//打印输出,直接调用getKey()方法得到Key值,直接调用getValue()得到Value值
System.out.println("Key的值为:"+i.getKey()+" "+"Value的值为:"+i.getValue());
}
}
运行结果:(我们发现输出顺序与插入顺序不一致,这是因为我们使用了Set集合来遍历Map,Set集合具有存取数据不一致的问题)
Map基础强化
需求:创建一个Map集合,键是学生对象(Student),值是籍贯(String)。
要求:存储三个键值对元素(Entry),并遍历
Student类
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
Map遍历输出
public static void main(String[] args) {
//若要使用Map,我们需要导入import java.util.HashMap;与import java.util.Map;
//我们使用泛型来约束Map的数据类型,Key为Student数据类型,Value为String数据类型
//其具体实现类为HashMap
Map<Student, String> map=new HashMap<>();
//创建Student对象元素
Student student1=new Student("zhangsan",18);
Student student2=new Student("lisi",29);
Student student3=new Student("wangwu",33);
//使用put()方法来添加数据
map.put(student1,"北京");
map.put(student2,"上海");
map.put(student3,"广州");
//调用keySet方法遍历
//在Map遍历中Key占据着主导地位,可以通过Key值找到对应的Value值
//调用keySet()方法,Set<>泛型约束应与Key的数据类型一致
//例如在本代码中,Map<Student, String>,Key的数据类型为Student,因此Set<>泛型约束也应当为Student
//Set<Student> set11=map.keySet();代码的意思为将Map中所有Key值存入Set集合(student1,student2,student3)
//那么set11即为Key值集合
Set<Student> set11=map.keySet();
//使用forEach()语句遍历,Student为set11的数据类型,i为set11的复用名(相当于set11)
//那么i就成为了Key值
for(Student i:set11){
//在Map遍历中Key占据着主导地位,可以通过Key值找到对应的Value值
//接下来我们要根据Key值来查找各个Key值对应的Value值
//Value数据类型为String,设置一个String变量来存储Value
//map.get(i);代码意思为根据i(Key值)找到相对应的Value值
String str=map.get(i);
//打印输出
System.out.println("Key的值为:"+i+" "+"Value的值为:"+str);
}
System.out.println("====华丽的分割线====");
//调用entrySet方法遍历
//调用entrySet()方法,Set<>泛型约束应与Map.Entry的数据类型一致,即<Student, String>
//<Key,Value>键值对,在Java语言中又被称之为Entry/entry,Map.Entry就相当于Student.name,若name的数据类型为String,则Student.name的数据类型为String,同理若<key,value>中key的数据类型为Integer,value的数据类型为String,则Map.Entry的数据类型为<Integer,String>,在这里就是<Student, String>。
//Set<Map.Entry<Student, String>> set22=map.entrySet();代码的意思为将Map中所有(Key,Value)值存入Set集合[(Student("zhangsan",18),"北京"),(Student("lisi",29),“上海”),(Student("wangwu",33),“广州”)]
//那么set22即为(Key,Value)值集合
Set<Map.Entry<Student, String>> set22=map.entrySet();
//使用forEach()语句遍历,Integer为set11的数据类型,i为set22的复用名(相当于set22)
//那么i就成为了(Key,Value)值
for(Map.Entry<Student, String> i:set22){
//打印输出,直接调用getKey()方法得到Key值,直接调用getValue()得到Value值
System.out.println("Key的值为:"+i.getKey()+" "+"Value的值为:"+i.getValue());
}
}
运行结果:(我们发现输出顺序与插入顺序不一致,这是因为我们使用了Set集合来遍历Map,Set集合具有存取数据不一致的问题)
OK!!!Map介绍结束!!!
文章浏览阅读451次。dev/mem: 物理内存的全镜像。可以用来访问物理内存。/dev/kmem: kernel看到的虚拟内存的全镜像。可以用来访问kernel的内容。调试嵌入式Linux内核时,可能需要查看某个内核变量的值。/dev/kmem正好提供了访问内核虚拟内存的途径。现在的内核大都默认禁用了/dev/kmem,打开的方法是在 make menuconfig中选中 device drivers --> ..._dev/mem 源码实现
文章浏览阅读7.1k次,点赞2次,收藏19次。vxe-table,一个小众但功能齐全并支持excel操作的vue表格组件_vxe-table
文章浏览阅读62次。参考:http://www.ruanyifeng.com/blog/2016/01/babel.htmlBabelBabel是一个广泛使用的转码器,可以将ES6代码转为ES5代码,从而在现有环境执行// 转码前input.map(item => item + 1);// 转码后input.map(function (item) { return item..._让开发环境支持bable
文章浏览阅读2.8k次,点赞6次,收藏29次。摘要:FPGA视频处理FIFO的典型应用,视频输入FIFO的作用,视频输出FIFO的作用,视频数据跨时钟域FIFO,视频缩放FIFO的作用_fpga 频分复用 视频
文章浏览阅读575次。【代码】R语言:设置工作路径为当前文件存储路径。_r语言设置工作目录到目标文件夹
文章浏览阅读452次。格式:background: linear-gradient(direction, color-stop1, color-stop2, ...);<linear-gradient> = linear-gradient([ [ <angle> | to <side-or-corner>] ,]? &l..._background线性渐变
文章浏览阅读1k次,点赞26次,收藏8次。第十三届蓝桥杯青少年组python编程省赛真题一、题目要求(注:input()输入函数的括号中不允许添加任何信息)1、编程实现给定一个正整数N,输出正整数N中各数位最大的那个数字。例如:N=132,则输出3。2、输入输出输入描述:只有一行,输入一个正整数N输出描述:只有一行,输出正整数N中各数位最大的那个数字输入样例:
文章浏览阅读2.2k次。一个网络协议主要由以下三个要素组成:1.语法数据与控制信息的结构或格式,包括数据的组织方式、编码方式、信号电平的表示方式等。2.语义即需要发出何种控制信息,完成何种动作,以及做出何种应答,以实现数据交换的协调和差错处理。3.时序即事件实现顺序的详细说明,以实现速率匹配和排序。不完整理解:语法表示长什么样,语义表示能干什么,时序表示排序。转载于:https://blog.51cto.com/98..._网络协议三要素csdn
文章浏览阅读153次。主要的思想,将所有的系统都可以看作两部分,真正的数据log系统和各种各样的query engine所有的一致性由log系统来保证,其他各种query engine不需要考虑一致性,安全性,只需要不停的从log系统来同步数据,如果数据丢失或crash可以从log系统replay来恢复可以看出kafka系统在linkedin中的重要地位,不光是d..._the log: what every software engineer should know about real-time data's uni
文章浏览阅读746次。伟大是熬出来的 目录 前言 引言 时间熬成伟大:领导者要像狼一样坚忍 第一章 内圣外王——领导者的心态修炼 1. 天纵英才的自信心 2. 上天揽月的企图心 3. 誓不回头的决心 4. 宠辱不惊的平常心 5. 换位思考的同理心 6. 激情四射的热心 第二章 日清日高——领导者的高效能修炼 7. 积极主动,想到做到 8. 合理掌控自己的时间和生命 9. 制定目标,马..._当狼拖着受伤的右腿逃生时,右腿会成为前进的阻碍,它会毫不犹豫撕咬断自己的腿, 以
文章浏览阅读285次。在当今的大数据时代,人们对高速度和高带宽的需求越来越大,迫切希望有一种新型产品来作为高性能计算和数据中心的主要传输媒质,所以有源光缆(AOC)在这种环境下诞生了。有源光缆究竟是什么呢?应用在哪些领域,有什么优势呢?易天将为您解答!有源光缆(Active Optical Cables,简称AOC)是两端装有光收发器件的光纤线缆,主要构成部件分为光路和电路两部分。作为一种高性能计..._aoc 光缆
文章浏览阅读2.2k次。在“桌面”上按快捷键“Ctrl+R”,调出“运行”窗口。接着,在“打开”后的输入框中输入“Gpedit.msc”。并按“确定”按钮。如下图 找到“用户配置”下的“Windows设置”下的“Internet Explorer 维护”的“连接”,双击选择“自动浏览器配置”。如下图 选择“自动启动配置”,并在下面的“自动代理URL”中填写相应的PAC文件地址。如下..._設置proxy腳本