Java中Map的简介及其使用

发布时间:2021-08-25 16:59:40 作者:chen
来源:亿速云 阅读:125

这篇文章主要讲解了“Java中Map的简介及其使用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Java中Map的简介及其使用”吧!

1.Map集合概述和特点

概述:
将键映射到值的对象,一个映射不能包含重复的键,每个键最多只能映射到一个值。
Map接口和Collection接口的不同
Map是双列的,Collection是单列的
Map的键唯一,Collection的子体系Set是唯一的
Map集合的数据结构针对键有效,跟值无关;Collection集合的数据结构是针对元素有效。

2.Map集合的功能概述

(1):添加
V put(K key,V value):添加元素。这个其实还有另一个功能?替换
如果键是第一次存储,就直接存储元素,返回null
如果键不是第一次存在,就用值把以前的值替换掉,返回以前的值
(2)  :删除
void clear():移除所有的键值对元素
V remove(Object key):根据键删除键值对元素,并把值返回
(3)  :判断
boolean containsKey(Object key):判断集合是否包含指定的键
boolean containsValue(Object value):判断集合是否包含指定的值
boolean isEmpty():判断集合是否为空
(4)  :获取
Set<Map.Entry<K,V>> entrySet(): 返回一个键值对的Set集合
V get(Object key):根据键获取值
Set keySet():获取集合中所有键的集合
Collection values():获取集合中所有值的集合
(5)  :长度
int size():返回集合中的键值对的对数

3.Map集合的遍历之键找值

获取所有键的集合,遍历键的集合,获取到每一个键根据键找值  

示例代码如下:

public class Test4 {
   public static void main(String[] args) {
       HashMap<Phone,String> map = new HashMap<>();
       map.put(new Phone("Apple",7000),"美国");
       map.put(new Phone("Sony",5000),"日本");
       map.put(new Phone("Huawei",6000),"中国");
       Set<Phone> phones = map.keySet();
       Iterator<Phone> iterator = phones.iterator();
       while (iterator.hasNext()){
           Phone next = iterator.next();
           System.out.println(next.getBrand()+"=="+next.getPrice()+"=="+map.get(next));
       }

   }
}
class Phone{
   private String Brand;
   private int Price;

   public Phone(String brand, int price) {
       Brand = brand;
       Price = price;
   }

   public String getBrand() {
       return Brand;
   }

   public void setBrand(String brand) {
       Brand = brand;
   }

   public int getPrice() {
       return Price;
   }

   public void setPrice(int price) {
       Price = price;
   }
}

获取所有键值对对象的集合,遍历键值对对象的集合,获取到每一个键值对对象,根据键值对对象找键和值  

示例代码如下:

public class Test4 {
   public static void main(String[] args) {
       HashMap<Phone,String> map = new HashMap<>();
       map.put(new Phone("Apple",7000),"美国");
       map.put(new Phone("Sony",5000),"日本");
       map.put(new Phone("Huawei",6000),"中国");
       Set<Map.Entry<Phone, String>> entries = map.entrySet();
       for (Map.Entry<Phone, String> entry : entries) {
           System.out.println(entry.getKey().getBrand()+"==="+entry.getKey().getPrice()+"==="+entry.getValue());
       }
   }
}

4.LinkedHashMap的概述和使用

LinkedHashMap的概述: Map 接口的哈希表和链接列表实现,具有可预知的迭代顺序。LinkedHashMap的特点: 底层的数据结构是链表和哈希表 元素有序 并且唯一。
元素的有序性由链表数据结构保证 唯一性由 哈希表数据结构保证。
Map集合的数据结构只和键有关  

5.TreeMap集合

TreeMap 键不允许插入null
TreeMap: 键的数据结构是红黑树,可保证键的排序和唯一性
排序分为自然排序和比较器排序
线程是不安全的效率比较高
6.TreeMap集合排序:
实现Comparable接口,重写CompareTo方法
使用比较器  

7.TreeMap集合的遍历

示例代码如下:

public class Test4 {
   public static void main(String[] args) {
       TreeMap<Phone,String> map = new TreeMap<>();
       map.put(new Phone("Apple",7000),"美国");
       map.put(new Phone("Sony",5000),"日本");
       map.put(new Phone("Huawei",6000),"中国");
       Set<Phone> phones = map.keySet();
       Iterator<Phone> iterator = phones.iterator();
       while(iterator.hasNext()){
           Phone next = iterator.next();
           System.out.println(next.getBrand()+"==="+next.getPrice()+"==="+map.get(next));
       }
   }
}
class Phone implements Comparable<Phone>{
   private String Brand;
   private int Price;

   public Phone(String brand, int price) {
       Brand = brand;
       Price = price;
   }

   public String getBrand() {
       return Brand;
   }

   public void setBrand(String brand) {
       Brand = brand;
   }

   public int getPrice() {
       return Price;
   }

   public void setPrice(int price) {
       Price = price;
   }

   @Override
   public int compareTo(Phone o) {
       return this.getPrice() - o.getPrice();
   }
}

8.Collections工具类的概述和常见方法

(1):Collections类概述: 针对集合操作 的工具类
(2):Collections成员方法
public static void sort(List list): 排序,默认按照自然顺序
public static int binarySearch(List<?> list,T key): 二分查找
public static T max(Collection<?> coll): 获取最大值
public static void reverse(List<?> list): 反转
public static void shuffle(List<?> list): 随机置换

Map中的键唯一,但是当存储自定义对象时,需要重写Hashcode和equals方法  

感谢各位的阅读,以上就是“Java中Map的简介及其使用”的内容了,经过本文的学习后,相信大家对Java中Map的简介及其使用这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

推荐阅读:
  1. hadoop中slot简介(map slot 和 reduce slot)
  2. java中的map

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java

上一篇:FTP的主动模式和被动模式

下一篇:怎么让Git记住用户名和密码

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》