Map

Map集合概述和特点

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

Map集合的功能概述

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

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());}}
}

一般来说建议使用entrySet遍历方式,其效率高

LinkedHashMap的概述和使用

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

TreeMap集合

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

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;}@Overridepublic int compareTo(Phone o) {return this.getPrice() - o.getPrice();}
}

集合嵌套之HashMap嵌套HashMap

基础班
张三 20
李四 22
就业班
王五 21
赵六 23

public class Test5 {public static void main(String[] args) {HashMap<String, Integer> map = new HashMap<>();map.put("张三",20);map.put("李四",22);HashMap<String, Integer> map1 = new HashMap<>();map1.put("王五",21);map1.put("赵六",23);HashMap<String, HashMap<String, Integer>> mapmax = new HashMap<>();mapmax.put("基础班",map);mapmax.put("就业班",map1);Set<Map.Entry<String, HashMap<String, Integer>>> entries = mapmax.entrySet();for (Map.Entry<String, HashMap<String, Integer>> entry : entries) {System.out.println(entry.getKey());Set<Map.Entry<String, Integer>> entries1 = entry.getValue().entrySet();for (Map.Entry<String, Integer> stringIntegerEntry : entries1) {System.out.println("\t"+stringIntegerEntry.getKey()+"     "+stringIntegerEntry.getValue());}}}
}

Arraylist嵌套HashMap

public class Test3 {public static void main(String[] args) {HashMap<String, String> map = new HashMap<>();map.put("周瑜","小乔");map.put("吕布","貂蝉");HashMap<String, String> map1 = new HashMap<>();map1.put("郭靖","黄蓉");map1.put("杨过","小龙女");HashMap<String, String> map2 = new HashMap<>();map2.put("令狐冲","任盈盈");map2.put("林平之","岳灵珊");ArrayList<HashMap<String, String>> list = new ArrayList<>();list.add(map);list.add(map1);list.add(map2);for (HashMap<String, String> s : list) {Set<String> strings = s.keySet();for (String s1 : strings) {String s2 = s.get(s1);System.out.println("\t"+s1+"---"+s2);}System.out.println();}}
}

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

A:Collections类概述: 针对集合操作 的工具类
B: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): 随机置换

模拟斗地主洗牌和发牌并对牌进行排序的代码实现

public class Test4 {public static void main(String[] args) {//A://案例演示://模拟斗地主洗牌和发牌,牌没有排序//得有一副牌//生成54张牌放到牌盒里面String[] colors = {"♠", "♥", "♦", "♣"};String[] nums = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};HashMap<Integer, String> map = new HashMap<Integer, String>();ArrayList<Integer> list = new ArrayList<>();int index = 0;for (String num : nums) {for (String color : colors) {map.put(index, color.concat(num));list.add(index);index++;}}map.put(index, "☆");list.add(index);index++;map.put(index, "★");list.add(index);//洗牌Collections.shuffle(list);//发牌TreeSet<Integer> 高进 = new TreeSet<>();TreeSet<Integer> 刀仔 = new TreeSet<>();TreeSet<Integer> 星仔 = new TreeSet<>();TreeSet<Integer> 底牌 = new TreeSet<>();// 高进 = (ArrayList<String>) pokerBox.subList(0,17);// 高进 0  3 6 9//刀仔 1 4 7 10//  星仔 2 5 8 11for (int i = 0; i < list.size(); i++) {if (i >= list.size() - 3) {底牌.add(list.get(i));} else if (i % 3 == 0) {高进.add(list.get(i));} else if (i % 3 == 1) {刀仔.add(list.get(i));} else {星仔.add(list.get(i));}}//看牌lookPoker(map,高进,"高进");lookPoker(map,刀仔,"刀仔");lookPoker(map,星仔,"星仔");lookPoker(map,底牌,"底牌");}public static void lookPoker(HashMap<Integer, String> map, TreeSet<Integer> list, String name) {System.out.println(name);for (Integer s : list) {System.out.print(map.get(s));}System.out.println();}
}

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

Java中的Map及其使用相关推荐

  1. java中把map转换成list

    private String key;     private String value;          //把map转换成list的公共方法     public static List map ...

  2. Java中使用Map and Fold进行功能性编程

    在函数式编程中,Map和Fold是两个非常有用的运算符,它们属于每种函数式语言. 如果Map和Fold运算符是如此强大且必不可少,那么您如何解释说即使Java编程语言缺少这两个运算符,我们也可以使用J ...

  3. 谈谈java中遍历Map的几种方法

    java中的map遍历有多种方法,从最早的Iterator,到java5支持的foreach,再到java8 Lambda,让我们一起来看下具体的用法以及各自的优缺点 先初始化一个map public ...

  4. java map中套map_Java Map – Java中的Map

    java map中套map Java Map is part of collections framework. Java Map object is used to store key-value ...

  5. Java中Set Map List 的区别

    java中set map list的区别: 都是集合接口 简要说明 set --其中的值不允许重复,无序的数据结构 list   --其中的值允许重复,因为其为有序的数据结构 map--成对的数据结构 ...

  6. Java中遍历Map集合的5种方式总结

    这篇文章主要给大家介绍了关于Java中遍历Map集合的5种方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值. 方式一 通过Map.keySet使用iterator遍历 ...

  7. JAVA中的Map集合

    JAVA中的Map集合 1.Map简介 java.util.map接口 1.Map称为查找表,该数据结构的样子是一个"多行两列"的表格,左列为key,右列为value,Map总是根 ...

  8. 史上最全讲解:JAVA中的Map与Thread

    史上最全讲解:JAVA中的Map与Thread 文章目录 史上最全讲解:JAVA中的Map与Thread Map HashMap TreeMap Properties Thread 开启多线程方法1 ...

  9. java map迭代_在Java中对Map进行迭代

    #概述 本文,我们将了解一下在Java中迭代Map各种不同方法. 简单来说,我们可以使用keySet().valueSet()或entrySet()来提取Map的内容.因为这些都是Sets,所以类似的 ...

  10. java 定义map_定义map%3ck_v%3e,Java中定义Map恒量,List常量

    Java中定义Map常量,List常量 一般的方式的使用静态代码块.比如: public final static Map map = new HashMap(); static { map.put( ...

最新文章

  1. Oracle数据库的基本语法
  2. 考前自学系列·计算机组成原理
  3. 6.二元查找树的后序遍历结果[PostOrderOfBST]
  4. OpenGL材质Materials
  5. Python小游戏(并夕夕版飞机大战)
  6. 前端知识:如何创建自己的Iconfont图标库
  7. win7家庭版远程桌面补丁_无需惊慌!微软漏洞数月后再被“预警”打补丁即可防御...
  8. Kindeditor JS 取值。。。
  9. hibernate get方法有执行sql但是后台拿不到_「6」进大厂必须掌握的面试题-Hibernate...
  10. 华为nova 4e预热海报曝光:3月14日正式发布!
  11. SQL 增删改查(具体)
  12. Real Application Testing Database Replay、SPA的价格和介绍
  13. OA办公系统需要专业的系统管理员
  14. vscode超好玩好用的插件
  15. 浏览器上不去网络。需要进入ie点开Internet选项,网络中,局域网(LAN)设置,可以勾选上自动检测设置
  16. ProxyDHCP service did not reply to request on port 4011
  17. 职言 | 单纯做业务测试真的行得通吗?
  18. php如何采集,php采集入门教程,教你如何写采集
  19. VS2010 混合模式程序集是针对v1.1.4322版的运行时生成的 在没有配置其他信息的情况下 无法再4.0运行中
  20. Win11如何自动清理垃圾?Win11自动删除文件设置方法

热门文章

  1. 不用P图!用Python给头像加圣诞帽并制作成可执行软件!
  2. No enclosing instance of type TextRunnable is accessible. Must qualify the allocation with an enclo
  3. Scal:Master和worker之间的通信
  4. MATLAB轨迹规划 发给ROS中机器人实现仿真运动
  5. 2021年软件测试入门到进阶全套学习内容
  6. 深度学习基础概念-Batch、Iteration、Epoch理解
  7. html5经纬度定位 源码_基于浏览器的HTML5地理定位
  8. Spark SQL概述
  9. BAT卖不动「医疗云」:医院逃离、山头林立、行有行规
  10. 使用Element UI 开发页面遇到的问题之下拉菜单支持拼音缩写查询