Map集合没有继承Collection接口,其提供的是键到值的映射。Map不能包含相同的键,每个键只能映射一个值。键还决定了储存对象在映射中的储存位置。
HashMap集合特点:单一,无序
Map.Entry
映射项(键-值对)K=key,V=value
异常:
(黑体为重点讲解方法)
import java.util.HashMap;
class Test{
public static void main(String []args) {
HashMap map = new HashMap<>();
System.out.println(map.put("ABC001", "Kevin"));
System.out.println(map.put("ABC002", "Lily"));
System.out.println(map.put("ABC001", "Clack")); //此处Key与第一个重复!!
System.out.println(map);
}
}
Output:
null
null
Kevin
{ABC001=Clack, ABC002=Lily}
上述代码,由此可见,添加功能:V put (K key,V value),就是将key映射到value,如果key存在,则覆盖value,并将原来的value返回。
import java.util.Collection;
import java.util.HashMap;
class Test{
public static void main(String []args) {
HashMap<String, String> map = new HashMap<>();
map.put("ABC001", "Kevin");
map.put("ABC002", "Lily");
map.put("ABC001", "Clack");
map.put("ABC003", "Bob");
map.put("ABC004", "Zombie");
//Collection values()
Collection<String> values=map.values();
for(String value: values) {
System.out.println(value);
}
}
}
Output:
Clack
Lily
Bob
Zombie
Collection< V> values()获取到所有Value的值
import java.util.HashMap;
import java.util.Set;
class Test{
public static void main(String []args) {
HashMap<String, String> map = new HashMap<>();
map.put("ABC001", "Kevin");
map.put("ABC002", "Lily");
map.put("ABC001", "Clack");
map.put("ABC003", "Bob");
map.put("ABC004", "Zombie");
//Set keySet()
Set<String> keys = map.keySet();
for(String key: keys) {
System.out.println(key);
}
}
}
Output:
ABC001
ABC002
ABC003
ABC004
Set< K> keySet()以Set的形式返回所有的key
获取每一个Key,通过Key来找Value
import java.util.HashMap;
import java.util.Set;
class Test{
public static void main(String []args) {
HashMap<String, String> map = new HashMap<>();
map.put("ABC001", "Kevin");
map.put("ABC002", "Lily");
map.put("ABC001", "Clack");
map.put("ABC003", "Bob");
map.put("ABC004", "Zombie");
//获取所有key
Set<String> keys = map.keySet();
for(String key: keys) {
//通过Key来找Value
String value = map.get(key);
System.out.println("ID:"+key+"--Name: "+value);
}
}
}
Output:
ID:ABC001--Name: Clack
ID:ABC002--Name: Lily
ID:ABC003--Name: Bob
ID:ABC004--Name: Zombie
通过(entry)中间人Set
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
class Test{
public static void main(String []args) {
HashMap<String, String> map = new HashMap<>();
map.put("ABC001", "Kevin");
map.put("ABC002", "Lily");
map.put("ABC001", "Clack");
map.put("ABC003", "Bob");
map.put("ABC004", "Zombie");
//获取所有中间人entrys
Set<Map.Entry<String, String>> entrys =map.entrySet();
//遍历所有entrys用Map.Entry类型的entry接收
for(Map.Entry<String, String> entry:entrys) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println("ID:"+key+"--Name: "+value);
}
}
}
Output:
ID:ABC001--Name: Clack
ID:ABC002--Name: Lily
ID:ABC003--Name: Bob
ID:ABC004--Name: Zombie
注意⚠️,如果要放入自定义类型的数据当作key,一定要重写hashcode()和equals()防止重复。
HashTable出现在JDK1.0, 线程安全,因为不能存入null类型数据
HashMap出现在JDK1.5,线程不安全,因为能存入null
用法跟HashMap基本一致,它是基于链表和哈希表结构的所以具有存取有序,键不重复的特性,存的顺序和遍历出来的顺序是一致的。
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
class Test{
public static void main(String []args) {
LinkedHashMap<String, String> map = new LinkedHashMap<>();
map.put("ABC001", "Kevin");
map.put("ABC003", "Lily");
map.put("ABC001", "Clack");
map.put("ABC002", "Bob");
map.put("ABC004", "Zombie");
//获取所有中间人entrys
Set<Map.Entry<String, String>> entrys =map.entrySet();
//遍历所有entrys用Map.Entry类型的entry接收
for(Map.Entry<String, String> entry:entrys) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println("ID:"+key+"--Name: "+value);
}
}
}
Output:
ID:ABC001--Name: Clack
ID:ABC003--Name: Lily
ID:ABC002--Name: Bob
ID:ABC004--Name: Zombie
TreeMap与TreeSet类似,都需要重写比较器(外部比较器+内部比较器)
TreeMap集合特点:单一,有序
下面举例,外部比较器与匿名对象类结合
import java.util.Comparator;
import java.util.Map.Entry;
import java.util.TreeMap;
public class Test {
public static void main(String[] args) {
TreeMap<Student,String> map = new TreeMap<>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
if (o1 == o2){
return 0;
}
int result = o1.getAge() - o2.getAge();
if (result == 0){
return o1.getName().compareTo(o2.getName());
}
return result;
}
});
map.put(new Student("A", 12), "JAVA");
map.put(new Student("C", 50), "IOS");
map.put(new Student("D", 32), "JS");
map.put(new Student("B", 32), "PHP");
map.put(new Student("E", 12), "C++");
for (Entry<Student,String> entry : map.entrySet()){
System.out.println(entry.getKey()+"==="+entry.getValue());
}
}
}
Output:
Student [name=A, age=12]===JAVA
Student [name=E, age=12]===C++
Student [name=B, age=32]===PHP
Student [name=D, age=32]===JS
Student [name=C, age=50]===IOS
注意⚠️:
1.存入的时候添加了两个张三,如果Map中键相同的时候,当后面的值会覆盖掉前面的值
2.TreeMap 取出来的顺序是经过排序的,是根据compara方法排序的
3.新建的比较器对象应传入TreeMap中,该段代码中,用匿名对象new comparator< Student>(){……}
传入TreeMap
最后的括号内,即TreeMap
内部比较器与TreeMap结合的例子,请看TreeSet有关例子
(https://blog.csdn.net/weixin_44551646/article/details/94898951)。