|
@@ -0,0 +1,178 @@
|
|
|
+package com.sf.javase.day16;
|
|
|
+
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.*;
|
|
|
+import java.util.function.BiConsumer;
|
|
|
+import java.util.function.BiFunction;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Map
|
|
|
+ *
|
|
|
+ */
|
|
|
+public class T {
|
|
|
+ @Test
|
|
|
+ public void t1(){
|
|
|
+ Map<String,String> map = new HashMap();
|
|
|
+ map.put("code","200");
|
|
|
+ map.put("msg","success");
|
|
|
+ System.out.println("----"+ map);
|
|
|
+// Set<Map.Entry<String, String>> entries = map.entrySet();
|
|
|
+// for (Map.Entry<String, String> entry : entries) {
|
|
|
+// String key = entry.getKey();
|
|
|
+// String value = entry.getValue();
|
|
|
+// System.out.println(key+"===="+value);
|
|
|
+// String error = entry.setValue("error");
|
|
|
+// String value1 = entry.getValue();
|
|
|
+// System.out.println(key+"===="+value1);
|
|
|
+// System.out.println(error);
|
|
|
+// }
|
|
|
+
|
|
|
+ Set<String> key = map.keySet();
|
|
|
+ map.get(key);
|
|
|
+ System.out.println("-------------");
|
|
|
+ map.forEach(new BiConsumer<String, String>() {
|
|
|
+ @Override
|
|
|
+ public void accept(String s, String s2) {
|
|
|
+ System.out.println("key--"+s);
|
|
|
+ System.out.println("value=="+s2);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ System.out.println("-------------");
|
|
|
+ //第二种
|
|
|
+ Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
|
|
|
+ while (iterator.hasNext()){
|
|
|
+ Map.Entry<String, String> entry = iterator.next();
|
|
|
+ entry.getKey();
|
|
|
+ entry.getValue();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 第三种
|
|
|
+ for (Map.Entry<String, String> entry : map.entrySet()) {
|
|
|
+ entry.getValue();
|
|
|
+ entry.getKey();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 只能获取value 只能获取key keyset
|
|
|
+ for (String value : map.values()) {
|
|
|
+ System.out.println(value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void t2(){
|
|
|
+ Map<String,String> map = new HashMap<>();
|
|
|
+ map.put("userName","admin");
|
|
|
+ map.put("pwd","123456");
|
|
|
+ map.put("pwd1","123456");
|
|
|
+ Map<String,String> map1 = new HashMap<>();
|
|
|
+ map1.putAll(map);
|
|
|
+ System.out.println(map1);
|
|
|
+ String pwd1 = map.remove("pwd1");
|
|
|
+ System.out.println(pwd1);
|
|
|
+ System.out.println(map);
|
|
|
+ map1.clear();
|
|
|
+ System.out.println(map1);
|
|
|
+ boolean userName = map.containsKey("userName");
|
|
|
+ System.out.println(userName);
|
|
|
+ boolean admin = map.containsValue("admin");
|
|
|
+ System.out.println(admin);
|
|
|
+ boolean equals = map.equals(map1);
|
|
|
+ System.out.println(equals);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void t4(){
|
|
|
+ // 实现定制排序
|
|
|
+ TreeMap<String,Object> map = new TreeMap<>(new Comparator<String>() {
|
|
|
+ @Override
|
|
|
+ public int compare(String o1, String o2) {
|
|
|
+ return o1.compareTo(o2);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ map.put("CC",45);
|
|
|
+ map.put("MM",78);
|
|
|
+ map.put("DD",56);
|
|
|
+ map.put("GG",89);
|
|
|
+ map.put("JJ",99);
|
|
|
+ System.out.println(map);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 自然排序、
|
|
|
+ * User
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void t5(){
|
|
|
+ TreeMap<User,String> treeMap = new TreeMap();
|
|
|
+ treeMap.put(new User(2,"zs"),"user1");
|
|
|
+ treeMap.put(new User(1,"li"),"user2");
|
|
|
+ treeMap.put(new User(3,"ww"),"user3");
|
|
|
+ System.out.println(treeMap);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void t6(){
|
|
|
+ Hashtable<String,String> hashtable = new Hashtable();
|
|
|
+ hashtable.put("user","admin");
|
|
|
+ hashtable.put("pwd","123");
|
|
|
+// hashtable.replaceAll(new BiFunction<String, String, String>() {
|
|
|
+// @Override // 满足key = s value = s2 将value替换成新的返回值
|
|
|
+// public String apply(String s, String s2) {
|
|
|
+//// String admin1 = s2.replaceAll(s2, "admin1");
|
|
|
+//// System.out.println("key = "+admin1);
|
|
|
+//// System.out.println("value = "+s2);
|
|
|
+// return "admin1";
|
|
|
+// }
|
|
|
+// });
|
|
|
+// System.out.println(hashtable);
|
|
|
+ hashtable.compute("user1", new BiFunction<String, String, String>() {
|
|
|
+ @Override // key 如果存在 更改value值 若不存在 新增一条记录entry[key 返回值]
|
|
|
+ public String apply(String s, String s2) {
|
|
|
+ return "qqq";
|
|
|
+ }
|
|
|
+ });
|
|
|
+ System.out.println(hashtable);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void t7(){
|
|
|
+ Properties properties = new Properties();
|
|
|
+ properties.setProperty("username","admin");
|
|
|
+ System.out.println(properties.getProperty("username"));
|
|
|
+ System.out.println(properties.getProperty("user"));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void t8(){
|
|
|
+ Properties properties = System.getProperties();
|
|
|
+ String property = properties.getProperty("file.encoding");
|
|
|
+ System.out.println(property);
|
|
|
+ properties.setProperty("file.encoding","GBK");
|
|
|
+ System.out.println(properties.getProperty("file.encoding"));
|
|
|
+ String property1 = System.getProperty("file.encoding");
|
|
|
+ System.out.println(property1);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void t9() throws IOException {
|
|
|
+ Properties properties = new Properties();
|
|
|
+ // jdbc.properties
|
|
|
+ properties.load(new FileInputStream("jdbc.properties")); //加载
|
|
|
+ String username = properties.getProperty("user_name");
|
|
|
+ System.out.println(username);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) throws IOException {
|
|
|
+ Properties properties = new Properties();
|
|
|
+ // jdbc.properties
|
|
|
+ properties.load(new FileInputStream("jdbc.properties")); //加载
|
|
|
+ String username = properties.getProperty("user_name");
|
|
|
+ System.out.println(username);
|
|
|
+ }
|
|
|
+}
|