一
package map;
import java.util.HashMap;
import java.util.Map;
public class MapDemo01 {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("张三", 23);
Integer i1 = map.put("张三", 23);
map.put("李四", 24);
map.put("王五", 25);
map.put("赵六", 26);
System.out.println(map);
System.out.println(i1); // 相同的键不存储,把覆盖的键值value返回
}
}
输出结果如下:
{李四=24, 张三=23, 王五=25, 赵六=26}
23
二
package map;
import java.util.HashMap;
import java.util.Map;
public class MapDemo02 {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("张三", 23);
map.put("李四", 24);
map.put("王五", 25);
map.put("赵六", 26);
Integer value = map.remove("张三");
System.out.println(value); // 23
System.out.println(map); // {赵六=26, 李四=24, 王五=25}
}
}
输出结果如下:
23
{李四=24, 王五=25, 赵六=26}
三
package map;
import java.util.HashMap;
import java.util.Map;
public class MapDemo03 {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("张三", 23);
map.put("李四", 24);
map.put("王五", 25);
map.put("赵六", 26);
System.out.println(map.containsKey("张三")); // true
System.out.println(map.containsValue(23)); // true
System.out.println(map); // {赵六=26, 张三=23, 李四=24, 王五=25}
}
}
输出结果如下:
true
true
{李四=24, 张三=23, 王五=25, 赵六=26}
四
package map;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
public class MapDemo04 {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("张三", 23);
map.put("李四", 24);
map.put("王五", 25);
map.put("赵六", 26);
Collection<Integer> c = map.values();
System.out.println(c); // 获取集合中所有值的集合 [26, 23, 24, 25]
System.out.println(map.size()); // 4
}
}
输出结果如下:
[24, 23, 25, 26]
4
五
package map;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/*
* 遍历:
* 1、键找值
* 2、键值对象找键和值
*/
public class HashMapDemo01 {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("1", "value1");
map.put("2", "value2");
map.put("3", "value3");
map.put("4", "value4");
// 1.键找值遍历
Set<String> set1 = map.keySet();
for (String key : set1) {
String value = map.get(key);
System.out.println(key + "——" + value);
}
System.out.println("-----------------");
// 2.键值对象找键和值遍历
Set<Map.Entry<String, String>> set2 = map.entrySet();
for (Map.Entry<String, String> me : set2) {
System.out.println(me.getKey() + "——" + me.getValue());
}
}
}
输出结果如下:
1——value1
2——value2
3——value3
4——value4
-----------------
1——value1
2——value2
3——value3
4——value4
六
package map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class HashMapDemo02 {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("1", "value1");
map.put("2", "value2");
map.put("3", "value3");
//第一种:普遍使用,二次取值
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());
}
//第四种:通过Map.values()遍历所有的value,但不能遍历key
System.out.println("第四种:通过Map.values()遍历所有的value,但不能遍历key");
for (String v : map.values()) {
System.out.println("value= " + v);
}
}
}
输出结果如下:
第一种:通过Map.keySet遍历key和value:
key= 1 and value= value1
key= 2 and value= value2
key= 3 and value= value3
第二种:通过Map.entrySet使用iterator遍历key和value:
key= 1 and value= value1
key= 2 and value= value2
key= 3 and value= value3
第三种:通过Map.entrySet遍历key和value
key= 1 and value= value1
key= 2 and value= value2
key= 3 and value= value3
第四种:通过Map.values()遍历所有的value,但不能遍历key
value= value1
value= value2
value= value3
七
package map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* Map集合遍历的五种方式
*/
public class HashMapDemo03 {
public static void main(String[] args) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("1", "中国");
map.put("2", "美国");
map.put("3", "俄罗斯");
map.put("4", "英国");
map.put("5", "法国");
System.out.println("第一种:通过Map.KeySet遍历key和value");
for (String key : map.keySet()) {
String value = map.get(key);
System.out.println("key: " + key + " value: " + value);
}
System.out.println("------------------------------------");
System.out.println("第二种:通过Map.entrySet使用iterator遍历key和value");
Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> entry = iterator.next();
System.out.println("key: " + entry.getKey() + " value: " + entry.getValue());
}
System.out.println("------------------------------------");
System.out.println("第三种:通过Map.entrySet遍历key和value");
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println("key: " + entry.getKey() + " value: " + entry.getValue());
}
System.out.println("------------------------------------");
System.out.println("第四种:通过Map.values()遍历value,无法遍历key");
for (String value : map.values()) {
System.out.println(" value: " + value);
}
System.out.println("------------------------------------");
System.out.println("第五种:通过Map.keySet()key,无法遍历value");
for (String key : map.keySet()) {
System.out.println(" key: " + key);
}
System.out.println("------------------------------------");
}
}
输出结果如下:
第一种:通过Map.KeySet遍历key和value
key: 1 value: 中国
key: 2 value: 美国
key: 3 value: 俄罗斯
key: 4 value: 英国
key: 5 value: 法国
------------------------------------
第二种:通过Map.entrySet使用iterator遍历key和value
key: 1 value: 中国
key: 2 value: 美国
key: 3 value: 俄罗斯
key: 4 value: 英国
key: 5 value: 法国
------------------------------------
第三种:通过Map.entrySet遍历key和value
key: 1 value: 中国
key: 2 value: 美国
key: 3 value: 俄罗斯
key: 4 value: 英国
key: 5 value: 法国
------------------------------------
第四种:通过Map.values()遍历value,无法遍历key
value: 中国
value: 美国
value: 俄罗斯
value: 英国
value: 法国
------------------------------------
第五种:通过Map.keySet()key,无法遍历value
key: 1
key: 2
key: 3
key: 4
key: 5
------------------------------------