首页 技术 正文
技术 2022年11月23日
0 收藏 470 点赞 4,725 浏览 2905 个字

参考http://how2j.cn/k/collection/collection-hashmap/365.html

HashMap的键值对

HashMap储存数据的方式是—— 键值对

package collection;import java.util.HashMap;public class TestCollection {
public static void main(String[] args) {
HashMap<String,String> dictionary = new HashMap<>();
dictionary.put("adc", "物理英雄");
dictionary.put("apc", "魔法英雄");
dictionary.put("t", "坦克"); System.out.println(dictionary.get("t"));
}
}

键不能重复,值可以重复

对于HashMap而言,key是唯一的,不可以重复的。
所以,以相同的key 把不同的value插入到 Map中会导致旧元素被覆盖,只留下最后插入的元素。

不过,同一个对象可以作为值插入到map中,只要对应的key不一样

遍历HashMap几种方式

//第一种:普遍使用,二次取值
System.out.println("通过Map.keySet遍历key和value:");
for (String key : map.keySet()) {
System.out.println("key= "+ key + " and value= " + map.get(key));
} //第二种
System.out.println("通过Map.entrySet使用iterator遍历key和value:");
Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> entry = it.next();
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
} //第三种:推荐,尤其是容量大时
System.out.println("通过Map.entrySet遍历key和value");
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
} //第四种
System.out.println("通过Map.values()遍历所有的value,但不能遍历key");
for (String v : map.values()) {
System.out.println("value= " + v);
}
}

查找内容性能比较

准备一个ArrayList其中存放3000000(三百万个)Hero对象,其名称是随机的,格式是hero-[4位随机数]
hero-3229
hero-6232
hero-9365

因为总数很大,所以几乎每种都有重复,把名字叫做 hero-5555的所有对象找出来
要求使用两种办法来寻找
1. 不使用HashMap,直接使用for循环找出来,并统计花费的时间
2. 借助HashMap,找出结果,并统计花费的时间

package swordOffer;import java.util.*;public class test {
public static void main(String[] args) {
ArrayList<Hero> arrayList=new ArrayList<>();
//初始化
for (int i=;i<;i++){
Hero hero=new Hero();
String str=String.valueOf(Math.random()*+).substring(,);
hero.setName("hero-"+str);
arrayList.add(hero);
} test(arrayList,"hero-5555");
testHashMap(arrayList,"hero-5555"); } private static void test(ArrayList<Hero> arrayList,String key){ long timeBegin=System.currentTimeMillis();
int count=;
for (Hero hero:arrayList){
if(hero.getName().equals(key)){
count++;
}
}
long timeEnd=System.currentTimeMillis();
System.out.println("没有使用HashMap:ArrayList其中存放3000000(三百万个)Hero对象,其名称是随机的,格式是hero-[4位随机数],把名字叫做 hero-5555的所有对象找出来用时:"+(timeEnd-timeBegin)+"毫秒;共有"+count+"个hero-5555");
} private static void testHashMap(ArrayList<Hero> arrayList,String key){ long timeBegin=System.currentTimeMillis();
HashMap<String,ArrayList<Hero>> resultList=new HashMap<>();
for (Hero hero:arrayList){
if(resultList.get(hero.getName())!=null){
ArrayList list=resultList.get(hero.getName());
resultList.put(hero.getName(),list);
}
       list.add(hero);
}
long timeEnd=System.currentTimeMillis();
System.out.println("使用HashMap:ArrayList其中存放3000000(三百万个)Hero对象,其名称是随机的,格式是hero-[4位随机数],把名字叫做 hero-5555的所有对象找出来用时:"+(timeEnd-timeBegin)+"毫秒;共有"+resultList.get(key).size()+"个hero-5555");
}
}class Hero { private String name; public Hero(){} public Hero(String name){
name=name;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}}class ADHero extends Hero {}

java集合框架之HashMap

由此可见,不使用HashMap,只用for循环 比 使用HashMap 快12倍

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,291
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,718
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,554
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,324
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,962
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,123