HashMap遍历
基本概念
Map 接口的实现类有 TreeMap和 HashMap 等。Map 接口类用于存储键(key)-值(value) 对,Map 类中存储的键-值对利用键来标识,所以键值不能重复。
代码上路
键-值对: 1:1(1对1)
public class HashMapTry {
public static void main(String[] args) {
// 基本数据类型
long a = 3234;
/* 预期效果是 3254转化 int --> Integer(类) --> Long(类)
实际效果是 3254转化 int --> Integer(Integer Long 二者之间没有继承关系,注意注意)
常用的一句话: 鱼和熊掌不可兼得.
Long s = 3254; // 报错
*/
// 1:1 (1对1的情况)
// 使用人数 map里存放的必须是引用类型
// 假如你存放了基础类型,抱歉会报错
Map<String, Long> map = new HashMap<>();
// 随便取的
//如果存在任意原始值,它就默认将对象转换为表示它的原始值;
//对象是复合值,而大多数对象无法真正表示为一个原始值,
//因此默认的valueOf()方法简单地返回对象本身,
//而不是返回一个原始值
map.put("网易云音乐", Long.valueOf(23425432));
map.put("QQ", Long.valueOf(23536563));
map.put("360", Long.valueOf(22222222));
//常用的是获得key值的,或是得到key+value值
Set<String> keyset = map.keySet();
System.out.println("--得到key+value值 --");
System.out.println("---1. 迭代获取---");
System.out.println("--- keyset() + get---");
//将keyset在塞进迭代器中
Iterator<String> it = keyset.iterator();
while(it.hasNext()){
String key = it.next();
Long value = map.get(key);
System.out.println(key + "---" + value);
}
System.out.println("---foreach---");
// foreach 其底层利用迭代器实现的
for (String key : keyset) {
Long value = map.get(key);
System.out.println(key + "---" + value);
}
System.out.println("--entrySet()实现--");
System.out.println("---2. 迭代获取---");
Set<Entry<String, Long>> entry = map.entrySet();
Iterator<Entry<String, Long>> it1 = entry.iterator();
while (it1.hasNext()) {
// 获取每个节点的key和value值
Entry<String, Long> gEntry = it1.next();
System.out.println(gEntry.getKey() + "---" + gEntry.getValue());
}
System.out.println("---foreach---");
for (Entry<String, Long> entry2 : entry) {
System.out.println(entry2.getKey() + "---" + entry2.getValue());
}
}
}
键-值对: 1:N(1对多)
面对多值的情况下,需要创建一个SearchHashMap类:
public class SearchHashMap {
// 计数
private Integer count;
//
private String title;
//空构造也要加的, 比如反射,spring要用到的
// 养成好习惯
public SearchHashMap() {
super();
// TODO Auto-generated constructor stub
}
public SearchHashMap(String title, Integer count) {
super();
this.count = count;
this.title = title;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
测试代码(HashMapTry1 类)
// 1:N(1对多)
public class HashMapTry1 {
public static void main(String[] args) {
// 准备的字符串数据
// 利用map查找is关键字出现的次数,当然也可以是其他的关键字
String mString = "But human life is also the moment of cosmic dust across the sky, not to mention everything is leaving, as the changing of the Star, things change.";
Map<String, SearchHashMap> map = new HashMap<>();
// 第一个\代表的是转移字符
// \s+ 代表一个以上的空格
String[] letter = mString.split("\\s+");
for (String title : letter) {
// 首先map没有赋初值的,
//下面的代码大意先在map中找对应key
// 通过key得到类
SearchHashMap value = map.get(title);
// 为空则,添加
if (null == value) {
map.put(title, new SearchHashMap(title, 1));
}else{
// 存在,则计数器+1
value.setCount(value.getCount()+1);
}
}
// is出现的次数
SearchHashMap searchHashMap = map.get("is");
if (null != letter) {
System.out.println(searchHashMap.getCount());
}else{
// 不让它抛异常,文字更加友好点
System.out.println("没有你要找的单词哦");
}
}
}
结果为:
4
干货满满
HashMap: 从一堆中快速定位一个对象,底层:数组+链表
增: put(k,v)
遍历: keySet +get entrySet+getKey|getValue
删:remove
改: put(k,v)
查: size get(K)