package com.shrimpking.t15;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2024/9/10 21:06
*/
public class UseHashMap
{
public static void main(String[] args)
{
Map<Integer,String> map = new HashMap<>();
map.put(1,"one");
map.put(2,"two");
map.put(3,"three");
map.put(4,"four");
System.out.println(map);
System.out.printf("key=%d, value=%s%n",3,map.get(3));
Set<Integer> keys = map.keySet();
System.out.println("映射中的键:");
for (Integer key : keys){
System.out.printf("%d\t",key);
}
System.out.println();
Collection<String> values = map.values();
System.out.println("映射中的值:");
for (String value : values){
System.out.printf("%s\t",value);
}
System.out.println();
System.out.println("映射中的键值有:");
Set<Map.Entry<Integer,String>> entries = map.entrySet();
for (Map.Entry entry : entries){
System.out.printf("%d:%s\t",entry.getKey(),entry.getValue());
}
System.out.println();
System.out.println("遍历映射中的键值对:");
map.forEach((k,v)-> System.out.printf("%d:%s\t",k,v));
}
}
UseHashMap
最新推荐文章于 2024-11-16 22:34:09 发布