HashMap遍历和stream流
HashMap的遍历方式及优缺点
Map.Entry的定义
Map的entrySet()方法返回一个实现Map.Entry接口的对象集合。集合中每个对象都是底层Map中一个特定的键/值对。通过这个集合的迭代器,获得每一个条目(唯一获取方式)的键或值并对值进行更改。Map.Entry中的常用方法如下所示:
(1) Object getKey(): 返回条目的关键字
(2) Object getValue(): 返回条目的值
(3) Object setValue(Object value): 将相关映像中的值改为value,并且返回旧值
Map.Entry的作用
Map.Entry是为了更方便的输出map键值对。一般情况下,要输出Map中的key 和 value 是先得到key的集合keySet(),然后再迭代(循环)由每个key得到每个value。values()方法是获取集合中的所有值,不包含键,没有对应关系。而Entry可以一次性获得这两个值。
常用的遍历Map的方法
package com.example.demo;
import org.junit.Test;
import java.util.*;
import java.util.jar.JarOutputStream;
import java.util.stream.Collectors;
/**
* @author Jia
* @date 2022/1/18 10:15
*/
public class HashTest {
//1、第一种方式:(效率比较高:只遍历了一次,把key和value都放在了entry中)
@Test
public void test1(){
HashMap<String,Object> map = new HashMap<>();
map.put("一","测试1");
map.put("二","测试2");
map.put("三","测试3");
map.put("四","测试4");
map.put("三","测试重复key");
map.put("","");
//1.使用迭代器遍历 ,效率最高
Iterator<Map.Entry<String, Object>> iterator = map.entrySet().iterator();
while(iterator.hasNext()) {
Map.Entry<String, Object> entry = iterator.next();
System.out.print("key="+entry.getKey());
System.out.println("value="+entry.getValue());
}
}
//1、foreach 是java5的新特性之一,在遍历数组,集合方面有很大用处。foreach不是一个关键词,而是把增强型的for语句称为foreach语句。
@Test
public void test2(){
HashMap<String,Object> map = new HashMap<>();
map.put("一","测试1");
map.put("二","测试2");
map.put("三","测试3");
map.put("四","测试4");
map.put("三","测试重复key");
map.put("","");
//2.使用for循环进行遍历,需要二次取值,取到下标值。所以效率较低
for (Map.Entry<String, Object> entry : map.entrySet()) {
System.out.print("key="+entry.getKey());
System.out.println("value="+entry.getValue());
}
}
//2、第二种方式:(效率较低:keySet其实是遍历了2遍,一次转换成iterator,一次从hasgmap中取出Key所对于的value.
@Test
public void test3(){
HashMap<String,Object> map = new HashMap<>();
map.put("一","测试1");
map.put("二","测试2");
map.put("三","测试3");
map.put("四","测试4");
map.put("三","测试重复key");
map.put("","");
//3.使用迭代器执行查询,但keySet需要执行两次,效率也不高
Iterator<String> iterator = map.keySet().iterator();
while(iterator.hasNext()){
String next = iterator.next();
Object value = map.get(next);
System.out.println(next);
System.out.println(value);
}
}
//对map的value进行排序使用lambda表达式
@Test
public void test4(){
HashMap<String,Integer> map = new HashMap<>();
map.put("一",1);
map.put("二",2);
map.put("三",2);
map.put("四",4);
map.put("三",6);
map.put("", 8);
List<Map.Entry<String,Integer>> listMap = new ArrayList<>();
listMap.addAll(map.entrySet());
//比较
/*Collections.sort(listMap, new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
return o1.getValue()-o2.getValue();
}
});*/
Collections.sort(listMap,(o1,o2) ->o1.getValue()-o2.getValue());
listMap.forEach(m -> System.out.println(m.getKey()));
}
//将map集合转换为stream流进行操作
//要求:过滤map为空的值、map的建进行条件筛选、
@Test
public void streamTest() {
HashMap<String, Integer> map = new HashMap<>();
map.put("张三", 16);
map.put("里斯", 1);
map.put("李四", 22);
map.put("王五", 99);
map.put("阿斯", 100);
map.put("阿三", -3);
//collect生成新的map
Map<String, Integer> collect = map.entrySet().stream().filter(item -> checkValue(item) && item.getValue() > 1).collect(Collectors.toMap(a -> a.getKey(), b -> b.getValue()));
collect.entrySet().forEach(item -> System.out.println(item.getKey() + "=" + item.getValue()));
}
private static boolean checkValue(Object object) {
if (object instanceof String && "".equals(object)) {
return false;
}
if (null == object) {
return false;
}
return true;
}
//instanceof关键字作用:判断一个对象是否属于一个类
@Test
public void test5(){
String a="年后";
boolean b = a instanceof String;
System.out.println("字符串="+b);
}
}