首页 > 其他分享 >集合框架5-----Map体系

集合框架5-----Map体系

时间:2022-08-19 09:48:47浏览次数:67  
标签:Map hashMap System treeMap println ----- Student 集合 out

Map体系集合

参考视频:13.33 Map集合概述哔哩哔哩bilibili

 

 

HashMap(),代码如下:

import java.util.HashMap;
import java.util.Map;


public class HashDemo2 {
   public static void main(String[] args) {
       //创建集合
       HashMap<Object, Object> hashMap = new HashMap<>();
       //1.添加元素
       Student s1 = new Student("小明",11);
       Student s2 = new Student("小红",12);
       Student s3 = new Student("小李",13);
       hashMap.put(s1,"北京");
       hashMap.put(s2,"天津");
       hashMap.put(s3,"上海");
       hashMap.put(new Student("小李",13),"上海");//由于重写了hashcode和equals方法,所以不会添加进去
       /*如果将上行代码,也就是小李的地址改为南京,则最终输出是南京,相当于修改了小李的地址*/
       System.out.println(hashMap.toString());/*输出{Student{name='小明', age=11}=北京, Student{name='小李',
       age=13}=上海, Student{name='小红', age=12}=天津}*/

       //2.删除略
//       hashMap.remove(s3);
//       System.out.println(hashMap.size());//输出2

       //3.遍历
       //3.1使用keySet()方法
       for (Object key:hashMap.keySet()) {
           System.out.println(key.toString()+":"+hashMap.get(key));
      }
       System.out.println("------------------------------------------");
       //3.2使用entrySet()方法
       for (Map.Entry<Object,Object> entry:hashMap.entrySet()) {
           System.out.println(entry.getKey()+":"+entry.getValue());
           
      }
       //4.判断
       System.out.println(hashMap.containsKey(s1));
       System.out.println(hashMap.containsValue("上海"));
       System.out.println(hashMap.isEmpty());
  }
}

 

 

import java.util.Map;
import java.util.TreeMap;

public class TreeMapDemo {
   public static void main(String[] args) {
       TreeMap<Student,String> treeMap = new TreeMap<>();

       //1.添加元素
       Student s1 = new Student("小明",11);
       Student s2 = new Student("小红",12);
       Student s3 = new Student("小李",13);
       treeMap.put(s1,"南京");
       treeMap.put(s2,"上海");
       treeMap.put(s3,"湖南");
       treeMap.put(new Student("小红",18),"天津");
       System.out.println(treeMap.size());//输出需要实现Comparable接口

       //2.删除
//     1. treeMap.remove(s3);
//     2. treeMap.remove(new Student("小红"),18);

       //3.遍历
       //3.1使用keySet()
       for (Student s: treeMap.keySet()) {
           System.out.println(s+":"+treeMap.get(s));
      }
       System.out.println("------------------------------");

       for (Map.Entry<Student,String> entry : treeMap.entrySet()) {
           System.out.println(entry.getKey()+":"+entry.getValue());
      }
  }
}
 

标签:Map,hashMap,System,treeMap,println,-----,Student,集合,out
From: https://www.cnblogs.com/mokuiran/p/16600922.html

相关文章

  • 集合框架6-----Collection工具类
    Collection工具类:packagecom.mokuiran.collection;​importjava.util.ArrayList;importjava.util.Arrays;importjava.util.Collections;importjava.util.List;​/**......
  • 定时任务Quartz、elastic-job、xxl-job对比
    一、概述Quartz:Java上的定时任务标准。但Quartz关注点在于定时任务而非数据,并无一套根据数据处理而定制化的流程。虽然Quartz可以基于数据库实现作业的高可用,但缺少分布......
  • 解题报告——P3477 [POI2008]PER-Permutation
    这道题如果不是任意模数的话还是比较平凡的(这道题的式子其实很好推,根据康托展开的思路,一位一位考虑,只不过是多重集,可能有重复情况,排除即可,每一位的式子为:\[ans_i=\dfrac{......
  • ios开发之--Cocoapods更新指定的库
    最近接手了一个swift的老项目,问题还不少,把bug修复完成功编译后,还没来得及高兴,发现一运行都崩溃,经排查是三方库太旧的原因,但是有些老版本的库又不能全部更新,所以只能更新指......
  • python常用开发函数-生成指定长度随机字符串
    通过random生成随机随机字符串方法一importrandomdefrandom_string(length:int)->str:"""length:指定随机字符串长度"""random_str=''......
  • 解决查询问题-分库分表后
     多维度分片需求,如何解决查询问题? 大家好,我是【架构摆渡人】,一只十年的程序猿。这是分库分表系列的第一篇文章,这个系列会给大家分享很多在实际工作中有用的经验,如果......
  • 2022-08-18 第六小组 高佳誉 学习笔记
    MySQL常用函数聚合函数count:计数。count(*)≈count(1)>count(主键)count(*):MySQL对count(*)底层优化,count(0)。count(1)count(主键)count(字段)min:最小值max:最......
  • JAVA从头学习-2022年8月15日
    总概述1、JAVA是什么是一门高级编程语言2、JAVA是哪家公司研发的,现在属于哪家公司sun,oracle3、Java之父是谁詹姆斯.高斯林......
  • 第九章 - 方法区
    少年最好的地方就是:嘴上说着要放弃,心里却都憋着一口气1.栈、堆、方法区的交互关系从内存结构上看从线程共享与否的角度看栈、堆、方法区的交互关系Person类的.c......
  • PowerShell教程 - PowerShell编辑器(PowerShell editors)
    更新记录转载请注明出处:https://www.cnblogs.com/cqpanda/p/16589935.html2022年8月19日发布。2022年8月15日从笔记迁移到博客。PowerShell编辑器(PowerShelledit......