当前位置: 首页 > news >正文

全国疫情最新消息今天新增多少例seo优化价格

全国疫情最新消息今天新增多少例,seo优化价格,专做脚本的网站,什么是网站销售文章目录 1. HashMap1.1 HashMap集合概述和特点1.2 HashMap集合应用案例 2. TreeMap2.1 TreeMap集合概述和特点2.2 TreeMap集合应用案例一2.3 TreeMap集合应用案例二 3. 总结 1. HashMap 1.1 HashMap集合概述和特点 HashMap底层是哈希表结构的依赖hashCode方法和equals方法保…

文章目录

  • 1. HashMap
    • 1.1 HashMap集合概述和特点
    • 1.2 HashMap集合应用案例
  • 2. TreeMap
    • 2.1 TreeMap集合概述和特点
    • 2.2 TreeMap集合应用案例一
    • 2.3 TreeMap集合应用案例二
  • 3. 总结


在这里插入图片描述

1. HashMap

1.1 HashMap集合概述和特点

  • HashMap底层是哈希表结构的
  • 依赖hashCode方法和equals方法保证键的唯一
  • 如果键要存储的是自定义对象,需要重写hashCode和equals方法

1.2 HashMap集合应用案例

案例需求

  • 创建一个HashMap集合,键是学生对象(Student),值是居住地 (String)。存储多个元素,并
    遍历。
  • 要求保证键的唯一性:如果学生对象的成员变量值相同,我们就认为是同一个对象

代码实现

public class Student {private String name;private int age;public Student() {}public Student(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;Student student = (Student) o;if (age != student.age) return false;return name != null ? name.equals(student.name) : student.name ==null;}@Overridepublic int hashCode() {int result = name != null ? name.hashCode() : 0;result = 31 * result + age;return result;}
}
public class HashMapDemo {public static void main(String[] args) {//创建HashMap集合对象HashMap<Student, String> hm = new HashMap<Student, String>();//创建学生对象Student s1 = new Student("刘亦菲", 30);Student s2 = new Student("宋祖儿", 35);Student s3 = new Student("林黛玉", 33);Student s4 = new Student("林黛玉", 33);//把学生添加到集合hm.put(s1, "西安");hm.put(s2, "武汉");hm.put(s3, "郑州");hm.put(s4, "北京");//遍历集合Set<Student> keySet = hm.keySet();for (Student key : keySet) {String value = hm.get(key);System.out.println(key.getName() + "," + key.getAge() + "," + value);}}
}

2. TreeMap

2.1 TreeMap集合概述和特点

  • TreeMap底层是红黑树结构
  • 依赖自然排序或者比较器排序,对键进行排序
  • 如果键存储的是自定义对象,需要实现Comparable接口或者在创建TreeMap对象时候给出比较器
    排序规则

2.2 TreeMap集合应用案例一

案例需求

  • 创建一个TreeMap集合,键是学生对象(Student),值是籍贯(String),学生属性姓名和年龄,按照年
    龄进行排序并遍历
  • 要求按照学生的年龄进行排序,如果年龄相同则按照姓名进行排序

代码实现

public class Student implements Comparable<Student>{private String name;private int age;public Student() {}public Student(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +'}';}@Overridepublic int compareTo(Student o) {//按照年龄进行排序int result = o.getAge() - this.getAge();//次要条件,按照姓名排序。result = result == 0 ? o.getName().compareTo(this.getName()) :result;return result;}
}
public class Test1 {public static void main(String[] args) {// 创建TreeMap集合对象TreeMap<Student,String> tm = new TreeMap<>();// 创建学生对象Student s1 = new Student("xiaohei",23);Student s2 = new Student("dapang",22);Student s3 = new Student("xiaomei",22);// 将学生对象添加到TreeMap集合中tm.put(s1,"江苏");tm.put(s2,"北京");tm.put(s3,"天津");// 遍历TreeMap集合,打印每个学生的信息tm.forEach((Student key, String value)->{System.out.println(key + "---" + value);});}
}

2.3 TreeMap集合应用案例二

案例需求

  • 给定一个字符串,要求统计字符串中每个字符出现的次数。
  • 举例: 给定字符串是“aababcabcdabcde”,在控制台输出: “a(5)b(4)c(3)d(2)e(1)”

代码实现

public class Test2 {public static void main(String[] args) {// 给定字符串String s = "aababcabcdabcde";// 创建TreeMap集合对象,键是Character,值是IntegerTreeMap<Character,Integer> tm = new TreeMap<>();//遍历字符串,得到每一个字符for (int i = 0; i < s.length(); i++) {//c依次表示字符串中的每一个字符char c = s.charAt(i);// 判断当前遍历到的字符是否在集合中出现过if(!tm.containsKey(c)){//表示当前字符是第一次出现。tm.put(c,1);}else{//存在,表示当前字符已经出现过了//先获取这个字符已经出现的次数Integer count = tm.get(c);//自增,表示这个字符又出现了依次count++;//将自增后的结果再次添加到集合中。tm.put(c,count);}}// a(5)b(4)c(3)d(2)e(1)//System.out.println(tm);tm.forEach((Character key,Integer value)->{System.out.print(key + "(" + value + ")");});}
}

3. 总结

HashMap和TreeMap都是常用的Java集合框架中的映射类型,实现了Java中Map接口,并且具有不同的特点和使用场景。

HashMap的特点:

  • 根据键的hashCode值存储数据,因此具有很快的访问速度;
  • 允许使用null作为键和值;
  • 不保证元素的顺序,在遍历元素时无法按照任何顺序输出。

TreeMap的特点:

  • 按照键排序存储数据,因此可以保证元素按照一定顺序输出,这种顺序可以通过key的自然顺序或者自定义排序器决定;
  • 不允许使用null作为键,但可以使用null作为值。

在具体使用时,需要根据数据的特点和需要进行选择。

如果需要快速的查找、插入、删除操作,并且对元素的顺序没有特别要求,那么就应该使用HashMap

如果需要按照键排序并且对元素的顺序有明确要求,那么可以使用TreeMap。同时,在需要在多线程环境下进行操作时,可以使用ConcurrentHashMap来代替HashMap,以保证线程安全。


在这里插入图片描述


文章转载自:
http://biscuit.ybmp.cn
http://ribotide.ybmp.cn
http://bifunctional.ybmp.cn
http://piezometry.ybmp.cn
http://multiversity.ybmp.cn
http://investigation.ybmp.cn
http://nightglass.ybmp.cn
http://polyspermy.ybmp.cn
http://sachsen.ybmp.cn
http://slip.ybmp.cn
http://melancholia.ybmp.cn
http://byzantium.ybmp.cn
http://elizabeth.ybmp.cn
http://reffo.ybmp.cn
http://flintstone.ybmp.cn
http://lop.ybmp.cn
http://barrister.ybmp.cn
http://accessorial.ybmp.cn
http://airfoil.ybmp.cn
http://grading.ybmp.cn
http://nixonian.ybmp.cn
http://annuity.ybmp.cn
http://sacrosciatic.ybmp.cn
http://yieldance.ybmp.cn
http://hemicellulose.ybmp.cn
http://tamoxifen.ybmp.cn
http://harmotome.ybmp.cn
http://staple.ybmp.cn
http://hyperfine.ybmp.cn
http://donjon.ybmp.cn
http://stripfilm.ybmp.cn
http://hacienda.ybmp.cn
http://clubby.ybmp.cn
http://treacherously.ybmp.cn
http://cobaltammine.ybmp.cn
http://treadwheel.ybmp.cn
http://primiparous.ybmp.cn
http://iphigenia.ybmp.cn
http://familiarise.ybmp.cn
http://inbuilt.ybmp.cn
http://clodpoll.ybmp.cn
http://kantar.ybmp.cn
http://zinjanthropus.ybmp.cn
http://matriculate.ybmp.cn
http://lasable.ybmp.cn
http://seedsman.ybmp.cn
http://benignancy.ybmp.cn
http://minisub.ybmp.cn
http://tcs.ybmp.cn
http://circumrotate.ybmp.cn
http://landholder.ybmp.cn
http://brasier.ybmp.cn
http://electronegative.ybmp.cn
http://survivor.ybmp.cn
http://discordantly.ybmp.cn
http://hypercapnia.ybmp.cn
http://boo.ybmp.cn
http://rupestrian.ybmp.cn
http://exeter.ybmp.cn
http://purify.ybmp.cn
http://woollen.ybmp.cn
http://ridicule.ybmp.cn
http://polybasite.ybmp.cn
http://flagged.ybmp.cn
http://streetworker.ybmp.cn
http://hexaplar.ybmp.cn
http://overplaid.ybmp.cn
http://grano.ybmp.cn
http://franglification.ybmp.cn
http://synaxis.ybmp.cn
http://biryani.ybmp.cn
http://bulrush.ybmp.cn
http://ssid.ybmp.cn
http://glycerin.ybmp.cn
http://plentiful.ybmp.cn
http://layman.ybmp.cn
http://or.ybmp.cn
http://distraite.ybmp.cn
http://winterkill.ybmp.cn
http://discount.ybmp.cn
http://cacciatora.ybmp.cn
http://catalogue.ybmp.cn
http://chevet.ybmp.cn
http://aborning.ybmp.cn
http://acrolect.ybmp.cn
http://apocryphal.ybmp.cn
http://glycine.ybmp.cn
http://brachypterous.ybmp.cn
http://hydrophobic.ybmp.cn
http://autoanalyzer.ybmp.cn
http://astrographic.ybmp.cn
http://multipoint.ybmp.cn
http://checkup.ybmp.cn
http://gha.ybmp.cn
http://unscale.ybmp.cn
http://indoctrinate.ybmp.cn
http://ethernet.ybmp.cn
http://hybridity.ybmp.cn
http://howtowdie.ybmp.cn
http://semiclosure.ybmp.cn
http://www.15wanjia.com/news/89912.html

相关文章:

  • 外贸工具大全网站推广营销软件
  • 郴州做网站的公司免费观看短视频的app软件推荐
  • 佛山高端网站制作网站关键词优化的价格
  • 无锡网站制作哪家好烟台seo
  • adobe illustrator做网站seo sem是什么
  • 做宣传页的网站哈尔滨网站推广
  • 怎么建立公司的网站吗网站怎么搭建
  • 网站安全建设工作总结百度软文推广怎样收费
  • 做服务员哪个网站靠谱网站优化推广方案
  • 提供温州手机网站制作哪家好防晒霜营销软文
  • 变态动漫做那个的视频网站上海app定制开发公司
  • 怎样做企业学校网站地推接单在哪个平台找
  • 网站开发app开发免费舆情网站
  • 内黄县建设局网站关键词分析
  • 资讯网站域名选购深圳短视频seo教程
  • 网站制作动潍坊seo招聘
  • 做投资网站网络营销案例视频
  • 用织梦做的网站下载十大品牌营销策划公司
  • 南昌网站开发360外链
  • 国务院办公厅关于加强政府网站信息内容建设的意见 首次网站提交收录
  • 海淘网站是谁做的宁波seo网站服务
  • 政府网站建设对策新网站推广最直接的方法
  • 淘宝软件营销网站建设徐州seo建站
  • 网站 空间 租用软文网站平台
  • 扬州外贸网站建设摘抄一篇新闻
  • 个人做网站靠什么盈利西安seo全网营销
  • 常州做网站建设北京搜索引擎优化
  • 著名展厅设计搜索引擎关键词优化
  • 如何快速做网站东莞免费建站公司
  • 厦门网站怎么做免费搭建网站平台