import java.util.Map;
import java.util.Properties;
import java.util.Set;
/**
* properties(map)增.删.改.查.遍历
*/
public class Demo1 {
public static void main(String[] args) {
Properties prop = new Properties();
System.out.println("添加:添加一个键值对");
prop.put("a", "aaa");
prop.put("b", "bbb");
prop.put("c", "ccc");
prop.put("d", "ddd");
System.out.println(prop);
System.out.println();
System.out.println();
System.out.println("删除:根据指定的键删除对应的键值对");
prop.remove("a");
System.out.println("删除a的键之后:" + prop);
System.out.println();
System.out.println();
System.out.println("改:根据键,去修改对应的值");
prop.put("b", "java");
System.out.println("修改b的值" + prop);
System.out.println();
System.out.println();
System.out.println("查:根据key 获得value");
Object value = prop.get("c");
System.out.println("获得键C的值: " + value);
System.out.println();
System.out.println();
System.out.println("获得所有的键,根据键遍历");
Set<Object> keySet = prop.keySet();
for (Object key : keySet) {
Object kevalue = prop.get(key);
System.out.println(key + "=======" + kevalue);
}
System.out.println();
System.out.println();
System.out.println("获得所有的键值对进行遍历");
Set<Map.Entry<Object, Object>> entries = prop.entrySet();
for (Map.Entry<Object, Object> entry : entries) {
Object ek = entry.getKey();
Object ev = entry.getValue();
System.out.println(ek + "=====" + ev);
}
}
}
properties(map)增.删.改.查.遍历
最新推荐文章于 2022-04-02 15:06:50 发布