前言
Java容器,作為最基礎的問題桃煎,無論你是多牛逼的程序員篮幢,都離不開這個技術的使用和總結,但是为迈,作為我們?nèi)粘i_發(fā)工作中最常用的技術之一三椿,你是否真的了解(大佬自動掠過,謝謝)
這是我在日常工作以及學習中總結的關于Java容器的總結葫辐,今天就單講一點:Map
Map 遍歷
我們通過源碼來回想一下
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "a");
map.put(2, "b");
map.put(3, "ab");
map.put(4, "ab");
map.put(4, "ab");// 和上面相同 搜锰, 會自己篩選
System.out.println(map.size());
// 第一種:
System.out.println("第一種:通過Map.keySet遍歷key和value:");
for (Integer in : map.keySet()) {
? ? //map.keySet()返回的是所有key的值
? ? String str = map.get(in);//得到每個key多對用value的值
? ? System.out.println(in + "? ? " + str);
}
// 第二種:
System.out.println("第二種:通過Map.entrySet使用iterator遍歷key和value:");
Iterator<Map.Entry<Integer, String>> it = map.entrySet().iterator();
while (it.hasNext()) {
? ? Map.Entry<Integer, String> entry = it.next();
? ? System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
}
// 第三種:推薦,尤其是容量大時, 效率高
System.out.println("第三種:通過Map.entrySet遍歷key和value");
for (Map.Entry<Integer, 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);
}
2.map的長度:
int size=Map.size();
var length? = Object.keys(map).length ;
3.java? 8? 遍歷
public class LambdaMap {
? ? private Map<String, Object> map = new HashMap<>();
? ? @Before
? ? public void initData() {
? ? ? ? map.put("key1", "value1");
? ? ? ? map.put("key2", "value2");
? ? ? ? map.put("key3", "value3");
? ? ? ? map.put("key4", 4);
? ? ? ? map.put("key5", 5);
? ? ? ? map.put("key5", 'h');
? ? }
? ? /**
? ? * 遍歷Map的方式一
? ? * 通過Map.keySet遍歷key和value
? ? */
? ? @Test
? ? public void testErgodicWayOne() {
? ? ? ? System.out.println("---------------------Before JAVA8 ------------------------------");
? ? ? ? for (String key : map.keySet()) {
? ? ? ? ? ? System.out.println("map.get(" + key + ") = " + map.get(key));
? ? ? ? }
? ? ? ? System.out.println("---------------------JAVA8 ------------------------------");
? ? ? ? map.keySet().forEach(key -> System.out.println("map.get(" + key + ") = " + map.get(key)));
? ? }
? ? /**
? ? * 遍歷Map第二種
? ? * 通過Map.entrySet使用Iterator遍歷key和value
? ? */
? ? @Test
? ? public void testErgodicWayTwo() {
? ? ? ? System.out.println("---------------------Before JAVA8 ------------------------------");
? ? ? ? Iterator<Map.Entry<String, Object>> iterator = map.entrySet().iterator();
? ? ? ? while (iterator.hasNext()) {
? ? ? ? ? ? Map.Entry<String, Object> entry = iterator.next();
? ? ? ? ? ? System.out.println("key:value = " + entry.getKey() + ":" + entry.getValue());
? ? ? ? }
? ? ? ? System.out.println("---------------------JAVA8 ------------------------------");
? ? ? ? map.entrySet().iterator().forEachRemaining(item -> System.out.println("key:value=" + item.getKey() + ":" + item.getValue()));
? ? }
? ? /**
? ? * 遍歷Map第三種
? ? * 通過Map.entrySet遍歷key和value蛋叼,在大容量時推薦使用
? ? */
? ? @Test
? ? public void testErgodicWayThree() {
? ? ? ? System.out.println("---------------------Before JAVA8 ------------------------------");
? ? ? ? for (Map.Entry<String, Object> entry : map.entrySet()) {
? ? ? ? ? ? System.out.println("key:value = " + entry.getKey() + ":" + entry.getValue());
? ? ? ? }
? ? ? ? System.out.println("---------------------JAVA8 ------------------------------");
? ? ? ? map.entrySet().forEach(entry -> System.out.println("key:value = " + entry.getKey() + ":" + entry.getValue()));
? ? }
? ? /**
? ? * 遍歷Map第四種
? ? * 通過Map.values()遍歷所有的value,但不能遍歷key
? ? */
? ? @Test
? ? public void testErgodicWayFour() {
? ? ? ? System.out.println("---------------------Before JAVA8 ------------------------------");
? ? ? ? for (Object value : map.values()) {
? ? ? ? ? ? System.out.println("map.value = " + value);
? ? ? ? }
? ? ? ? System.out.println("---------------------JAVA8 ------------------------------");
? ? ? ? map.values().forEach(System.out::println); // 等價于map.values().forEach(value -> System.out.println(value));
? ? }
? ? /**
? ? * 遍歷Map第五種
? ? * 通過k,v遍歷剂陡,Java8獨有的
? ? */
? ? @Test
? ? public void testErgodicWayFive() {
? ? ? ? System.out.println("---------------------Only JAVA8 ------------------------------");
? ? ? ? map.forEach((k, v) -> System.out.println("key:value = " + k + ":" + v));
? ? }
}
這里展示了5種map的遍歷方式狈涮,也是我在日常工作過程中經(jīng)常使用的幾種技術,在這里總結給大家鸭栖,可能針對新人會更加有用一些歌馍,已經(jīng)非常熟練的朋友,就當作是回顧一下吧
最后
學習的路上從來沒有盡頭這一說晕鹊,需要不斷地努力骆姐、進步,才能不被時代所篩選
需要我整理的知識體系的捏题,關注+轉(zhuǎn)發(fā)后玻褪,私信“資料”即可查看獲取方式