3 changed files with 47 additions and 0 deletions
@ -0,0 +1,20 @@ |
|||
import java.util.HashMap; |
|||
import java.util.Map; |
|||
public class Cache<K, V> { |
|||
private final Map<K, V> cache; |
|||
public Cache() { |
|||
cache = new HashMap<>(); |
|||
} |
|||
public synchronized void put(K key, V value) { |
|||
cache.put(key, value); |
|||
} |
|||
public synchronized V get(K key) { |
|||
return cache.get(key); |
|||
} |
|||
public synchronized boolean hasKey(K key) { |
|||
return cache.containsKey(key); |
|||
} |
|||
public synchronized void remove(K key) { |
|||
cache.remove(key); |
|||
} |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
public class Main { |
|||
public void main(){ |
|||
Pair pair=new Pair("鞋子",7); |
|||
Pair pair2=pair.swap(pair); |
|||
Object a=pair2.getkey(); |
|||
Object b=pair2.getvalue(); |
|||
System.out.println("pair2的key是 "+a+" value 是 "+b); |
|||
} |
|||
} |
|||
@ -0,0 +1,18 @@ |
|||
public class Pair<K,V>{ |
|||
K key; |
|||
V value; |
|||
public Pair(K key,V value){ |
|||
this.key=key; |
|||
this.value=value; |
|||
} |
|||
public K getkey(){ |
|||
return key; |
|||
} |
|||
public V getvalue(){ |
|||
return value; |
|||
} |
|||
public static Pair swap (Pair pair){ |
|||
return new Pair(pair.getvalue(),pair.getkey()); |
|||
|
|||
} |
|||
} |
|||
Loading…
Reference in new issue