该文章Github地址:https://github.com/AntonyCheng/java-notes
在此介绍一下作者开源的SpringBoot项目初始化模板(Github仓库地址:https://github.com/AntonyCheng/spring-boot-init-template & CSDN文章地址:https://blog.csdn.net/AntonyCheng/article/details/136555245),该模板集成了最常见的开发组件,同时基于修改配置文件实现组件的装载,除了这些,模板中还有非常丰富的整合示例,同时单体架构也非常适合SpringBoot框架入门,如果觉得有意义或者有帮助,欢迎Star & Issues & PR!
上一章:由浅到深认识Java语言(28):集合
40.集合
Map
Map接口是键值对集合的一个基本接口,是双列集合的顶级接口,没有父接口;
键值对解释:就是由键和值组成的一对关系,键就是 Key,值就是 Value,通过键就能找到值;
键不可以重复,但是值可以重复;
主要实现类:HashMap 类 增删查改;
-
get(Key k)
查询单个值,返回的就是 Key 对应的 Value,System.out.println
查询整个 Map; -
put(Key k,Value v)
增加 Map 中的键值对;该方法一般返回 value 的数据类型,返回内容为 null,但是当 put 进重复的 key 时,会覆盖原有键所对应的 value 值;Student 类:
package top.sharehome.Bag; public class Student { private String name; private int age; public Student() { super(); // TODO Auto-generated constructor stub } public Student(String name, int age) { super(); 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 + "]"; } }
Demo 类:
package top.sharehome.Bag; import java.util.HashMap; import java.util.Map; public class Demo { public static void main(String[] args) { Map map = new HashMap(); Student stu1 = new Student("xiaoming", 20); Student stu2 = new Student("xiaozhang", 21); Student stu3 = new Student("xiaochen", 22); map.put(001, stu1); map.put(002, stu2); map.put(003, stu3); Object object = map.get(001); System.out.println(object); } }
打印效果如下:
-
remove(Key k)
删除该 Key 所对应的键值对,并且返回该键值对,当然clear()
能够实现删除整个Map;Student 类:
package top.sharehome.Bag; public class Student { private String name; private int age; public Student() { super(); // TODO Auto-generated constructor stub } public Student(String name, int age) { super(); 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 + "]"; } }
Demo 类:
package top.sharehome.Bag; import java.util.HashMap; import java.util.Map; public class Demo { public static void main(String[] args) { Map map = new HashMap(); Student stu1 = new Student("xiaoming", 20); Student stu2 = new Student("xiaozhang", 21); Student stu3 = new Student("xiaochen", 22); map.put(001, stu1); map.put(002, stu2); map.put(003, stu3); Object remove = map.remove(001); System.out.println(remove); System.out.println(map); } }
打印效果如下:
-
put(Key k,Value v)
不仅可以增加内容,还可以通过输入要改的键来修改值;Student 类:
package top.sharehome.Bag; public class Student { private String name; private int age; public Student() { super(); // TODO Auto-generated constructor stub } public Student(String name, int age) { super(); 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 + "]"; } }
Demo 类:
package top.sharehome.Bag; import java.util.HashMap; import java.util.Map; public class Demo { public static void main(String[] args) { Map map = new HashMap(); Student stu1 = new Student("xiaoming", 20); Student stu2 = new Student("xiaozhang", 21); map.put(001, stu1); map.put(002, stu2); System.out.println("map = " + map); map.put(001, stu2); System.out.println("修改后map = " + map); } }
打印效果如下:
-
keySet()
将 Map 中所有的键保存在一个数组里面,整体返回出来;Student 类:
package top.sharehome.Bag; public class Student { private String name; private int age; public Student() { super(); // TODO Auto-generated constructor stub } public Student(String name, int age) { super(); 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 + "]"; } }
Demo 类:
package top.sharehome.Bag; import java.util.HashMap; import java.util.Map; import java.util.Set; public class Demo { public static void main(String[] args) { Map map = new HashMap(); Student stu1 = new Student("xiaoming", 20); Student stu2 = new Student("xiaozhang", 21); Student stu3 = new Student("xiaochen", 22); map.put(001, stu1); map.put(002, stu2); map.put(003, stu3); Set keySet = map.keySet(); System.out.println(keySet); } }
打印效果如下:
-
containKey(K)
判断集合是否包含这个键,包含返回 true;示例如下:
package top.sharehome.BigJava; import java.util.HashMap; import java.util.Map; public class Demo { public static void main(String[] args) { Map<Integer,String> m = new HashMap<>(); m.put(1,"xiaohong"); m.put(2,"xiaoming"); m.put(3,"xiaochen"); m.put(4,"xiaofang"); m.put(5,"xiaoying"); boolean b1 = m.containsKey(3); boolean b2 = m.containsKey(6); System.out.println(b1); System.out.println(b2); } }
打印效果如下:
-
containValue(V)
判断集合是否包含这个值,包含返回 true;示例如下:
package top.sharehome.BigJava; import java.util.HashMap; import java.util.Map; public class Demo { public static void main(String[] args) { Map<Integer,String> m = new HashMap<>(); m.put(1,"xiaohong"); m.put(2,"xiaoming"); m.put(3,"xiaochen"); m.put(4,"xiaofang"); m.put(5,"xiaoying"); boolean b1 = m.containsValue("xiaohong"); boolean b2 = m.containsValue("xiaoli"); System.out.println(b1); System.out.println(b2); } }
打印效果如下:
-
size()
返回集合长度,Map集合中键值对的个数;主要是用来判断该Map是否为空;
示例如下:
package top.sharehome.BigJava; import java.util.HashMap; import java.util.Map; public class Demo { public static void main(String[] args) { Map<Integer,String> m = new HashMap<>(); m.put(1,"xiaohong"); m.put(2,"xiaoming"); m.put(3,"xiaochen"); m.put(4,"xiaofang"); m.put(5,"xiaoying"); int i = m.size(); System.out.println(i); } }
打印效果如下:
-
value()
Map集合中的所有的值拿出存储到 Collection<V> 集合中该方法的返回值就是一个Collection,所以不能用它的实现类去接收,我们也不必考虑它的转换过程,但一定要跟上泛型;;
示例如下:
package top.sharehome.BigJava; import java.util.*; public class Demo { public static void main(String[] args) { Map<Integer,String> m = new HashMap<>(); m.put(1,"xiaohong"); m.put(2,"xiaoming"); m.put(3,"xiaochen"); m.put(4,"xiaofang"); m.put(5,"xiaoying"); Collection<String> coll = m.values(); System.out.println(coll); } }
打印效果如下:
遍历Map
方式一(常用)
方法解释:
- 首先要有一个 Map 集合,只要我们能够拿到所有的键,就可以让我们返回所有对应的值;
- 然后通过 map.ksySet() 方法获取所有的键,返回并包装成一个 Collection 接口下的 Set 子接口,此时我们就可以根据遍历 Collection 的迭代器方法来遍历该 Map;
- 然后获得一个迭代器,在循环语句中,将所有 key 赋值为每一次循环的 iterator.next(),再通过 Map.get(key) 方法获得所有 key 所对应的 value;
- 最后输出出来;
示例如下:
package top.sharehome.Bag;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class Demo {
public static void main(String[] args) {
Map map = new HashMap();
map.put(1, "a");
map.put(2, "b");
map.put(3, "c");
map.put(4, "d");
map.put(5, "e");
//map集合里放的是键值对,Key和Value的映射关系
//获得Map中所有的键,就能够遍历出所有的值; ==> map中的 keySet() 方法
Set keySet = map.keySet();
//返回一个包含所有键的 Set ,即可获得该 Set 的迭代器
Iterator iterator = keySet.iterator();
//步骤和 Collection 迭代方式一样
while(iterator.hasNext()) {
Object key = iterator.next();
//这里要通过 map 中的 get() 方法得到每一个键所对应的值
Object value = map.get(key);
System.out.println("Key = "+key+" ==> value = "+value);
}
}
}
打印效果如下:
方式二
方法解释:
通过 Map 集合的 values() 方法获得集合中所有的值;
该方法将所有的值放入一个容器并返回一个 Collection 接口下的 List 子接口(因为 Map 集合中的值可以重复)
示例如下:
package top.sharehome.Bag;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Demo {
public static void main(String[] args) {
Map map = new HashMap();
map.put(1, "a");
map.put(2, "b");
map.put(3, "c");
map.put(4, "d");
map.put(5, "e");
map.put(6, "a");
Set keys = map.keySet();
Collection values = map.values();
System.out.println(keys);
System.out.println(values);
}
}
打印效果如下:
方式三(须知)
方法解释:
使用 Map 中的 entrySet() 内部类,能够直接获取 Map 中的所有映射关系(Entry),并且返回一个 Set 接口,这个接口中存储的元素比较特殊,存储的是 Map 集合中键值对映射关系的对象,内部接口 Map.Entry<K,V> ;
然后又可以用 Set 接口中的迭代器方法做遍历,参见方式一;
映射关系相关的接口 Map.Entry<K,V>:
-
接口常见方法:
equals(Object O)
比较指定对象与该映射项是否相等;getKey()
返回该映射项对应的键;getValue()
返回该映射项对应的值;setValue(Value V)
用指定的值替换该映射项的值; -
方法示例:
package top.sharehome.Bag; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Set; public class Demo { public static void main(String[] args) { Map map = new HashMap(); Map mapTest = new HashMap(); map.put(1, "a"); map.put(2, "b"); map.put(3, "c"); map.put(4, "d"); map.put(5, "e"); map.put(6, "a"); mapTest.putAll(map); Set entrySet1 = map.entrySet(); Set entrySet2 = mapTest.entrySet(); Iterator iterator1 = entrySet1.iterator(); Iterator iterator2 = entrySet2.iterator(); Object next1 = iterator1.next(); Object next2 = iterator2.next(); //以下判断是向下转型 if(next1 instanceof Map.Entry) { if(next2 instanceof Map.Entry) { //以下两条是强转,因为 Object 中没有 entry 里的方法 Entry entry1 = (Entry) next1; Entry entry2 = (Entry) next2; boolean equals = entry1.equals(entry2); Object key1 = entry1.getKey(); Object key2 = entry2.getKey(); Object value1 = entry1.getValue(); Object value2 = entry2.getValue(); System.out.println("equals = " + equals); System.out.println("key1 = " + key1); System.out.println("key2 = " + key2); System.out.println("value1 = " + value1); System.out.println("value2 = " + value2); Object setValue1 = entry1.setValue("A"); Object setValue2 = entry2.setValue("B"); System.out.println("setValue1 = " + setValue1); System.out.println("wetValue2 = " + setValue2); } } } }
打印效果如下:
方式三示例如下:
package top.sharehome.BigJava;
import java.util.*;
public class Demo {
public static void main(String[] args) {
Map<Integer,String> m = new HashMap<>();
m.put(1,"xiaohong");
m.put(2,"xiaoming");
m.put(3,"xiaochen");
m.put(4,"xiaofang");
m.put(5,"xiaoying");
Set<Map.Entry<Integer,String>> s = m.entrySet();
Iterator<Map.Entry<Integer,String>> it = s.iterator();
while (it.hasNext()){
Map.Entry<Integer,String> e = it.next();
Integer key = e.getKey();
String value = e.getValue();
System.out.println("key = "+key+" ==> value = "+value);
}
}
}
打印效果如下:
HashMap(常用)
- HashMap集合特点
- 是哈希表结构
- 为了保证唯一性,用于键的对象,必须重写 hashCode 和 equals 方法,不然会出现该对象作为键时,插入内容相同的对象而会被判定成两个不一样的对象;
- 线程不安全集合,运行速度快
- 集合允许使用 null 作为键或者值
LinkedHashMap
继承于 HashMap ,实现 Map 接口,LinkedHashMap底层实现原理是哈希表,双向连,存取有序,其他的特性和它的父类 HashMap 一样;
示例如下:
我们从键盘获取,来存储 3 个学生对象;
package top.sharehome.BigJava;
import java.util.*;
public class Demo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入学生个数:");
int stuNum = sc.nextInt();
Map<Integer, String> m = MyScan(stuNum);
System.out.println("下面是第一种遍历方式:");
Set<Integer> set1 = m.keySet();
Iterator<Integer> it1 = set1.iterator();
while (it1.hasNext()) {
int key = it1.next();
String value = m.get(key);
System.out.println("key = " + key + " ==> value =" + value);
}
System.out.println("下面是第二种遍历方式:");
Set<Map.Entry<Integer, String>> set2 = m.entrySet();
Iterator<Map.Entry<Integer, String>> it2 = set2.iterator();
while (it2.hasNext()) {
Map.Entry<Integer, String> e = it2.next();
int key = e.getKey();
String value = e.getValue();
System.out.println("key = " + key + " ==> value =" + value);
}
}
public static LinkedHashMap<Integer, String> MyScan(int num) {
Scanner sc = new Scanner(System.in);
LinkedHashMap<Integer, String> m = new LinkedHashMap<>();
for (int i = 0; i < num; i++) {
System.out.println("请输入第" + (i + 1) + "个学生的序号和名字:");
int stuNum = sc.nextInt();
String name = sc.next();
m.put(stuNum, name);
}
sc.close();
return m;
}
}
打印效果如下:
Hashtable
这是 Map 接口的实现类,这个类诞生于 JDK1.0 版本,而 Map 接口诞生于 JDK1.2 版本,所以 Hashtable 类从 JDK1.2 开始,改进为实现 Map 接口;
- Hashtable 类的特点
- 底层数据结构是哈希表
- 线程安全的,运行速度慢,被更加先进的 HashMap 取代
- 不允许 null 值和 null 键
Vector
List 接口的实现 Vector,命运和 Hashtable 一样;
- Vector 类的特点
- 底层实现结构式数组
- 数组的默认容量是 10,每次扩容是原来的两倍
- 线程安全,运行速度,被 ArrayList 取代
TreeMap
-
TreeMap 特点
- 底层实现是红黑树结构(添加查询速度都是比较快的)
- 存储到 TreeMap 中的元素会对键进行排序,排序的依据:
- 对象的自然顺序,作为键的对象,实现了接口 Comparable
- 自己提供比较器,实现接口 Comparator (优先级高)
- 线程不安全,运行速度快
-
示例如下:
package top.sharehome.BigJava; import java.util.*; public class Demo { public static void main(String[] args) { Map<Student, Integer> m = new TreeMap<>(new MyCom()); m.put(new Student("xiaochen", 21), 1); m.put(new Student("xiaoyang", 22), 1); m.put(new Student("xiaohong", 19), 1); Set<Student> set = m.keySet(); Iterator<Student> it = set.iterator(); while (it.hasNext()) { Student key = it.next(); int value = m.get(key); System.out.println("key = " + key + " ==> value = " + value); } } } class MyCom implements Comparator<Student> { public int compare(Student o1, Student o2) { return o1.getAge()-o2.getAge(); } } class Student { private String name; private int age; public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Student)) return false; Student student = (Student) o; if (getAge() != student.getAge()) return false; return getName() != null ? getName().equals(student.getName()) : student.getName() == null; } @Override public int hashCode() { int result = getName() != null ? getName().hashCode() : 0; result = 31 * result + getAge(); return result; } }
打印效果如下:
Properties
-
Properties 集合特点
-
继承 Hashtable,实现 Map 接口
-
底层是哈希表结构
-
线程是安全的,运行速度慢
-
这个集合没有泛型的写法,键和值的数据类型锁定为 String 类型
-
这个集合有自己的特有方法
-
setProperties(String key,String value) 存放元素,不能用 put ,因为 put 的泛型被取消掉
示例如下:
package top.sharehome.BigJava; import java.util.Properties; public class Demo { public static void main(String[] args) { Properties p = new Properties(); p.setProperty("a", "1"); p.setProperty("b", "1"); p.setProperty("c", "1"); System.out.println(p); } }
打印效果如下:
-
getProperties(String Key) 获取value
示例如下:
package top.sharehome.BigJava; import java.util.Properties; public class Demo { public static void main(String[] args) { Properties p = new Properties(); p.setProperty("a", "1"); p.setProperty("b", "2"); p.setProperty("c", "3"); System.out.println(p.getProperty("a")); System.out.println(p.getProperty("b")); System.out.println(p.getProperty("c")); } }
打印效果如下:
-
stringPropertyNames() 返回一个 Set 集合,等同于 Map 里的 keySet()
示例如下:
package top.sharehome.BigJava; import java.util.Iterator; import java.util.Properties; import java.util.Set; public class Demo { public static void main(String[] args) { Properties p = new Properties(); p.setProperty("a", "1"); p.setProperty("b", "2"); p.setProperty("c", "3"); Set<String> s = p.stringPropertyNames(); Iterator<String> it = s.iterator(); while (it.hasNext()){ System.out.println(it.next()); } } }
打印效果如下:
-
-
此集合可以和 IO 流对象结合使用,实现数据的持久存储
-
和 IO 流相关的方法:
load()
输入流;
-
-
遍历 Properties 集合
示例如下:
package top.sharehome.BigJava; import java.util.Iterator; import java.util.Properties; import java.util.Set; public class Demo { public static void main(String[] args) { Properties p = new Properties(); p.setProperty("a", "1"); p.setProperty("b", "2"); p.setProperty("c", "3"); Set<String> s = p.stringPropertyNames(); Iterator<String> it = s.iterator(); while (it.hasNext()){ String key = it.next(); String value = p.getProperty(key); System.out.println("key = "+key+" ==> value ="+value); } } }
打印效果如下: