Browse Source

!57 给KV添加keep方法
Merge pull request !57 from hzh740053757/N/A

JFinal 5 years ago
parent
commit
48daacadcf
1 changed files with 18 additions and 1 deletions
  1. 18 1
      src/main/java/com/jfinal/kit/Kv.java

+ 18 - 1
src/main/java/com/jfinal/kit/Kv.java

@@ -74,7 +74,24 @@ public class Kv extends HashMap {
 		super.remove(key);
 		super.remove(key);
 		return this;
 		return this;
 	}
 	}
-	
+
+	public Kv keep(String... attrs){
+		if (attrs != null && attrs.length > 0) {
+			Kv t = Kv.create();
+			for (Object o : attrs) {
+				if(super.containsKey(o)){
+					t.set(o, get(o));
+				}				
+			}
+			super.clear();
+			super.putAll(t);
+		}
+		else {
+			super.clear();
+		}
+		return this;
+	}
+    
 	public <T> T getAs(Object key) {
 	public <T> T getAs(Object key) {
 		return (T)get(key);
 		return (T)get(key);
 	}
 	}