当前位置:网站首页>双列集合(Map集合)
双列集合(Map集合)
2022-06-21 06:24:00 【naoguaziteng】
目
录
一.Map集合
1.概述
针对于键值对应关系的数据,Java为了我们方便操作,给我们提供了另外一种集合叫做Map 专门存储键值对应关系的数据.Map集合也称为双列集合.Map接口----->Map<K,V>中的K是指键,映射所维护的键的类型,V是指值,映射值的类型.
2.Map接口和Collection接口的不同
Map接口是一个不同于Conllection接口的接口,它们之间也没有什么联系.也有较大的区别!
- Map是双列的,Collection是单列的
- Map的键唯一,Collection的子体系Set是唯一的
- Map集合的数据结构针对键有效,跟值无关;Collection集合的数据结构是针对元素有效
3.Map集合体系

4.键对应关系
一个键只能对应一个值,键相同,则值覆盖.并且所有的双列集合的数据结构只跟键有关,跟值没有关系.
import java.util.HashMap;
public class MapTest {
public static void main(String[] args) {
HashMap<Integer, String> hm=new HashMap();
hm.put(1001,"张三");
hm.put(1002,"李四");
hm.put(1003,"王五");
hm.put(1001,"刘六");
System.out.println(hm.get(1001)); //刘六
}
}5.Map集合中的方法
- put(); 向集合中添加键值(这个其实还有另一个功能:替换. 如果键是第一次存储,就直接存储元素,返回null. 如果键不是第一次存在,就用值把以前的值替换掉,返回以前的值)
- clear(); 移除所有的键值对元素
- remove(Object key); 根据键删除键值对元素,并把值返回
- containsKey(Object key); 判断集合是否包含指定的键
- containsValue(Object value); 判断集合是否包含指定的值
- isEmpty(); 判断集合是否为空
- entrySet(); 返回一个键值对的Set集合
- get(Object key); 根据键获取值
- keySet(); 获取集合中所有键的集合,放入到Set集合
- values(); 获取集合中所有值的集合,放入到Collection集合
- size(); 返回集合中的键值对的对数即集合的长度
6.Map集合的遍历
- 键找值(get(key))
- 获取整个键值对对象(Node)
- forEach方法
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
public class MapTest {
public static void main(String[] args) {
//map集合的遍历
HashMap<String, String> hm = new HashMap<>();
hm.put("我", "喜欢画画");
hm.put("你", "喜欢打乒乓");
hm.put("他", "喜欢听音乐");
hm.put("它", "喜欢吃小鱼干");
//方式1:键找值
Set<String> keySet = hm.keySet();
for (String key : keySet) {
String value = hm.get(key);
System.out.println(key + "===" + value);
}
//方式2: 获取所有键值对,对象的集合
Set<Map.Entry<String, String>> entries = hm.entrySet();
for (Map.Entry<String, String> node : entries) {
String key = node.getKey();
String value = node.getValue();
System.out.println(key + "====" + value);
}
System.out.println("====================================");
//方式3: forEach()
hm.forEach(new BiConsumer<String, String>() {
@Override
public void accept(String key, String value) {
System.out.println(key + "====" + value);
}
});
}
}
二.HashMap集合(元素唯一,无序)
1.HashMap简介
HashMap的数据结构是哈希表(数组+链表+红黑树),元素唯一,无序.(HashMap键的唯一性,是靠键重写hashCode 和equals()方法来保证的,如果不重写,则无法保证键的唯一性.)HashMap集合允许插入null键 null值.
2.HashMap与Hashtable的区别
- HashMap可以存储 null值 null键; Hashtable 他键或者值,不允许是null值.
- HashMap线程不安全,效率高; Hashtable线程安全,效率低.
三.LinkedHashMap集合(元素唯一,有序)
底层的数据结构是链表和哈希表, 元素有序并且唯一. 元素的有序性由链表数据结构保证, 唯一性由 哈希表数据结构保证. (谨记: Map集合的数据结构只和键有关)
import java.util.LinkedHashMap;
public class LinkedHashMapTest {
public static void main(String[] args) {
LinkedHashMap<String, String> lh = new LinkedHashMap<>();
lh.put("我", "喜欢画画");
lh.put("你", "喜欢打乒乓");
lh.put("他", "喜欢听音乐");
lh.put("它", "喜欢吃小鱼干");
System.out.println(lh);
}
}
四.TreeMap集合(元素唯一,有序且可排序)
TreeMap键的数据结构是红黑树,可保证键的排序和唯一性. 排序分为自然排序和比较器排序,一般默认为自然排序. 线程是不安全的效率比较高.
关于排序与TreeSet集合相同.只是创建的集合不同罢了! (了解相关排序点击这里)
package com.xingyun.test;
import java.util.Comparator;
import java.util.TreeMap;
import java.util.function.BiConsumer;
public class TreeMapTest {
public static void main(String[] args) {
//1. 自然排序
//按照年龄进行排序
TreeMap<Student,String> treeMap = new TreeMap<>(); //空参构造
treeMap.put(new Student("张三", 23),"热爱生活");
treeMap.put(new Student("李四", 27),"热爱JAVA");
treeMap.put(new Student("刘德华", 21),"热爱足球");
treeMap.put(new Student("欧阳震华", 28),"热爱画画");
treeMap.put(new Student("张三", 23),"热爱音乐");
treeMap.forEach(new BiConsumer<Student, String>() {
@Override
public void accept(Student student, String s) {
System.out.println(student.getAge()+"==="+student.getName()+"==="+s);
}
});
/*
21===刘德华===热爱足球
23===张三===热爱音乐
27===李四===热爱JAVA
28===欧阳震华===热爱画画
*/
//比较器排序
TreeMap<Student,String> treeMap2 = new TreeMap<>(new Comparator<Student>() {
//匿名内部类
@Override
public int compare(Student s1, Student s2) {
//根据名字长度排序
int num = s1.getName().length() - s2.getName().length();
//名字长度相同则根据名字内容
int num2 = num == 0 ? s1.getName().compareTo(s2.getName()) : num;
//都相同的话,根据年龄排序
int num3 = num2 == 0 ? s1.getAge() - s2.getAge() : num2;
return num3;
}
});
treeMap2.put(new Student("张三", 23),"热爱生活");
treeMap2.put(new Student("李四", 27),"热爱JAVA");
treeMap2.put(new Student("刘德华", 21),"热爱足球");
treeMap2.put(new Student("欧阳震华", 28),"热爱画画");
treeMap2.put(new Student("张三", 23),"热爱音乐");
treeMap2.forEach(new BiConsumer<Student, String>() {
@Override
public void accept(Student student, String s) {
System.out.println(student.getName()+"==="+student.getAge()+"==="+s);
}
});
/*
张三===23===热爱音乐
李四===27===热爱JAVA
刘德华===21===热爱足球
欧阳震华===28===热爱画画
*/
}
}
(小编也在努力学习更多哟!以后再慢慢分享的啦!)

希望对友友们有所帮助!!!!
边栏推荐
- docker 安装mysql
- FPGA - 7 Series FPGA selectio -04- ideay and ideayctrl of logical resources
- 第6期:大学生应该选择哪种主流编程语言
- TypeError: iter() returned non-iterator of type ‘xxx‘
- 【MySQL】数据库多表操作通关教程(外键约束、多表联合查询)
- 太厉害了MySQL总结的太全面了
- 【JDBC从入门到实战】JDBC基础通关教程(全面总结上篇)
- Aurora8B10B IP使用 -02- IP功能设计技巧
- leetcode 410. 分割数组的最大值——(每日一难day30)
- Aurora8B10B IP使用 -03- IP配置应用指南
猜你喜欢

第7期:内卷和躺平,你怎么选

Aurora8B10B IP使用 -05- 收发测试应用示例

【JDBC從入門到實戰】JDBC基礎通關教程(全面總結上篇)

粽子大战 —— 猜猜谁能赢

太厉害了MySQL总结的太全面了

Pyshark tutorial

My college entrance examination experience and summary

Dragon Boat Festival - simple side navigation bar

Aurora8b10b IP use-04-ip routine application example

C语言实现模拟银行存取款管理系统课程设计(纯C语言版)
随机推荐
WordPress pseudo original tool - update website one click pseudo original publishing software
nametuple的源码为什么要使用.replace(‘,‘, ‘ ‘).split()而不是.split(‘,‘)
Construction and protection of small-scale network examination
Fastdfs cluster
Aurora8B10B IP使用 -05- 收发测试应用示例
The framework and script of cognitive linguistics
端午节-简单侧边导航栏
Niuke-top101-bm26
[JDBC from introduction to actual combat] JDBC basic customs clearance tutorial (comprehensive summary part I)
【笔记自用】myeclipse连接MySQL数据库详细步骤
[data mining] final review Chapter 3
Solve the first problem of Huawei's machine test on April 20 by recursion and circulation (100 points)
递归建立链式二叉树,完成前中后序遍历以及其他功能(附源码)
小程序【第一期】
Aurora8b10b IP usage-02-ip function design skills
5254. 卖木头块 动态规划
Pycharm的快捷键Button 4 Click是什么?
Idea usage record
Latest analysis on operation of refrigeration and air conditioning equipment in 2022 and examination question bank for operation of refrigeration and air conditioning equipment
fastdfs集群