本文已参与「新人创作礼」活动,一起开启掘金创作之路。
Java集合中,Map与Collection集合类和数组不同,其存储的是具有映射关系或者是键值对的元素。这里的元素指的是两组数据:key和value,它们都可以使任何引用类型的数据,但注意:key不能重复。这样的话就可以通过指定的key就可以取出对应的value。(Map非常重要的特征)。Map接口有四个比较重要的实现类,分别是HashMap、LinkedHashMap、TreeMap和HashTable。
接下来介绍几种Map遍历的方式,所谓温故而知新。
1.通过map.entrySet()获取到的set集合来遍历Map
package com.tct.sms.mtf;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* @author wl
* @date 2022/5/19
*/
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class AppTest {
@Test
public void test1() {
Map<String, String> map = new HashMap<>(16);
map.put("1", "a");
map.put("2", "b");
map.put("3", "c");
for (Map.Entry<String, String> entry : map.entrySet()) {
log.info("key: {},value: {}", entry.getKey(), entry.getValue());
}
}
}
运行结果:
2.通过迭代器遍历Map
package com.tct.sms.mtf;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* @author wl
* @date 2022/5/19
*/
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class AppTest {
@Test
public void test1() {
Map<String, String> map = new HashMap<>(16);
map.put("1", "a");
map.put("2", "b");
map.put("3", "c");
Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> entry = iterator.next();
log.info("key: {},value: {}", entry.getKey(), entry.getValue());
}
}
}
运行结果:
3.Java8新特性遍历Map
值得注意的是,这种方法无法改变变量的值,会提示Variable used in lambda expression should be final or effectively final
package com.tct.sms.mtf;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* @author wl
* @date 2022/5/19
*/
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class AppTest {
@Test
public void test1() {
Map<String, String> map = new HashMap<>(16);
map.put("1", "a");
map.put("2", "b");
map.put("3", "c");
map.forEach((key, value) -> {
log.info("key: {},value: {}", key, value);
});
}
}
运行结果:
4.通过键值遍历,这种方式的效率比较低,因为本身从键取值是耗时的操作
package com.tct.sms.mtf;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* @author wl
* @date 2022/5/19
*/
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class AppTest {
@Test
public void test1() {
Map<String, String> map = new HashMap<>(16);
map.put("1", "a");
map.put("2", "b");
map.put("3", "c");
for (String key : map.keySet()) {
String value = map.get(key);
log.info("key: {},value: {}", key, value);
}
}
}
运行结果: